示例#1
0
class CustomerResource(resources.ModelResource):
    id = fields.Field(attribute='id', widget=IdWidget(), readonly=True)
    email = fields.Field(attribute='user',
                         default="*****@*****.**",
                         widget=OneToOneWidget(User, 'email'),
                         readonly=False)
    phone1 = fields.Field(attribute='phone1',
                          default="1234",
                          widget=CharWidget(),
                          readonly=False)
    phone2 = fields.Field(attribute='phone2',
                          widget=CharWidget(),
                          readonly=False)
    date_balance = fields.Field(attribute='get_admin_date_balance',
                                widget=CharWidget(),
                                readonly=True)
    balance = fields.Field(attribute='get_admin_balance',
                           widget=TwoMoneysWidget(),
                           readonly=True)
    may_order = fields.Field(attribute='may_order',
                             default=False,
                             widget=DecimalBooleanWidget(),
                             readonly=False)
    is_group = fields.Field(attribute='is_group',
                            default=False,
                            widget=DecimalBooleanWidget(),
                            readonly=False)
    represent_this_buyinggroup = fields.Field(
        attribute='represent_this_buyinggroup',
        default=False,
        widget=DecimalBooleanWidget(),
        readonly=True)
    is_active = fields.Field(attribute='is_active',
                             widget=DecimalBooleanWidget(),
                             readonly=True)
    membership_fee_valid_until = fields.Field(
        attribute='membership_fee_valid_until',
        default=timezone.now().date(),
        widget=DateWidgetExcel(),
        readonly=False)
    last_membership_fee = fields.Field(attribute='get_last_membership_fee',
                                       widget=TwoMoneysWidget(),
                                       readonly=True)
    last_membership_fee_date = fields.Field(
        attribute='last_membership_fee_date',
        widget=DateWidgetExcel(),
        readonly=True)
    purchase = fields.Field(attribute='get_purchase',
                            widget=ZeroDecimalsWidget(),
                            readonly=True)
    participation = fields.Field(attribute='get_participation',
                                 widget=ZeroDecimalsWidget(),
                                 readonly=True)
    delivery_point = fields.Field(attribute='delivery_point',
                                  widget=TranslatedForeignKeyWidget(
                                      LUT_DeliveryPoint, field='short_name'))
    valid_email = fields.Field(attribute='valid_email',
                               widget=DecimalBooleanWidget(),
                               readonly=True)
    zero_waste = fields.Field(attribute='zero_waste',
                              widget=DecimalBooleanWidget(),
                              readonly=True)
    date_joined = fields.Field(attribute='get_admin_date_joined',
                               widget=CharWidget(),
                               readonly=True)

    def before_save_instance(self, instance, using_transactions, dry_run):
        """
        Override to add additional logic.
        """
        user_model = get_user_model()
        user_email_qs = user_model.objects.filter(email=instance.user.email,
                                                  is_staff=False).order_by('?')
        user_username_qs = user_model.objects.filter(
            username=instance.short_basket_name).order_by('?')
        if instance.id is not None:
            customer = Customer.objects.filter(
                id=instance.id).order_by('?').only('id', 'user_id').first()
            user_email_qs = user_email_qs.exclude(id=customer.user_id)
            user_username_qs = user_username_qs.exclude(id=customer.user_id)
        else:
            customer = None
        if user_email_qs.exists():
            raise ValueError(
                _("The email {} is already used by another user.").format(
                    instance.user.email))
        if user_username_qs.exists():
            raise ValueError(
                _("The short_basket_name {} is already used by another user.").
                format(instance.short_basket_name))
        if using_transactions or not dry_run:
            if instance.id is not None:
                email = instance.user.email
                instance.user = user_model.objects.get(id=customer.user_id)
                instance.user.username = instance.short_basket_name
                instance.user.first_name = EMPTY_STRING
                instance.user.last_name = instance.short_basket_name
                instance.user.email = email
                instance.user.save()
            else:
                instance.user = user_model.objects.create_user(
                    username=instance.short_basket_name,
                    email=instance.user.email,
                    password=uuid.uuid1().hex,
                    first_name=EMPTY_STRING,
                    last_name=instance.short_basket_name)
                instance.user_id = instance.user.id

    class Meta:
        model = Customer
        fields = ('id', 'may_order', 'short_basket_name', 'long_basket_name',
                  'email', 'email2', 'language', 'phone1', 'phone2', 'address',
                  'city', 'bank_account1', 'bank_account2', 'date_balance',
                  'balance', 'price_list_multiplier',
                  'membership_fee_valid_until', 'last_membership_fee',
                  'last_membership_fee_date', 'participation', 'purchase',
                  'represent_this_buyinggroup', 'is_group', 'is_active',
                  'delivery_point', 'zero_waste', 'valid_email')
        export_order = fields
        import_id_fields = ('id', )
        skip_unchanged = True
        report_skipped = False
        use_transactions = False
示例#2
0
文件: product.py 项目: CenaZ/repanier
class ProductResource(resources.ModelResource):
    id = fields.Field(attribute='id', widget=IdWidget(), readonly=True)
    producer_name = fields.Field(attribute='producer',
                                 widget=ForeignKeyWidget(
                                     Producer, field='short_profile_name'))
    long_name = fields.Field(attribute='long_name')
    department_for_customer = fields.Field(attribute='department_for_customer',
                                           widget=TranslatedForeignKeyWidget(
                                               LUT_DepartmentForCustomer,
                                               field='short_name'))
    order_unit = fields.Field(attribute='order_unit',
                              widget=ChoiceWidget(
                                  LUT_PRODUCT_ORDER_UNIT,
                                  LUT_PRODUCT_ORDER_UNIT_REVERSE))
    order_average_weight = fields.Field(attribute='order_average_weight',
                                        widget=ThreeDecimalsWidget())
    producer_unit_price = fields.Field(attribute='producer_unit_price',
                                       widget=TwoMoneysWidget())
    customer_unit_price = fields.Field(attribute='customer_unit_price',
                                       widget=TwoMoneysWidget())
    unit_deposit = fields.Field(attribute='unit_deposit',
                                widget=TwoMoneysWidget())
    vat_level = fields.Field(attribute='vat_level',
                             widget=ChoiceWidget(LUT_ALL_VAT,
                                                 LUT_ALL_VAT_REVERSE))
    customer_minimum_order_quantity = fields.Field(
        attribute='customer_minimum_order_quantity',
        widget=ThreeDecimalsWidget())
    customer_increment_order_quantity = fields.Field(
        attribute='customer_increment_order_quantity',
        widget=ThreeDecimalsWidget())
    customer_alert_order_quantity = fields.Field(
        attribute='customer_alert_order_quantity',
        widget=ThreeDecimalsWidget())
    wrapped = fields.Field(attribute='wrapped', widget=DecimalBooleanWidget())
    stock = fields.Field(attribute='stock', widget=ThreeDecimalsWidget())
    limit_order_quantity_to_stock = fields.Field(
        attribute='limit_order_quantity_to_stock',
        widget=DecimalBooleanWidget(),
        readonly=False)
    producer_order_by_quantity = fields.Field(
        attribute='producer_order_by_quantity', widget=ThreeDecimalsWidget())
    label = fields.Field(attribute='production_mode',
                         widget=TranslatedManyToManyWidget(LUT_ProductionMode,
                                                           separator="; ",
                                                           field='short_name'))
    picture = fields.Field(attribute='picture2', readonly=True)
    is_into_offer = fields.Field(attribute='is_into_offer',
                                 widget=DecimalBooleanWidget(),
                                 readonly=True)
    is_active = fields.Field(attribute='is_active',
                             widget=DecimalBooleanWidget(),
                             readonly=True)

    def before_save_instance(self, instance, using_transactions, dry_run):
        """
        Override to add additional logic.
        """
        if instance.wrapped is None:
            instance.wrapped = False
        if instance.producer_unit_price is None:
            instance.producer_unit_price = REPANIER_MONEY_ZERO
        if instance.customer_unit_price is None:
            instance.customer_unit_price = REPANIER_MONEY_ZERO
        if instance.unit_deposit is None:
            instance.unit_deposit = REPANIER_MONEY_ZERO
        if instance.customer_minimum_order_quantity is None:
            instance.customer_minimum_order_quantity = DECIMAL_ZERO
        if instance.customer_increment_order_quantity is None:
            instance.customer_increment_order_quantity = DECIMAL_ZERO
        if instance.customer_alert_order_quantity is None:
            instance.customer_alert_order_quantity = DECIMAL_ZERO
        if instance.stock is None:
            instance.stock = DECIMAL_ZERO
        if instance.producer_order_by_quantity is None:
            instance.producer_order_by_quantity = DECIMAL_ZERO
        if instance.order_unit is None:
            raise ValueError(_('The order unit must be set.'))
        if instance.order_unit != PRODUCT_ORDER_UNIT_DEPOSIT:
            if instance.producer_unit_price < DECIMAL_ZERO:
                raise ValueError(
                    _('The price must be greater than or equal to zero.'))
            if instance.customer_unit_price < DECIMAL_ZERO:
                raise ValueError(
                    _('The price must be greater than or equal to zero.'))
            if instance.order_unit in [
                    PRODUCT_ORDER_UNIT_PC, PRODUCT_ORDER_UNIT_PC_PRICE_KG,
                    PRODUCT_ORDER_UNIT_PC_PRICE_LT,
                    PRODUCT_ORDER_UNIT_PC_PRICE_PC, PRODUCT_ORDER_UNIT_PC_KG
            ]:
                # Do not allow decimal value when the qty represents pieces.
                if instance.customer_minimum_order_quantity != instance.customer_minimum_order_quantity // 1:
                    raise ValueError(
                        _('The minimum order quantity must be an integer.'))
                if instance.customer_increment_order_quantity != instance.customer_increment_order_quantity // 1:
                    raise ValueError(_('The increment must be an integer.'))
                if instance.stock != instance.stock // 1:
                    raise ValueError(_('The stock must be an integer.'))
                if instance.customer_alert_order_quantity != instance.customer_alert_order_quantity // 1:
                    raise ValueError(
                        _('The alert quantity must be an integer.'))

        if instance.order_unit < PRODUCT_ORDER_UNIT_DEPOSIT:
            if instance.customer_minimum_order_quantity <= DECIMAL_ZERO:
                raise ValueError(
                    _('The minimum order quantity must be greater than zero.'))

            if instance.customer_minimum_order_quantity != instance.customer_alert_order_quantity \
                    and instance.customer_increment_order_quantity <= DECIMAL_ZERO:
                raise ValueError(_('The increment must be greater than zero.'))

        qs = Product.objects.filter(reference=instance.reference,
                                    producer=instance.producer).order_by('?')
        if instance.id is not None:
            qs = qs.exclude(id=instance.id)
        if qs.exists():
            raise ValueError(
                _("The reference %(reference)s is already used by %(product)s")
                % {
                    'reference': instance.reference,
                    'product': qs.first()
                })

    class Meta:
        model = Product
        fields = ('id', 'producer_name', 'reference',
                  'department_for_customer', 'long_name', 'order_unit',
                  'wrapped', 'order_average_weight', 'producer_unit_price',
                  'customer_unit_price', 'unit_deposit', 'vat_level',
                  'customer_minimum_order_quantity',
                  'customer_increment_order_quantity',
                  'customer_alert_order_quantity', 'stock',
                  'limit_order_quantity_to_stock',
                  'producer_order_by_quantity', 'label', 'picture',
                  'is_into_offer', 'is_active')
        export_order = fields
        import_id_fields = ('id', )
        skip_unchanged = True
        report_skipped = False
        use_transactions = False
示例#3
0
class ProducerResource(resources.ModelResource):

    id = fields.Field(attribute="id", widget=IdWidget(), readonly=True)
    phone1 = fields.Field(attribute="phone1",
                          widget=CharWidget(),
                          readonly=False)
    phone2 = fields.Field(attribute="phone2",
                          widget=CharWidget(),
                          readonly=False)

    price_list_multiplier = fields.Field(
        attribute="price_list_multiplier",
        default=DECIMAL_ONE,
        widget=TwoDecimalsWidget(),
        readonly=False,
    )
    date_balance = fields.Field(attribute="get_admin_date_balance",
                                widget=DateWidgetExcel(),
                                readonly=True)
    balance = fields.Field(attribute="get_admin_balance",
                           widget=TwoMoneysWidget(),
                           readonly=True)
    invoice_by_basket = fields.Field(
        attribute="invoice_by_basket",
        default=False,
        widget=DecimalBooleanWidget(),
        readonly=False,
    )
    sort_products_by_reference = fields.Field(
        attribute="sort_products_by_reference",
        default=False,
        widget=DecimalBooleanWidget(),
        readonly=False,
    )
    represent_this_buyinggroup = fields.Field(
        attribute="represent_this_buyinggroup",
        default=False,
        widget=DecimalBooleanWidget(),
        readonly=True,
    )
    is_active = fields.Field(attribute="is_active",
                             widget=DecimalBooleanWidget(),
                             readonly=True)
    reference_site = fields.Field(attribute="reference_site", readonly=True)

    def before_save_instance(self, instance, using_transactions, dry_run):
        """
        Override to add additional logic.
        """
        producer_qs = Producer.objects.filter(
            short_profile_name=instance.short_profile_name).order_by("?")
        if instance.id is not None:
            producer_qs = producer_qs.exclude(id=instance.id)
        if producer_qs.exists():
            raise ValueError(
                _("The short_basket_name {} is already used by another producer."
                  ).format(instance.short_profile_name))

    class Meta:
        model = Producer
        fields = (
            "id",
            "short_profile_name",
            "long_profile_name",
            "email",
            "email2",
            "email3",
            "language",
            "phone1",
            "phone2",
            "fax",
            "address",
            "invoice_by_basket",
            "sort_products_by_reference",
            "producer_price_are_wo_vat",
            "price_list_multiplier",
            "reference_site",
            "bank_account",
            "date_balance",
            "balance",
            "represent_this_buyinggroup",
            "is_active",
        )
        export_order = fields
        import_id_fields = ("id", )
        skip_unchanged = True
        report_skipped = False
        use_transactions = False
示例#4
0
class ProducerResource(resources.ModelResource):
    id = fields.Field(attribute='id', widget=IdWidget(), readonly=True)
    phone1 = fields.Field(attribute='phone1',
                          default='1234',
                          widget=CharWidget(),
                          readonly=False)
    phone2 = fields.Field(attribute='phone2',
                          widget=CharWidget(),
                          readonly=False)
    price_list_multiplier = fields.Field(attribute='price_list_multiplier',
                                         default=DECIMAL_ONE,
                                         widget=TwoDecimalsWidget(),
                                         readonly=False)
    date_balance = fields.Field(attribute='get_admin_date_balance',
                                widget=DateWidgetExcel(),
                                readonly=True)
    balance = fields.Field(attribute='get_admin_balance',
                           widget=TwoMoneysWidget(),
                           readonly=True)
    invoice_by_basket = fields.Field(attribute='invoice_by_basket',
                                     default=False,
                                     widget=DecimalBooleanWidget(),
                                     readonly=False)
    manage_replenishment = fields.Field(attribute='manage_replenishment',
                                        default=False,
                                        widget=DecimalBooleanWidget(),
                                        readonly=False)
    sort_products_by_reference = fields.Field(
        attribute='sort_products_by_reference',
        default=False,
        widget=DecimalBooleanWidget(),
        readonly=False)
    is_resale_price_fixed = fields.Field(attribute='is_resale_price_fixed',
                                         default=False,
                                         widget=DecimalBooleanWidget(),
                                         readonly=False)
    represent_this_buyinggroup = fields.Field(
        attribute='represent_this_buyinggroup',
        default=False,
        widget=DecimalBooleanWidget(),
        readonly=True)
    is_active = fields.Field(attribute='is_active',
                             widget=DecimalBooleanWidget(),
                             readonly=True)
    reference_site = fields.Field(attribute='reference_site', readonly=True)

    def before_save_instance(self, instance, using_transactions, dry_run):
        """
        Override to add additional logic.
        """
        if not settings.REPANIER_SETTINGS_STOCK:
            if instance.manage_replenishment:
                raise ValueError(
                    _("Manage replenishment must be set to False because this option is not activated."
                      ))
        producer_qs = Producer.objects.filter(
            short_profile_name=instance.short_profile_name).order_by('?')
        if instance.id is not None:
            producer_qs = producer_qs.exclude(id=instance.id)
        if producer_qs.exists():
            raise ValueError(
                _("The short_basket_name {} is already used by another producer."
                  ).format(instance.short_profile_name))

    class Meta:
        model = Producer
        fields = ('id', 'short_profile_name', 'long_profile_name', 'email',
                  'email2', 'email3', 'language', 'phone1', 'phone2', 'fax',
                  'address', 'invoice_by_basket', 'manage_replenishment',
                  'sort_products_by_reference', 'producer_pre_opening',
                  'producer_price_are_wo_vat', 'price_list_multiplier',
                  'is_resale_price_fixed', 'reference_site', 'bank_account',
                  'date_balance', 'balance', 'represent_this_buyinggroup',
                  'is_active')
        export_order = fields
        import_id_fields = ('id', )
        skip_unchanged = True
        report_skipped = False
        use_transactions = False