|
@@ -0,0 +1,46 @@
|
|
|
+
|
|
|
+
|
|
|
+import BeautifulSoup
|
|
|
+import datetime
|
|
|
+import dingguo
|
|
|
+import email
|
|
|
+import re
|
|
|
+
|
|
|
+def parse_order_confirmation_mail(mail):
|
|
|
+
|
|
|
+ assert isinstance(mail, email.message.Message)
|
|
|
+
|
|
|
+ doc = BeautifulSoup.BeautifulSoup(
|
|
|
+ mail.get_payload()[1].get_payload(decode = True).decode('utf-8')
|
|
|
+ )
|
|
|
+
|
|
|
+ if doc.find(text = re.compile(ur'Thomann')) is None:
|
|
|
+ raise Exception('no thomann order confirmation')
|
|
|
+
|
|
|
+ order = dingguo.Order(
|
|
|
+ customer_id = doc.find(text = re.compile(ur'Kundennummer')).parent.nextSibling.text,
|
|
|
+ order_id = doc.find(text = re.compile(ur'Auftragsnummer')).parent.nextSibling.text,
|
|
|
+ order_date = datetime.datetime.strptime(
|
|
|
+ doc.find(text = re.compile(ur'Auftragsdatum')).parent.nextSibling.text,
|
|
|
+ '%d.%m.%Y %H:%M',
|
|
|
+ ).date(),
|
|
|
+ platform = u'thomann',
|
|
|
+ )
|
|
|
+
|
|
|
+ for product_id_label_tag in doc.findAll(text = re.compile(ur'Artikelnummer')):
|
|
|
+ article_tag = product_id_label_tag.findParents('td')[1]
|
|
|
+ name_quantity_tag = article_tag.find('strong')
|
|
|
+ quantity, name = re.match(ur'(\d)x (.*)', name_quantity_tag.text).groups()
|
|
|
+ currency, price = re.match(
|
|
|
+ ur'(.) (\d+,\d+)',
|
|
|
+ product_id_label_tag.parent.nextSibling.text,
|
|
|
+ ).groups()
|
|
|
+ order.items.append(dingguo.Article(
|
|
|
+ features = name_quantity_tag.parent.nextSibling.text,
|
|
|
+ name = name,
|
|
|
+ price_brutto = dingguo.Sum(float(price.replace(',', '.')), currency),
|
|
|
+ product_id = product_id_label_tag.nextSibling.text,
|
|
|
+ quantity = int(quantity),
|
|
|
+ ))
|
|
|
+
|
|
|
+ return [order]
|