From c76e6fbc128d54e4c24d94552dad478ee9157fcc Mon Sep 17 00:00:00 2001 From: mtyton Date: Mon, 10 Jul 2023 22:36:11 +0200 Subject: [PATCH] configurator is almost done --- artel/store/forms.py | 8 ++- artel/store/models.py | 17 ++++++ .../templates/store/configure_product.html | 53 ++++++++----------- .../store/configure_product_summary.html | 0 .../templates/store/product_list_page.html | 20 ------- artel/store/urls.py | 1 + artel/store/views.py | 16 ++++++ 7 files changed, 63 insertions(+), 52 deletions(-) create mode 100644 artel/store/templates/store/configure_product_summary.html diff --git a/artel/store/forms.py b/artel/store/forms.py index 08e8b09..8bc7673 100644 --- a/artel/store/forms.py +++ b/artel/store/forms.py @@ -43,7 +43,7 @@ class CustomerDataForm(forms.Form): class ProductCategoryParamForm(forms.ModelForm): class Meta: model = ProductCategoryParam - fields = ("key", "value", ) + fields = ("key", "value") readonly_fields = ("key", ) def __init__(self, instance, *args, **kwargs): @@ -57,3 +57,9 @@ class ProductCategoryParamForm(forms.ModelForm): queryset=ProductCategoryParamValue.objects.none(), widget=forms.RadioSelect(attrs={"class": "form-control"}) ) + + def save(self, *args, **kwargs): + return ProductCategoryParamValue.objects.get( + param=self.instance, + value=str(self.cleaned_data["value"]) + ) diff --git a/artel/store/models.py b/artel/store/models.py index a605055..30d37bc 100644 --- a/artel/store/models.py +++ b/artel/store/models.py @@ -168,6 +168,23 @@ class ProductTemplateImage(BaseImageModel): is_main = models.BooleanField(default=False) +class ProductManager(models.Manager): + + def get_or_create_by_params(self, params: list[ProductCategoryParamValue], template: ProductTemplate): + product = self.filter(params__in=params).first() + if not product: + product = self.create( + name=f"{template.title} - AUTOGENERATED", + template=template, + price=..., + available=False + ) + for param in params: + product.params.add(param) + + return product + + class Product(ClusterableModel): name = models.CharField(max_length=255, blank=True) template = models.ForeignKey(ProductTemplate, on_delete=models.CASCADE, related_name="products") diff --git a/artel/store/templates/store/configure_product.html b/artel/store/templates/store/configure_product.html index 8e8f773..05666ff 100644 --- a/artel/store/templates/store/configure_product.html +++ b/artel/store/templates/store/configure_product.html @@ -1,16 +1,12 @@ {% extends 'base.html' %} {% block content %} -

{{template.title}}

- - -
@@ -24,36 +20,31 @@
-
- {% csrf_token %} - {% for form in forms %} -
-

- {{form.instance}} -

- -
- {% for value, label in form.value.field.choices %} -
- - +
+ + {% csrf_token %} +
+ {% for form in forms %} +
+

{{form.instance}}

+ {% for value, label in form.value.field.choices %} +
+ + +
+ {% endfor %}
- {% endfor %} -
-
- {% endfor %} -
-
-
- -
-
- + {% if forloop.counter|divisibleby:"2" %}
{% endif %} + {% endfor %} +
+
+
+
+
-
- + +
diff --git a/artel/store/templates/store/configure_product_summary.html b/artel/store/templates/store/configure_product_summary.html new file mode 100644 index 0000000..e69de29 diff --git a/artel/store/templates/store/product_list_page.html b/artel/store/templates/store/product_list_page.html index 778471c..3c09650 100644 --- a/artel/store/templates/store/product_list_page.html +++ b/artel/store/templates/store/product_list_page.html @@ -3,26 +3,6 @@ {% block content %} - -
{% for item in items %} diff --git a/artel/store/urls.py b/artel/store/urls.py index 103e01b..d49b1c7 100644 --- a/artel/store/urls.py +++ b/artel/store/urls.py @@ -10,6 +10,7 @@ router.register("cart-action", store_views.CartActionView, "cart-action") urlpatterns = [ path('product-configure//', store_views.ConfigureProductView.as_view(), name='product-configure'), + path('product-configure/summary//', store_views.ConfigureProductSummaryView.as_view(), name='product-configure-summary'), path('cart/', store_views.CartView.as_view(), name='cart'), path("order/", store_views.OrderView.as_view(), name="order"), path("order/confirm/", store_views.OrderConfirmView.as_view(), name="order-confirm") diff --git a/artel/store/views.py b/artel/store/views.py index 25537b8..c06db5c 100644 --- a/artel/store/views.py +++ b/artel/store/views.py @@ -111,6 +111,22 @@ class ConfigureProductView(View): context = self.get_context_data(pk) return render(request, self.template_name, context) + def post(self, request, pk: int, *args, **kwargs): + print(request.POST) + return HttpResponseRedirect(reverse("product-configure-summary", kwargs={"variant_pk": 1})) + + +class ConfigureProductSummaryView(View): + template_name = "store/configure_product_summary.html" + + def get(self, request, variant_pk: int, *args, **kwargs): + variant = Product.objects.get(pk=variant_pk) + context = { + "variant": variant, + "category_params": variant.template.category.category_params.all() + } + return render(request, self.template_name, context) + class OrderView(View): template_name = "store/order.html"