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]
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]
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
def __init__(self, *args, **kwargs): super(ManufacturerOrderReportForm, self).__init__(*args, **kwargs) manufacturer_field = Select2MultipleField( label=_("Manufacturer"), model=Manufacturer, required=False, help_text=_("Filter report results by manufacturer.")) manufacturers = self.initial_manufacturers("manufacturer") if manufacturers: manufacturer_field.initial = manufacturers manufacturer_field.widget.choices = [(obj.pk, obj.name) for obj in manufacturers] orderer_field = Select2MultipleField( label=_("Orderer"), model=Contact, required=False, help_text=_( "Filter report results by the person that made the order.")) orderers = self.initial_manufacturers("orderer") if orderers: orderer_field.initial = orderers orderer_field.widget.choices = [(obj.pk, obj.name) for obj in orderers] self.fields["manufacturer"] = manufacturer_field self.fields["orderer"] = orderer_field
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)
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 = ShuupSettings.get_setting( "SHUUP_ENABLE_MULTIPLE_SHOPS") self.disable_protected_fields()
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"].widget.choices = choices 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
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
def __init__(self, *args, **kwargs): super(PermissionGroupForm, self).__init__(*args, **kwargs) initial_permissions = self._get_initial_permissions() self.fields["name"].help_text = _("The permission group name.") self.fields["modules"] = forms.MultipleChoiceField( choices=sorted(self._get_module_choices()), initial=self._get_enabled_modules(initial_permissions), required=False, label=_("Module Permissions"), help_text=_( "Select the modules that should be accessible by this permission group. " "Modules with the same permissions as selected modules will be added automatically." ) ) initial_members = self._get_initial_members() members_field = Select2MultipleField( model=get_user_model(), initial=[member.pk for member in initial_members], required=False, label=_("Members"), help_text=_( "Set the users that belong to this permission group." ) ) members_field.widget.choices = [(member.pk, force_text(member)) for member in initial_members] self.fields["members"] = members_field
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") initial_groups = self._get_initial_groups() permission_groups_field = Select2MultipleField( model=PermissionGroup, initial=[group.pk for group in initial_groups], required=False, label=_("Permission Groups"), help_text=_( "The permission groups that this user belongs to. " "Permission groups are configured through Contacts - Permission Groups." )) permission_groups_field.widget.choices = [(group.pk, force_text(group)) for group in initial_groups] self.fields["permission_groups"] = permission_groups_field
def init_fields(self): super(CompanyContactBaseForm, self).init_fields() members_field = Select2MultipleField(model=PersonContact, required=False) 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
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
def __init__(self, *args, **kwargs): super(PermissionGroupForm, self).__init__(*args, **kwargs) self.fields["name"].help_text = _("The Permission Group name.") initial_members = self._get_initial_members() members_field = Select2MultipleField( model=get_user_model(), initial=[member.pk for member in initial_members], required=False, label=_("Members"), help_text=_("Set the users that belong to this Permission Group."), ) members_field.widget.choices = [(member.pk, force_text(member)) for member in initial_members] self.fields["members"] = members_field initial_permissions = list(get_permissions_from_group( self.instance.pk)) if self.instance.pk else [] self.admin_modules = self._get_module_choices() for admin_module in self.admin_modules: all_permissions_granted = True partial_permissions_granted = False admin_module.required_permissions_fields = [] admin_module.per_view_permissions_fields = [] for required_permission in admin_module.get_required_permissions(): field_id = "perm:{}".format(required_permission) self.fields[field_id] = forms.BooleanField( required=False, label=required_permission, initial=(required_permission in initial_permissions)) admin_module.required_permissions_fields.append(field_id) if required_permission in initial_permissions: partial_permissions_granted = True else: all_permissions_granted = False extra_permissions = list( get_permissions_from_urls(admin_module.get_urls())) + list( admin_module.get_extra_permissions()) for permission in extra_permissions: field_id = "perm:{}".format(permission) self.fields[field_id] = forms.BooleanField( required=False, label=permission, initial=(permission in initial_permissions)) admin_module.per_view_permissions_fields.append(field_id) if permission in initial_permissions: partial_permissions_granted = True else: all_permissions_granted = False admin_module.all_permissions_granted = all_permissions_granted admin_module.partial_permissions_granted = False if all_permissions_granted else partial_permissions_granted
def formfield_callback(self, f, **kwargs): if str(f) == "shuup.CompanyContact.members": formfield = Select2MultipleField(model=PersonContact, required=False, help_text=f.help_text) if hasattr(self.instance, "members"): formfield.widget.choices = [ (object.pk, force_text(object)) for object in self.instance.members.all() ] return formfield return f.formfield(**kwargs)
def __init__(self, *args, **kwargs): super(APIPermissionGroupsForm, self).__init__(*args, **kwargs) initial_groups = [] if self.instance.pk and hasattr(self.instance, "groups"): initial_groups = self.instance.groups.all() self.fields["groups"] = Select2MultipleField( model=PermissionGroup, initial=[group.pk for group in initial_groups], label=_("Permission Groups")) self.fields["groups"].widget.choices = [(group.pk, force_text(group)) for group in initial_groups]
def get_admin_fields(self): active, ordering = super(AttributeProductListFilter, self).get_admin_fields() active[1].help_text = _( "Allow products to be filtered according to their attributes.") attributes = Select2MultipleField( model=Attribute, label=_("Attributes that can be filtered"), required=False, help_text=_( "Select attributes that can used for filtering the products."), ) return [active, (self.product_attr_key, attributes)]
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]
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)
def __init__(self, changing_user, *args, **kwargs): super(PermissionChangeFormBase, self).__init__(*args, **kwargs) self.changing_user = changing_user if 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") if "is_superuser" in self.fields: self.fields["is_superuser"].label = _( "Superuser (Full rights) status") self.fields["is_superuser"].help_text = _( "Designates whether this user has all permissions without explicitly " "assigning them. Assigning Granular Permission Groups to a Superuser " "will not have any effect because Granular Permission Groups are only " " able to give more rights, but Superuser already has them all." ) self.fields["is_staff"].label = _("Access to Admin Panel status") self.fields["is_staff"].help_text = _( "Designates whether this user can log into this admin site. Even " "Superusers should have this status enabled, otherwise they won't " "be able to access the Admin Panel.") permission_groups_field = Select2MultipleField( model=PermissionGroup, required=False, label=_("Granular Permission Groups"), help_text= _("Use Permission Groups to granularly give more permissions. User " "can belong to many groups and their permissions add and stack together. " "Search for `Permission Groups` to change these and add them to " "multiple users. Go to user account -> `Actions` -> `Edit Main " "Permissions` to add them to a specific user. Will not influence " "Superusers as they already have all the rights and can't be " "stripped of them without removing Superuser status first."), ) 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
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) shop = get_shop(self.request) self.fields["is_approved"] = forms.BooleanField( label=_("Is approved for {}").format(shop), required=False, initial=True, help_text=_( "Indicates whether this supplier is approved for the shop."), ) if self.instance.pk: supplier_shop = SupplierShop.objects.filter( shop=shop, supplier=self.instance).first() self.fields["is_approved"].initial = bool( supplier_shop and supplier_shop.is_approved) else: self.fields["is_approved"].initial = False 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
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 ]
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 ]
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
def __init__(self, *args, **kwargs): super(OrderLineReportForm, self).__init__(*args, **kwargs) supplier = Select2MultipleField( label=_("Suppliers"), model=Supplier, required=False, help_text=_("Filter order lines by suppliers.")) order_status = forms.ModelMultipleChoiceField( label=_("Order status"), required=False, queryset=OrderStatus.objects.all(), help_text=_("Filter order lines by status of their order.")) suppliers = self.get_initial_suppliers("supplier") if suppliers: supplier.initial = suppliers supplier.widget.choices = [(obj.pk, obj.name) for obj in suppliers] self.fields["supplier"] = supplier self.fields["order_status"] = order_status
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." )) initial_members = self.instance.staff_members.all( ) if self.instance.pk else [] staff_members = Select2MultipleField( label=_("Staff"), help_text=_("Select staff members for this shop."), model=get_user_model(), initial=initial_members, required=False) staff_members.widget.choices = [(member.pk, force_text(member)) for member in initial_members] self.fields["staff_members"] = staff_members self.disable_protected_fields()