Repository: fidals/shopelectro

Issue:
Change price label for packs
Если у товара есть свойство "Упаковка" и значение больше 1, то  рядом с ценой пишем не хх руб / шт ,а хх руб / упаковка


Discussion:

Resolving patch:
diff --git a/shopelectro/admin.py b/shopelectro/admin.py
--- a/shopelectro/admin.py
+++ b/shopelectro/admin.py
@@ -113,7 +113,7 @@ class ProductInline(inlines.ProductInline):
         'fields': (
             ('name', 'id', 'vendor_code'),
             ('category', 'correct_category_id'),
-            ('price', 'in_stock'),
+            ('price', 'in_stock', 'in_pack'),
             'is_popular',
             ('purchase_price', 'wholesale_small'),
             ('wholesale_medium', 'wholesale_large'),
@@ -130,7 +130,7 @@ def formfield_for_dbfield(self, db_field, **kwargs):
                     'pk__startswith',
                 ],
             )
-        return super(ProductInline, self).formfield_for_dbfield(
+        return super().formfield_for_dbfield(
             db_field,
             **kwargs,
         )
diff --git a/shopelectro/templatetags/se_extras.py b/shopelectro/templatetags/se_extras.py
--- a/shopelectro/templatetags/se_extras.py
+++ b/shopelectro/templatetags/se_extras.py
@@ -85,6 +85,13 @@ def humanize_price(price):
     return intcomma(floatformat(price, 0))
 
 
+@register.filter
+def show_price_in_units(item):
+    if (getattr(item, 'in_pack', 1) > 1):
+        return 'руб / упаковка'
+    return 'руб / шт'
+
+
 # Not good code, but duker at 06/10/2016 don't know how to fix it.
 # It makes Image model very complex.
 @register.simple_tag


Test patch:
diff --git a/shopelectro/tests/tests_views.py b/shopelectro/tests/tests_views.py
--- a/shopelectro/tests/tests_views.py
+++ b/shopelectro/tests/tests_views.py
@@ -5,6 +5,7 @@
 They all should be using Django's TestClient.
 """
 import json
+import re
 from functools import partial
 from itertools import chain
 from operator import attrgetter
@@ -37,13 +38,14 @@ def json_to_dict(response: HttpResponse) -> dict:
     return json.loads(response.content)
 
 
-class BaseCatalogTestCase(TestCase):
+class ViewsTestCase(TestCase):
 
     fixtures = ['dump.json']
 
     def setUp(self):
         self.category = models.Category.objects.root_nodes().select_related('page').first()
         self.tags = models.Tag.objects.order_by_alphanumeric().all()
+        self.product = models.Product.objects.first()
 
     def get_category_url(
         self,
@@ -70,16 +72,26 @@ def get_category_page(self, *args, **kwargs):
         """See `self.get_category_url()` interface."""
         return self.client.get(self.get_category_url(*args, **kwargs))
 
-    def get_category_soup(self, *args, **kwargs) -> BeautifulSoup:
-        category_page = self.get_category_page(*args, **kwargs)
+    def get_product_page(self, product: models.Product=None):
+        product = product or self.product
+        return self.client.get(product.url)
+
+    def get_category_soup(self, *args, **kwargs):
+        return BeautifulSoup(
+            self.get_category_page(*args, **kwargs).content.decode('utf-8'),
+            'html.parser'
+        )
+
+    def get_product_soup(self, product: models.Product=None) -> BeautifulSoup:
+        product_page = self.get_product_page(product)
         return BeautifulSoup(
-            category_page.content.decode('utf-8'),
+            product_page.content.decode('utf-8'),
             'html.parser'
         )
 
 
 @tag('fast', 'catalog')
-class CatalogTags(BaseCatalogTestCase):
+class CatalogTags(ViewsTestCase):
 
     def test_category_page_contains_all_tags(self):
         """Category contains all Product's tags."""
@@ -198,7 +210,7 @@ def test_category_tag_relation(self):
 
 
 @tag('fast', 'catalog')
-class CatalogPagination(BaseCatalogTestCase):
+class CatalogPagination(ViewsTestCase):
 
     def get_pagination_links_soup(self, page_number: int):
         return (
@@ -274,7 +286,7 @@ def test_pagination_canonical(self):
 
 
 @tag('fast')
-class LoadMore(BaseCatalogTestCase):
+class LoadMore(ViewsTestCase):
 
     fixtures = ['dump.json']
     DEFAULT_LIMIT = 48
@@ -464,7 +476,7 @@ def test_yandex_aviso_body(self):
 
 
 @tag('fast', 'catalog')
-class Category(BaseCatalogTestCase):
+class Category(ViewsTestCase):
 
     fixtures = ['dump.json']
 
@@ -549,7 +561,7 @@ def test_crumb_siblings_are_active(self):
 
 
 @tag('fast', 'catalog')
-class CategoriesMatrix(BaseCatalogTestCase):
+class CategoriesMatrix(ViewsTestCase):
 
     fixtures = ['dump.json']
 
@@ -628,24 +640,10 @@ def test_product_tile(self):
 
 
 @tag('fast')
-class ProductPage(TestCase):
+class ProductPage(ViewsTestCase):
 
     fixtures = ['dump.json']
 
-    def setUp(self):
-        self.product = models.Product.objects.first()
-
-    def get_product_page(self, product: models.Product=None):
-        product = product or self.product
-        return self.client.get(product.url)
-
-    def get_product_soup(self, product: models.Product=None) -> BeautifulSoup:
-        product_page = self.get_product_page(product)
-        return BeautifulSoup(
-            product_page.content.decode('utf-8'),
-            'html.parser'
-        )
-
     def test_orphan_product(self):
         self.product.category = None
         self.product.save()
@@ -872,3 +870,35 @@ def test_disabled_cache(self):
             self.client.get(reverse('cart_get'))['Cache-Control'],
             'max-age=0, no-cache, no-store, must-revalidate',
         )
+
+
+@tag('fast', 'catalog')
+class InPack(ViewsTestCase):
+
+    fixtures = ['dump.json']
+
+    def pack_product(self):
+        product = models.Product.objects.first()
+        product.in_pack = 2
+        product.save()
+        return product
+
+    def assert_in_pack_units(self, soup, product):
+        results = soup.find_all(string=re.compile('упаковка'))
+
+        self.assertEqual(len(results), 1, results)
+        self.assertIn(str(int(product.price)), results[0], results[0])
+
+    def test_catalog_in_pack_units(self):
+        product = self.pack_product()
+        self.assert_in_pack_units(
+            self.get_category_soup(product.category),
+            product,
+        )
+
+    def test_product_in_pack_units(self):
+        product = self.pack_product()
+        self.assert_in_pack_units(
+            self.get_product_soup(product),
+            product,
+        )