From 8452d76fff27bf30962f309062f7fefdd97fb5d6 Mon Sep 17 00:00:00 2001 From: Artemiy Date: Sat, 15 Sep 2018 00:34:28 +0300 Subject: [PATCH] #565 add vendor to yml (#571) * Put brands to price files * Create test for prices' vendor fields * Review fixes --- shopelectro/management/commands/price.py | 25 +++++++++++++++--------- shopelectro/tests/tests_commands.py | 10 ++++++++++ templates/prices/price.yml | 5 +++-- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/shopelectro/management/commands/price.py b/shopelectro/management/commands/price.py index df580e8b..4b2c0bce 100644 --- a/shopelectro/management/commands/price.py +++ b/shopelectro/management/commands/price.py @@ -7,7 +7,7 @@ from django.template.loader import render_to_string from django.urls import reverse -from shopelectro.models import Product, Category +from shopelectro.models import Product, Category, Tag class PriceFilter: @@ -75,12 +75,13 @@ def put_utm(product): utm_mark_query = '&'.join('{}={}'.format(k, v) for k, v in utm_marks) product.utm_url = '{}{}?{}'.format(settings.BASE_URL, url, utm_mark_query) - product.prepared_params = list( - filter( + product.prepared_params = [ + (group, tags[0].name) + for (group, tags) in filter( lambda x: x[0].name != 'Производитель', product.get_params() - ) - ) + ) if tags + ] return product @@ -91,6 +92,10 @@ def put_crumbs(product): # Ignore PyDocStyleBear ) return product + def put_brand(product, brands): + product.brand = brands.get(product) + return product + def filter_categories(utm): categories_to_exclude = ( Category.objects @@ -115,11 +120,13 @@ def filter_categories(utm): def prepare_products(categories_, utm): """Filter product list and patch it for rendering.""" + products = PriceFilter(utm).run( + Product.actives.filter_by_categories(categories_) + ) + brands = Tag.objects.get_brands(products) return [ - put_crumbs(put_utm(product)) - for product in PriceFilter(utm).run( - Product.actives.filter_by_categories(categories_) - ) + put_brand(put_crumbs(put_utm(product)), brands) + for product in products ] categories = ( diff --git a/shopelectro/tests/tests_commands.py b/shopelectro/tests/tests_commands.py index c769b939..ebc9dfef 100644 --- a/shopelectro/tests/tests_commands.py +++ b/shopelectro/tests/tests_commands.py @@ -252,3 +252,13 @@ def test_products_in_yandex_price(self): len(products_in_yandex_price), Product.objects.filter(page__images__isnull=False).distinct().count() ) + + def test_brands(self): + """Price contains brand data.""" + for price in settings.UTM_PRICE_MAP.values(): + offer = self.get_price_offers_node(price).find('offer') + product = Product.objects.filter(id=offer.get('id')).first() + self.assertEqual( + product.get_brand_name(), + offer.find(f'vendor').text, + ) diff --git a/templates/prices/price.yml b/templates/prices/price.yml index 1e79788f..46c4416d 100644 --- a/templates/prices/price.yml +++ b/templates/prices/price.yml @@ -44,10 +44,11 @@ {% if not utm == 'GM' %} При заказе от {{ shop.local_delivery_cost_threshold }} руб. доставка по СПб бесплатно {% endif %} + {% if product.brand %}{{ product.brand.name }}{% endif %} {# product_type tag in google merchant doc : https://goo.gl/b0UJQp #} {% if utm == 'GM' %}{{ product.crumbs }}{% endif %} - {% for name, values in product.get_params %} - {{ values|first }} + {% for name, value in product.prepared_params %} + {{ value }} {% endfor %} {% endfor %}