コード例 #1
0
ファイル: base_forms.py プロジェクト: zaster/shuup
    def __init__(self, **kwargs):
        # TODO: Revise this. Since this is shop product form then maybe we should have shop available insted of request
        self.request = kwargs.pop("request", None)
        super(ShopProductForm, self).__init__(**kwargs)
        payment_methods_qs = PaymentMethod.objects.all()
        shipping_methods_qs = ShippingMethod.objects.all()
        if self.request:
            shop = self.request.shop
            payment_methods_qs = payment_methods_qs.filter(shop=shop)
            shipping_methods_qs = ShippingMethod.objects.filter(shop=shop)
        self.fields["payment_methods"].queryset = payment_methods_qs
        self.fields["shipping_methods"].queryset = shipping_methods_qs
        self.fields["default_price_value"].required = True

        initial_categories = []
        initial_suppliers = []

        if self.instance.pk:
            initial_categories = self.instance.categories.all()
            initial_suppliers = self.instance.suppliers.all()
        elif not settings.SHUUP_ENABLE_MULTIPLE_SUPPLIERS:
            supplier = Supplier.objects.first()
            initial_suppliers = ([supplier] if supplier else [])

        if settings.SHUUP_ADMIN_LOAD_SELECT_OBJECTS_ASYNC.get("suppliers"):
            self.fields["suppliers"] = Select2ModelMultipleField(
                initial=initial_suppliers,
                model=Supplier,
                widget=QuickAddSupplierMultiSelect(
                    initial=initial_suppliers,
                    attrs={"data-search-mode": "enabled"}),
                required=False)
        else:
            self.fields["suppliers"].widget = QuickAddSupplierMultiSelect(
                initial=initial_suppliers)

        if settings.SHUUP_ADMIN_LOAD_SELECT_OBJECTS_ASYNC.get("categories"):
            self.fields["primary_category"] = Select2ModelField(
                initial=(self.instance.primary_category
                         if self.instance.pk else None),
                model=Category,
                widget=QuickAddCategorySelect(
                    editable_model="shuup.Category",
                    initial=(self.instance.primary_category
                             if self.instance.pk else None),
                    attrs={"data-placeholder": ugettext("Select a category")}),
                required=False)
            self.fields["categories"] = Select2ModelMultipleField(
                initial=initial_categories,
                model=Category,
                widget=QuickAddCategoryMultiSelect(initial=initial_categories),
                required=False)
        else:
            categories_choices = [
                (cat.pk, cat.get_hierarchy())
                for cat in Category.objects.all_except_deleted(
                    shop=get_shop(self.request))
            ]
            self.fields["primary_category"].widget = QuickAddCategorySelect(
                initial=(self.instance.primary_category if self.instance.pk
                         and self.instance.primary_category else None),
                editable_model="shuup.Category",
                attrs={"data-placeholder": ugettext("Select a category")},
                choices=categories_choices,
                model=NoModel())
            self.fields["categories"].widget = QuickAddCategoryMultiSelect(
                initial=initial_categories,
                choices=categories_choices,
                model=NoModel())