- {% for product in template.products.all %}
-
diff --git a/artel/store/tests/test_models.py b/artel/store/tests/test_models.py
index fbdc288..3b07e79 100644
--- a/artel/store/tests/test_models.py
+++ b/artel/store/tests/test_models.py
@@ -4,12 +4,42 @@ from django.test import TestCase
from django.urls import reverse
from django.core import mail
from django.core.exceptions import ValidationError
+from django.db import transaction
from store.tests import factories
from store import models as store_models
from mailings.tests.factories import MailTemplateFactory
+class ProductCategoryParamTestCase(TestCase):
+ def setUp(self):
+ super().setUp()
+ self.category = factories.ProductCategoryFactory()
+ self.param = factories.ProductCategoryParamFactory(
+ category=self.category,
+ param_type="int",
+ key="test_param"
+ )
+
+ def test_get_available_values_no_values_success(self):
+ available_values = [v for v in self.param.get_available_values()]
+ self.assertEqual(available_values, [])
+
+ def test_get_available_values_one_value_success(self):
+ factories.ProductCategoryParamValueFactory(param=self.param, value="23")
+ available_values = [v for v in self.param.get_available_values()]
+ self.assertEqual(available_values, [23])
+ self.assertEqual(len(available_values), 1)
+
+ def test_get_available_values_multiple_values_success(self):
+ factories.ProductCategoryParamValueFactory(param=self.param, value="23")
+ factories.ProductCategoryParamValueFactory(param=self.param, value="24")
+ factories.ProductCategoryParamValueFactory(param=self.param, value="25")
+ available_values = [v for v in self.param.get_available_values()]
+ self.assertEqual(available_values, [23, 24, 25])
+ self.assertEqual(len(available_values), 3)
+
+
class ProductCategoryParamValueTestCase(TestCase):
def setUp(self):
super().setUp()
@@ -47,7 +77,8 @@ class ProductTestCase(TestCase):
key="test_param"
)
param_value = factories.ProductCategoryParamValueFactory(param=param, value="23")
- product.params.add(param_value)
+ with transaction.atomic():
+ product.params.add(param_value)
product.save()
self.assertEqual(product.params.count(), 1)
self.assertEqual(product.params.first().get_value(), 23)
@@ -62,8 +93,10 @@ class ProductTestCase(TestCase):
param_value = factories.ProductCategoryParamValueFactory(param=param, value="23")
sec_param_value = factories.ProductCategoryParamValueFactory(param=param, value="24")
with self.assertRaises(ValidationError):
- product.params.add(param_value)
- product.params.add(sec_param_value)
+ with transaction.atomic():
+ product.params.add(param_value)
+ product.params.add(sec_param_value)
+ self.assertEqual(product.params.count(), 0)
class OrderProductTestCase(TestCase):
diff --git a/artel/store/tests/test_validators.py b/artel/store/tests/test_validators.py
deleted file mode 100644
index 3a68554..0000000
--- a/artel/store/tests/test_validators.py
+++ /dev/null
@@ -1,42 +0,0 @@
-from django.test import TestCase
-from django.core.exceptions import ValidationError
-
-from store.tests import factories
-from store.validators import ProductParamDuplicateValidator
-from store.models import (
- ProductParam,
- Product
-)
-
-
-class CategoryParamValidationTestCase(TestCase):
-
- def setUp(self):
- super().setUp()
- self.product = factories.ProductFactory()
-
- def test_set_single_param_success(self):
- param = factories.ProductCategoryParamFactory(
- category=self.product.template.category,
- param_type="int",
- key="test_param"
- )
- param_value = factories.ProductCategoryParamValueFactory(param=param, value="23")
- param = ProductParam(param_value=param_value)
- self.product.params.add(param_value)
- self.assertEqual(self.product.params.count(), 1)
- self.assertEqual(self.product.params.first().get_value(), 23)
-
- def test_set_multuiple_same_type_param_failure(self):
- param = factories.ProductCategoryParamFactory(
- category=self.product.template.category,
- param_type="int",
- key="test_param"
- )
- param_value = factories.ProductCategoryParamValueFactory(param=param, value="23")
- sec_param_value = factories.ProductCategoryParamValueFactory(param=param, value="24")
- param = ProductParam(param_value=param_value)
- self.product.params.add(param_value)
- with self.assertRaises(ValidationError):
- self.product.params.add(sec_param_value)
-
diff --git a/artel/store/views.py b/artel/store/views.py
index 71068d1..25537b8 100644
--- a/artel/store/views.py
+++ b/artel/store/views.py
@@ -17,7 +17,10 @@ from store.serializers import (
CartSerializer,
CartProductAddSerializer
)
-from store.forms import CustomerDataForm
+from store.forms import (
+ CustomerDataForm,
+ ProductCategoryParamForm
+)
from store.models import (
Order,
Product,
@@ -91,8 +94,17 @@ class ConfigureProductView(View):
template_name = "store/configure_product.html"
def get_context_data(self, pk: int, **kwargs: Any) -> Dict[str, Any]:
- context = {}
- context["template"] = ProductTemplate.objects.get(pk=pk)
+ template = ProductTemplate.objects.get(pk=pk)
+ category_params = template.category.category_params.all()
+
+
+ context = {
+ "template": template,
+ "available_variants": Product.objects.filter(template__pk=pk),
+ "category_params": category_params,
+ "forms": [ProductCategoryParamForm(instance=param) for param in category_params]
+ }
+
return context
def get(self, request, pk: int, *args, **kwargs):