def form_valid(self, form):
        if self.__pk:
            obj = PurchasesAlbaran.objects.get(pk=self.__pk)
            self.request.albaran = obj
            form.instance.albaran = obj

        form.instance.validator_user = self.request.user

        raise Exception("revisar StorageBatch")
        """
        batch = StorageBatch.objects.filter(pk=form.data['batch']).first()
        if not batch:
            errors = form._errors.setdefault("batch", ErrorList())
            errors.append(_("Batch invalid"))
            return super(LineAlbaranCreate, self).form_invalid(form)
        """

        # comprueba si el producto comprado requiere un valor de atributo especial
        product_final = ProductFinal.objects.filter(pk=form.data['product']).first()
        feature_special_value = None
        if not product_final:
            errors = form._errors.setdefault("feature_special_value", ErrorList())
            errors.append(_("Product not selected"))
            return super(LineAlbaranCreate, self).form_invalid(form)
        elif product_final.product.feature_special:
            # es obligatorio la informacion de caracteristicas especiales
            if 'feature_special_value' not in form.data or not form.data['feature_special_value']:
                errors = form._errors.setdefault("feature_special_value", ErrorList())
                errors.append(_("Product needs information of feature special"))
                return super(LineAlbaranCreate, self).form_invalid(form)
            else:
                feature_special_value = list(set(filter(None, form.data['feature_special_value'].split('\n'))))
                try:
                    quantity = int(float(form.data['quantity']))
                except ValueError:
                    errors = form._errors.setdefault("quantity", ErrorList())
                    errors.append(_("Quantity is not valid"))
                    return super(LineAlbaranCreate, self).form_invalid(form)

                if product_final.product.feature_special.unique:
                    # mismo numero de caracteristicas que de cantidades
                    # si el feature special esta marcado como 'unico'
                    if len(feature_special_value) != quantity:
                        errors = form._errors.setdefault("feature_special_value", ErrorList())
                        errors.append(_("Quantity and values of feature special not equals"))
                        return super(LineAlbaranCreate, self).form_invalid(form)
                    # no existen las caracteristicas especiales dadas de alta en el sistema
                    elif ProductUnique.objects.filter(product_final=product_final, value__in=feature_special_value).exists():
                        errors = form._errors.setdefault("feature_special_value", ErrorList())
                        errors.append(_("Some value of feature special exists"))
                        return super(LineAlbaranCreate, self).form_invalid(form)
                elif len(feature_special_value) != 1:
                    errors = form._errors.setdefault("feature_special_value", ErrorList())
                    errors.append(_("The special feature must be unique for all products"))
                    return super(LineAlbaranCreate, self).form_invalid(form)
        try:
            with transaction.atomic():
                # save line albaran
                result = super(LineAlbaranCreate, self).form_valid(form)

                raise Exception("Cambiar ProductStock por ProductUnique")
                """
                if self.object.status != PURCHASE_ALBARAN_LINE_STATUS_REJECTED:
                    # prepare stock
                    ps = ProductStock()
                    ps.product_final = product_final
                    ps.line_albaran = self.object
                    ps.batch = batch
                    # save stock
                    ps.quantity = self.object.quantity
                    ps.save()

                    if feature_special_value:
                        # prepare product feature special
                        if product_final.product.feature_special.unique:
                            pfs = ProductUnique()
                            pfs.product_final = product_final
                            # save product featureSpecial and stock
                            for fs in feature_special_value:
                                pfs.pk = None
                                pfs.value = fs
                                pfs.save()

                        else:
                            pfs = ProductUnique.objects.filter(
                                value=feature_special_value[0],
                                product_final=product_final
                            ).first()
                            if pfs:
                                pfs.stock_real += self.object.quantity
                            else:
                                pfs = ProductUnique()
                                pfs.product_final = product_final
                                pfs.value = feature_special_value[0]
                                pfs.stock_real = self.object.quantity
                            pfs.save()
                    else:
                        # product unique by default
                        pfs = ProductUnique.objects.filter(product_final=product_final).first()
                        if not pfs:
                            pfs = ProductUnique()
                            pfs.product_final = product_final
                            pfs.stock_real = self.object.quantity
                        else:
                            pfs.stock_real += self.object.quantity
                        pfs.save()
                """
                return result
        except IntegrityError as e:
            errors = form._errors.setdefault("product", ErrorList())
            errors.append(_("Integrity Error: {}".format(e)))
            return super(LineAlbaranCreate, self).form_invalid(form)
def form_valid ( self , form ) : if self . __pk : obj = PurchasesAlbaran . objects . get ( pk = self . __pk ) self . request . albaran = obj form . instance . albaran = obj form . instance . validator_user = self . request . user raise Exception ( "revisar StorageBatch" ) product_final = ProductFinal . objects . filter ( pk = form . data [ 'product' ] ) . first ( ) feature_special_value = None if not product_final : errors = form . _errors . setdefault ( "feature_special_value" , ErrorList ( ) ) errors . append ( _ ( "Product not selected" ) ) return super ( LineAlbaranCreate , self ) . form_invalid ( form ) elif product_final . product . feature_special : if 'feature_special_value' not in form . data or not form . data [ 'feature_special_value' ] : errors = form . _errors . setdefault ( "feature_special_value" , ErrorList ( ) ) errors . append ( _ ( "Product needs information of feature special" ) ) return super ( LineAlbaranCreate , self ) . form_invalid ( form ) else : feature_special_value = list ( set ( filter ( None , form . data [ 'feature_special_value' ] . split ( '\n' ) ) ) ) try : quantity = int ( float ( form . data [ 'quantity' ] ) ) except ValueError : errors = form . _errors . setdefault ( "quantity" , ErrorList ( ) ) errors . append ( _ ( "Quantity is not valid" ) ) return super ( LineAlbaranCreate , self ) . form_invalid ( form ) if product_final . product . feature_special . unique : if len ( feature_special_value ) != quantity : errors = form . _errors . setdefault ( "feature_special_value" , ErrorList ( ) ) errors . append ( _ ( "Quantity and values of feature special not equals" ) ) return super ( LineAlbaranCreate , self ) . form_invalid ( form ) elif ProductUnique . objects . filter ( product_final = product_final , value__in = feature_special_value ) . exists ( ) : errors = form . _errors . setdefault ( "feature_special_value" , ErrorList ( ) ) errors . append ( _ ( "Some value of feature special exists" ) ) return super ( LineAlbaranCreate , self ) . form_invalid ( form ) elif len ( feature_special_value ) != 1 : errors = form . _errors . setdefault ( "feature_special_value" , ErrorList ( ) ) errors . append ( _ ( "The special feature must be unique for all products" ) ) return super ( LineAlbaranCreate , self ) . form_invalid ( form ) try : with transaction . atomic ( ) : result = super ( LineAlbaranCreate , self ) . form_valid ( form ) raise Exception ( "Cambiar ProductStock por ProductUnique" ) return result except IntegrityError as e : errors = form . _errors . setdefault ( "product" , ErrorList ( ) ) errors . append ( _ ( "Integrity Error: {}" . format ( e ) ) ) return super ( LineAlbaranCreate , self ) . form_invalid ( form )
batch = StorageBatch.objects.filter(pk=form.data['batch']).first()
        if not batch:
            errors = form._errors.setdefault("batch", ErrorList())
            errors.append(_("Batch invalid"))
            return super(LineAlbaranCreate, self).form_invalid(form)