Skip to content

Commit

Permalink
#565 add vendor to yml (#571)
Browse files Browse the repository at this point in the history
* Put brands to price files

* Create test for prices' vendor fields

* Review fixes
  • Loading branch information
ArtemijRodionov authored Sep 14, 2018
1 parent b347fd9 commit 8452d76
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 deletions.
25 changes: 16 additions & 9 deletions shopelectro/management/commands/price.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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 = (
Expand Down
10 changes: 10 additions & 0 deletions shopelectro/tests/tests_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
5 changes: 3 additions & 2 deletions templates/prices/price.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@
{% if not utm == 'GM' %}
<sales_notes>При заказе от {{ shop.local_delivery_cost_threshold }} руб. доставка по СПб бесплатно</sales_notes>
{% endif %}
{% if product.brand %}<vendor>{{ product.brand.name }}</vendor>{% endif %}
{# product_type tag in google merchant doc : https://goo.gl/b0UJQp #}
{% if utm == 'GM' %}<product_type>{{ product.crumbs }}</product_type>{% endif %}
{% for name, values in product.get_params %}
<param name="{{ name }}">{{ values|first }}</param>
{% for name, value in product.prepared_params %}
<param name="{{ name }}">{{ value }}</param>
{% endfor %}
</offer>
{% endfor %}
Expand Down

0 comments on commit 8452d76

Please sign in to comment.