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 %} -