refactored structure of XML-document

This commit is contained in:
Anatoly 2020-01-10 13:06:17 +03:00
parent 963e0597dc
commit 75e4077917

View File

@ -242,7 +242,7 @@ class SendExportBase:
def get_emails_to(self): def get_emails_to(self):
return [ return [
# 'a.feteleu@spider.ru', # 'a.feteleu@spider.ru',
'kuzmenko.da@gmail.com', # 'kuzmenko.da@gmail.com',
# 'sinapsit@yandex.ru' # 'sinapsit@yandex.ru'
] ]
@ -550,9 +550,7 @@ class SendGuideExport(SendExportBase):
if row.pop("node_name") == "WineNode": if row.pop("node_name") == "WineNode":
product = ET.SubElement(products, 'product') product = ET.SubElement(products, 'product')
for key, value in row.items(): for key, value in row.items():
prop = ET.SubElement(product, 'prop') product.set(key, str(value)) if value else None
prop.set('name', key)
prop.text = str(value)
# create a new XML file with the results # create a new XML file with the results
tree = ET.ElementTree(data) tree = ET.ElementTree(data)
@ -562,9 +560,7 @@ class SendGuideExport(SendExportBase):
elif self.guide.guide_type in [self.guide.ARTISAN, self.guide.RESTAURANT]: elif self.guide.guide_type in [self.guide.ARTISAN, self.guide.RESTAURANT]:
# establishment # establishment
# create the file structure objects = []
data = ET.Element('data')
items = ET.SubElement(data, 'cities')
city = None city = None
ad_number_of_pages = None ad_number_of_pages = None
ad_right_pages = None ad_right_pages = None
@ -578,18 +574,29 @@ class SendGuideExport(SendExportBase):
row['ad_number_of_pages'] = ad_number_of_pages row['ad_number_of_pages'] = ad_number_of_pages
row['ad_right_pages'] = ad_right_pages row['ad_right_pages'] = ad_right_pages
row_city = row.get('city_name') row_city = row.pop('city_name')
if row_city: if row_city:
city = row_city city = row_city
else: objects.append({'city': city, 'establishments': []})
row['city_name'] = city
if row.pop("node_name") == "EstablishmentNode": if row.pop("node_name") == "EstablishmentNode":
item = ET.SubElement(items, 'city') city_index = [i for i, obj in enumerate(objects)
for key, value in row.items(): if obj['city'] == city][0]
prop = ET.SubElement(item, 'prop') establishments = objects[city_index].get('establishments')
prop.set('name', key) establishments.append(row)
prop.text = str(value)
# 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 # create a new XML file with the results
tree = ET.ElementTree(data) tree = ET.ElementTree(data)