From 75e40779170231a02e0ca7e3d76bf036e9147911 Mon Sep 17 00:00:00 2001 From: Anatoly Date: Fri, 10 Jan 2020 13:06:17 +0300 Subject: [PATCH] refactored structure of XML-document --- apps/utils/export.py | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/apps/utils/export.py b/apps/utils/export.py index a4dda8c6..70a10b58 100644 --- a/apps/utils/export.py +++ b/apps/utils/export.py @@ -242,7 +242,7 @@ class SendExportBase: def get_emails_to(self): return [ # 'a.feteleu@spider.ru', - 'kuzmenko.da@gmail.com', + # 'kuzmenko.da@gmail.com', # 'sinapsit@yandex.ru' ] @@ -550,9 +550,7 @@ class SendGuideExport(SendExportBase): if row.pop("node_name") == "WineNode": product = ET.SubElement(products, 'product') for key, value in row.items(): - prop = ET.SubElement(product, 'prop') - prop.set('name', key) - prop.text = str(value) + product.set(key, str(value)) if value else None # create a new XML file with the results tree = ET.ElementTree(data) @@ -562,9 +560,7 @@ class SendGuideExport(SendExportBase): elif self.guide.guide_type in [self.guide.ARTISAN, self.guide.RESTAURANT]: # establishment - # create the file structure - data = ET.Element('data') - items = ET.SubElement(data, 'cities') + objects = [] city = None ad_number_of_pages = None ad_right_pages = None @@ -578,18 +574,29 @@ class SendGuideExport(SendExportBase): row['ad_number_of_pages'] = ad_number_of_pages row['ad_right_pages'] = ad_right_pages - row_city = row.get('city_name') + row_city = row.pop('city_name') if row_city: city = row_city - else: - row['city_name'] = city + objects.append({'city': city, 'establishments': []}) if row.pop("node_name") == "EstablishmentNode": - item = ET.SubElement(items, 'city') - for key, value in row.items(): - prop = ET.SubElement(item, 'prop') - prop.set('name', key) - prop.text = str(value) + city_index = [i for i, obj in enumerate(objects) + if obj['city'] == city][0] + establishments = objects[city_index].get('establishments') + establishments.append(row) + + # create xml-structure + data = ET.Element('data') + cities_element = ET.SubElement(data, 'cities') + + for city in objects: + city_element = ET.SubElement(cities_element, 'city', attrib={'name': city.get('city')}) + + establishments_element = ET.SubElement(city_element, 'establishments') + for establishment in city.get('establishments'): + establishment_element = ET.SubElement(establishments_element, 'establishment') + for key, value in establishment.items(): + establishment_element.set(key, str(value)) if value else None # create a new XML file with the results tree = ET.ElementTree(data)