Skip to content

Commit

Permalink
#1004 RSS price for Google Merchant (#1005)
Browse files Browse the repository at this point in the history
* #1004  Draft for GM rss price

* #1004  Cleanup rss price substasks

* #1004  Test RSS price
  • Loading branch information
duker33 authored Oct 31, 2019
1 parent b7efeaf commit 235fbcc
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 14 deletions.
21 changes: 14 additions & 7 deletions shopelectro/management/commands/price.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@

# --- files processing ---
class File:
def __init__(self, path: str, context: dict):
def __init__(self, path: str, context: dict, template_path: str):
self.path = path
self.context = context
self.template_path = template_path

def create(self):
with open(self.path, 'w', encoding='utf-8') as file:
file.write(render_to_string('prices/price.yml', self.context).strip())
file.write(render_to_string(self.template_path, self.context).strip())
logger.info(f'{self.path} generated.')


Expand Down Expand Up @@ -208,9 +209,15 @@ class Command(BaseCommand):
BASE_DIR = settings.ASSETS_DIR

def handle(self, *args, **options):
Files(
[File(
Files([
*[File(
path=os.path.join(self.BASE_DIR, filename),
context=Context(target).context()
) for target, filename in settings.UTM_PRICE_MAP.items()]
).create()
context=Context(target).context(),
template_path='prices/price.yml',
) for target, filename in settings.UTM_PRICE_MAP.items()],
File(
path=os.path.join(self.BASE_DIR, 'gm.rss'),
context=Context('GM').context(),
template_path='prices/price.rss',
)
]).create()
21 changes: 18 additions & 3 deletions shopelectro/tests/tests_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from django.test import TestCase, override_settings, tag

from shopelectro.exception import UpdateCatalogException
from shopelectro.management.commands import price
from shopelectro.management.commands._update_catalog import (
update_products, update_tags, update_pack,
)
Expand Down Expand Up @@ -269,7 +270,7 @@ def remove(self):


@tag('fast')
class GeneratePrices(TestCase):
class PricesTest(TestCase):

fixtures = ['dump.json']
CATEGORY_TO_EXCLUDE = 'Category #1 of #Category #0 of #Category #1'
Expand All @@ -288,13 +289,13 @@ class GeneratePrices(TestCase):
def setUpTestData(cls):
with cls.ignore_categories, cls.ignore_products:
call_command('price')
super(GeneratePrices, cls).setUpTestData()
super().setUpTestData()
cls.prices = Prices(settings.UTM_PRICE_MAP.keys())

@classmethod
def tearDownClass(cls):
cls.prices.remove()
super(GeneratePrices, cls).tearDownClass()
super().tearDownClass()

def test_prices_exists(self):
"""Price command should generate various price-list files."""
Expand All @@ -309,6 +310,20 @@ def test_categories_in_price(self):
categories_in_price = self.prices['priceru'].categories_node
self.assertEqual(len(categories_in_price), Category.objects.count())

# @todo #1004:30m Organize yml and rss prices testing in the right way.
# Now rss price has no clear class separation, like yml prices have.
# Prices filename is hardcoded.
# Depends from #714.
@ignore_categories
@ignore_products
def test_items_in_price_rss(self):
filename = 'gm.rss'
file_path = os.path.join(settings.ASSETS_DIR, filename)
root_node = ElementTree.parse(file_path)
items: list = root_node.getroot().find('channel').findall('item')
db_count = len(price.Context('GM').context()['products'])
self.assertEqual(len(items), db_count)

def test_categories_in_yandex_price(self):
categories = self.prices['YM'].categories_node
self.assertEqual(
Expand Down
40 changes: 40 additions & 0 deletions templates/prices/price.rss
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{% load se_extras %}

<?xml version="1.0"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
{% comment %}
@todo #1004:30m Avoid hardcoded values in RSS template.
Take values like site's title and description from DB.
{% endcomment %}
<title>Интернет магазин Элементов питания с доставкой по России</title>
<link>{{ base_url }}</link>
<description>Элементы питания в СПб с доставкой по России - интернет магазин элементов питания ShopElectro</description>
{% for product in products %}
<item>
<title>{{ product.name }}</title>
<link>{{ product.utm_url }}</link>
<description>{{ product.page.display.description }}</description>
<g:id>{{ product.vendor_code }}</g:id>
<g:condition>new</g:condition>
<g:price>{{ product.price }}</g:price>
<g:availability>{{ product.in_stock|yesno:'true,false' }}</g:availability>
<g:image_link>http://images.example.com/TV_123456.png</g:image_link>
<g:brand>{{ product.brand.name }}</g:brand>
<g:product_type>{{ product.crumbs }}</g:product_type>

{% comment %}
@todo #1004:30m Implement specific to GM set of RSS labels.
See the main set of them below.
And take to account values from ahead code:
"g:condition" and "g:availability".
{% endcomment %}
<g:google_product_category>Электроника &amp;gt; Телевизоры &amp;gt; Жидкокристаллические панели и LCD
</g:google_product_category>
<g:mpn>LCC3525G</g:mpn>
<g:custom_label_0>high_margin</g:custom_label_0>
<g:custom_label_1>Msk</g:custom_label_1>
</item>
{% endfor %}
</channel>
</rss>
8 changes: 4 additions & 4 deletions templates/prices/price.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% load se_extras %}

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE yml_catalog SYSTEM "shops.dtd">
<yml_catalog date="{% now "Y-m-d H:i" %}">
Expand Down Expand Up @@ -35,12 +36,11 @@
<delivery>true</delivery>
{% if product.price > shop.local_delivery_cost %}<local_delivery_cost>0</local_delivery_cost>{% endif %}
<name>{{ product.name }}</name>
{% comment "Description in an issue #257" %}
<description>
{% if product.page.display.description and not utm == 'GM' %}
<description>{{ product.page.display.description }}</description>
{{ product.page.display.description }}
{% endif %}
{% endcomment %}
<description></description>
</description>
{% if not utm == 'GM' and not utm == 'YM' %}
<sales_notes>При заказе от {{ shop.local_delivery_cost_threshold }} руб. доставка по СПб бесплатно</sales_notes>
{% endif %}
Expand Down

0 comments on commit 235fbcc

Please sign in to comment.