Example #1
0
class CategoryProductForm(forms.Form):
    primary_products = Select2MultipleField(
        label=_("Primary Category"),
        help_text=_("Set this category as a primary for selected products."),
        model=Product,
        required=False)
    additional_products = Select2MultipleField(
        label=_("Additional Category"),
        help_text=_("Add selected products to this category"),
        model=Product,
        required=False)
    remove_products = forms.MultipleChoiceField(
        label=_("Remove Products"),
        help_text=_("Remove selected products from this category"),
        required=False)

    def __init__(self, shop, category, **kwargs):
        self.shop = shop
        self.category = category
        super(CategoryProductForm, self).__init__(**kwargs)
        self.fields["remove_products"].choices = [(None, "-----")] + [
            (obj.product.pk, obj.product.name) for obj in category.shop_products.filter(shop=shop)
        ]

    @atomic
    def save(self):
        data = self.cleaned_data
        is_visible = self.category.status == CategoryStatus.VISIBLE
        visibility_groups = self.category.visibility_groups.all()
        primary_product_ids = [int(product_id) for product_id in data.get("primary_products", [])]
        for shop_product in ShopProduct.objects.filter(
                Q(shop_id=self.shop.id),
                Q(product_id__in=primary_product_ids) | Q(product__variation_parent_id__in=primary_product_ids)):
            shop_product.primary_category = self.category
            shop_product.visibility = (
                ShopProductVisibility.ALWAYS_VISIBLE if is_visible else ShopProductVisibility.NOT_VISIBLE
            )
            shop_product.visibility_limit = self.category.visibility.value
            shop_product.visibility_groups = visibility_groups
            shop_product.save()
            shop_product.categories.add(self.category)

        additional_product_ids = [int(product_id) for product_id in data.get("additional_products", [])]
        for shop_product in ShopProduct.objects.filter(
                Q(shop_id=self.shop.id),
                Q(product_id__in=additional_product_ids) | Q(product__variation_parent_id__in=additional_product_ids)):
            shop_product.categories.add(self.category)

        remove_product_ids = [int(product_id) for product_id in data.get("remove_products", [])]
        for shop_product in ShopProduct.objects.filter(
                Q(product_id__in=remove_product_ids) | Q(product__variation_parent_id__in=remove_product_ids)):
            if shop_product.primary_category == self.category:
                if self.category in shop_product.categories.all():
                    shop_product.categories.remove(self.category)
                shop_product.primary_category = None
                shop_product.save()
            shop_product.categories.remove(self.category)
Example #2
0
    def __init__(self, changing_user, *args, **kwargs):
        super(PermissionChangeFormBase, self).__init__(*args, **kwargs)
        self.changing_user = changing_user
        if getattr(self.instance, 'is_superuser', False) and not getattr(self.changing_user, 'is_superuser', False):
            self.fields.pop("is_superuser")

        if not (
            self.changing_user == self.instance or
            getattr(self.instance, 'is_superuser', False)
        ):
            # Only require old password when editing
            self.fields.pop("old_password")

        permission_groups_field = Select2MultipleField(
            model=PermissionGroup,
            required=False,
            label=_("Permission Groups"),
            help_text=_(
                "The permission groups that this user belongs to. "
                "Permission groups are configured through Contacts - Permission Groups."
            )
        )
        initial_groups = self._get_initial_groups()
        permission_groups_field.initial = [group.pk for group in initial_groups]
        permission_groups_field.widget.choices = [(group.pk, force_text(group)) for group in initial_groups]
        self.fields["permission_groups"] = permission_groups_field
Example #3
0
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop("request")
        super(SupplierBaseForm, self).__init__(*args, **kwargs)

        # add shops field when superuser only
        if getattr(self.request.user, "is_superuser", False):
            initial_shops = (self.instance.shops.all() if self.instance.pk else [])
            self.fields["shops"] = Select2MultipleField(
                label=_("Shops"),
                help_text=_("Select shops for this supplier. Keep it blank to share with all shops."),
                model=Shop,
                required=False,
                initial=initial_shops
            )
            self.fields["shops"].choices = initial_shops
            self.fields["shops"].widget.choices = [
                (shop.pk, force_text(shop)) for shop in initial_shops
            ]
        else:
            # drop shops fields
            self.fields.pop("shops", None)

        choices = Supplier.get_module_choices(
            empty_label=(_("No %s module") % Supplier._meta.verbose_name)
        )
        self.fields["module_identifier"].choices = self.fields["module_identifier"].widget.choices = choices
Example #4
0
 def init_fields(self):
     super(CompanyContactBaseForm, self).init_fields()
     self.fields["name"].help_text = _("The company name.")
     members_field = Select2MultipleField(model=PersonContact, required=False, help_text=_(
         "The contacts that are members of this company."
     ))
     if self.instance.pk and hasattr(self.instance, "members"):
         members_field.widget.choices = [
             (object.pk, force_text(object)) for object in self.instance.members.all()
         ]
     self.fields["members"] = members_field
Example #5
0
class CouponsUsageForm(OrderReportForm):
    coupon = Select2MultipleField(label=_("Coupon"),
                                  model=Coupon,
                                  required=False,
                                  help_text=_("Filter report results by coupon."))

    def __init__(self, *args, **kwargs):
        super(CouponsUsageForm, self).__init__(*args, **kwargs)

        if self.data and "coupon" in self.data:
            coupon = Coupon.objects.filter(pk__in=self.data.getlist("coupon"))
            self.fields["coupon"].initial = coupon
            self.fields["coupon"].widget.choices = [(obj.pk, obj.code) for obj in coupon]
Example #6
0
    def __init__(self, *args, **kwargs):
        super(OrderReportForm, self).__init__(*args, **kwargs)

        customer_field = Select2MultipleField(label=_("Customer"),
                                              model=Contact,
                                              required=False,
                                              help_text=_("Filter report results by customer."))
        customers = self.initial_contacts("customer")
        if customers:
            customer_field.initial = customers
            customer_field.widget.choices = [(obj.pk, obj.name) for obj in customers]
        orderer_field = Select2MultipleField(
            label=_("Orderer"), model=Contact, required=False, help_text=_(
                "Filter report results by the person that made the order."
            )
        )
        orderers = self.initial_contacts("orderer")
        if orderers:
            orderer_field.initial = orderers
            orderer_field.widget.choices = [(obj.pk, obj.name) for obj in orderers]
        self.fields["customer"] = customer_field
        self.fields["orderer"] = orderer_field
Example #7
0
class TaxesReportForm(OrderReportForm):
    tax = Select2MultipleField(label=_("Tax"),
                               model=Tax,
                               required=False,
                               help_text=_("Filter report results by tax."))

    tax_class = Select2MultipleField(label=_("Tax Class"),
                                     model=TaxClass,
                                     required=False,
                                     help_text=_("Filter report results by tax class."))

    def __init__(self, *args, **kwargs):
        super(TaxesReportForm, self).__init__(*args, **kwargs)

        if self.data and "tax" in self.data:
            taxes = Tax.objects.filter(pk__in=self.data.getlist("tax"))
            self.fields["tax"].initial = taxes.first()
            self.fields["tax"].widget.choices = [(obj.pk, obj.name) for obj in taxes]

        if self.data and "tax_class" in self.data:
            tax_classes = TaxClass.objects.filter(pk__in=self.data.getlist("tax_class"))
            self.fields["tax_class"].initial = tax_classes
            self.fields["tax_class"].widget.choices = [(obj.pk, obj.name) for obj in tax_classes]
Example #8
0
class ShippingReportForm(OrderReportForm):
    shipping_method = Select2MultipleField(label=_("Shipping Method"),
                                           model=ShippingMethod,
                                           required=False,
                                           help_text=_("Filter report results by shipping method."))

    carrier = Select2MultipleField(label=_("Carrier"),
                                   model=Carrier,
                                   required=False,
                                   help_text=_("Filter report results by carrier."))

    def __init__(self, *args, **kwargs):
        super(ShippingReportForm, self).__init__(*args, **kwargs)

        if self.data and "shipping_method" in self.data:
            shipping_method = ShippingMethod.objects.filter(pk__in=self.data.getlist("shipping_method"))
            self.fields["shipping_method"].initial = shipping_method.first()
            self.fields["shipping_method"].widget.choices = [(obj.pk, obj.name) for obj in shipping_method]

        if self.data and "carrier" in self.data:
            carrier = Carrier.objects.filter(pk__in=self.data.getlist("carrier"))
            self.fields["carrier"].initial = carrier
            self.fields["carrier"].widget.choices = [(obj.pk, obj.name) for obj in carrier]
Example #9
0
 def __init__(self, *args, **kwargs):
     self.request = kwargs.pop("request")
     super(ManufacturerForm, self).__init__(*args, **kwargs)
     # add shops field when superuser only
     if getattr(self.request.user, "is_superuser", False):
         self.fields["shops"] = Select2MultipleField(
             label=_("Shops"),
             help_text=_("Select shops for this manufacturer. Keep it blank to share with all shops."),
             model=Shop,
             required=False,
         )
         initial_shops = (self.instance.shops.all() if self.instance.pk else [])
         self.fields["shops"].widget.choices = [(shop.pk, force_text(shop)) for shop in initial_shops]
     else:
         # drop shops fields
         self.fields.pop("shops", None)
Example #10
0
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop("request")
        self.shop = get_shop(self.request)
        super(CouponCodeForm, self).__init__(*args, **kwargs)

        if self.instance.pk:
            self.fields["coupon_code_discounts"] = Select2MultipleField(
                label=_("Product Discounts"),
                help_text=_("Select discounts linked to this coupon code."),
                model=Discount,
                required=False
            )
            initial_discounts = (self.instance.coupon_code_discounts.all() if self.instance.pk else [])
            self.fields["coupon_code_discounts"].initial = initial_discounts
            self.fields["coupon_code_discounts"].widget.choices = [
                (discount.pk, force_text(discount)) for discount in initial_discounts
            ]
Example #11
0
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop("request")
        self.shop = get_shop(self.request)
        super(AvailabilityExceptionForm, self).__init__(*args, **kwargs)

        if self.instance.pk:
            self.fields["discounts"] = Select2MultipleField(
                label=_("Product Discounts"),
                help_text=_("Select discounts to be ignored on given time frame."),
                model=Discount,
                required=False
            )
            initial_discounts = (self.instance.discounts.all() if self.instance.pk else [])
            self.fields["discounts"].initial = initial_discounts
            self.fields["discounts"].widget.choices = [
                (discount.pk, force_text(discount)) for discount in initial_discounts
            ]
Example #12
0
    def __init__(self, **kwargs):
        super(ShopBaseForm, self).__init__(**kwargs)
        self.fields["currency"] = forms.ChoiceField(
            choices=get_currency_choices(),
            required=True,
            label=_("Currency"),
            help_text=_("The primary shop currency. This is the currency used when selling your products.")
        )

        staff_members = Select2MultipleField(
            label=_("Staff"),
            help_text=_("Select staff members for this shop."),
            model=get_user_model(),
            required=False
        )
        staff_members.widget = QuickAddUserMultiSelect(attrs={"data-model": "auth.User"})
        initial_members = (self.instance.staff_members.all() if self.instance.pk else [])
        staff_members.widget.choices = [(member.pk, force_text(member)) for member in initial_members]
        self.fields["staff_members"] = staff_members
        self.fields["domain"].required = E-CommerceSettings.get_setting("E-Commerce_ENABLE_MULTIPLE_SHOPS")
        self.disable_protected_fields()
Example #13
0
class ProductTypeForm(MultiLanguageModelForm):
    attributes = Select2MultipleField(model=Attribute, required=False, help_text=_(
        "Select attributes that go with your product type. These are defined in Products Settings - Attributes."
    ))

    class Meta:
        model = ProductType
        exclude = ()

    def __init__(self, **kwargs):
        super(ProductTypeForm, self).__init__(**kwargs)
        if self.instance.pk:
            choices = [(a.pk, a.name) for a in self.instance.attributes.all()]
            self.fields["attributes"].initial = [pk for pk, name in choices]

    def clean_attributes(self):
        attributes = [int(a_id) for a_id in self.cleaned_data.get("attributes", [])]
        return Attribute.objects.filter(pk__in=attributes).all()

    def save(self, commit=True):
        obj = super(ProductTypeForm, self).save(commit=commit)
        obj.attributes.clear()
        obj.attributes = self.cleaned_data["attributes"]
        return self.instance
Example #14
0
    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop("request")
        self.shop = get_shop(self.request)
        super(HappyHourForm, self).__init__(*args, **kwargs)

        if self.instance.pk:
            self.fields["discounts"] = Select2MultipleField(
                label=_("Product Discounts"),
                help_text=_("Select discounts for this happy hour."),
                model=Discount,
                required=False
            )
            initial_discounts = (self.instance.discounts.all() if self.instance.pk else [])
            self.fields["discounts"].initial = initial_discounts
            self.fields["discounts"].widget.choices = [
                (discount.pk, force_text(discount)) for discount in initial_discounts
            ]

        if self.instance.pk:
            weekdays, from_hour, to_hour = _get_initial_data_for_time_ranges(self.instance)
            if weekdays and from_hour and to_hour:
                self.fields["weekdays"].initial = weekdays
                self.fields["from_hour"].initial = from_hour
                self.fields["to_hour"].initial = to_hour

        # Since we touch these views in init we need to reset some
        # widgets and help texts after setting the initial values.
        self.fields["from_hour"].widget = TimeInput(attrs={"class": "time"})
        self.fields["to_hour"].widget = TimeInput(attrs={"class": "time"})
        help_texts = [
            ("from_hour", _("12pm is considered noon and 12am as midnight. Start hour is included to the discount.")),
            ("to_hour", _("12pm is considered noon and 12am as midnight. End hours is included to the discount.")),
            ("weekdays", _("Weekdays the happy hour is active."))
        ]
        for field, help_text in help_texts:
            self.fields[field].help_text = help_text