def post(self): # TODO Implement validation try: dict_list = multidict_to_list(self.request.params) except MultidictError as e: raise HTTPBadRequest(e.message) counts = {'product': 0, 'category': 0, 'package': 0} new_report_keys = list() error_msgs = list() for dict_ in dict_list: try: report, new_items = PriceReport.assemble( storage_manager=self.root, **dict_) new_report_keys.append(report.key) prod_is_new, cat_is_new, pack_is_new = new_items counts['product'] += int(prod_is_new) counts['category'] += int(cat_is_new) counts['package'] += int(pack_is_new) except (PackageLookupError, CategoryLookupError, ValueError, TypeError) as e: error_msgs.append(e.message) counts['total'] = len(dict_list) counts['report'] = len(new_report_keys) counts['error'] = len(error_msgs) if len(new_report_keys): general_region.invalidate(hard=False) reporters = ', '.join( set(self.request.params.getall('reporter_name'))) # send email from pyramid_mailer import get_mailer from pyramid_mailer.message import Message mailer = get_mailer(self.request) message = Message( subject=u'Price Watch: отчеты от {}'.format(reporters), sender='*****@*****.**', recipients=["*****@*****.**"], html=render('email/post_report_stats.mako', {'counts': counts, 'reporters': reporters, 'error_msgs': error_msgs})) mailer.send(message) return { 'new_report_keys': new_report_keys, 'counts': counts, 'errors': error_msgs } else: raise HTTPBadRequest('No new reports\n' + '\n'.join(error_msgs))
def post(self): # TODO Implement validation try: dict_list = multidict_to_list(self.request.params) except MultidictError as e: raise HTTPBadRequest(e.message) counts = {'product': 0, 'category': 0, 'package': 0} new_report_keys = list() error_msgs = list() for dict_ in dict_list: try: report, new_items = PriceReport.assemble( storage_manager=self.root, **dict_) new_report_keys.append(report.key) prod_is_new, cat_is_new, pack_is_new = new_items counts['product'] += int(prod_is_new) counts['category'] += int(cat_is_new) counts['package'] += int(pack_is_new) except (PackageLookupError, CategoryLookupError, ValueError, TypeError) as e: error_msgs.append(e.message) counts['total'] = len(dict_list) counts['report'] = len(new_report_keys) counts['error'] = len(error_msgs) if len(new_report_keys): general_region.invalidate(hard=False) reporters = ', '.join( set(self.request.params.getall('reporter_name'))) # send email from pyramid_mailer import get_mailer from pyramid_mailer.message import Message mailer = get_mailer(self.request) message = Message( subject=u'Price Watch: отчеты от {}'.format(reporters), sender='*****@*****.**', recipients=["*****@*****.**"], html=render( 'email/post_report_stats.mako', { 'counts': counts, 'reporters': reporters, 'error_msgs': error_msgs })) mailer.send(message) return { 'new_report_keys': new_report_keys, 'counts': counts, 'errors': error_msgs } else: raise HTTPBadRequest('No new reports\n' + '\n'.join(error_msgs))
def test_multidict_to_list(self): from price_watch.utilities import multidict_to_list from webob.multidict import MultiDict multidict = MultiDict(( ('price_value', 55.6), ('url', 'http://howies.com/products/milk/4'), ('product_title', u'Молоко Красная Цена у/паст. 3.2% 1л'.encode('utf-8')), ('merchant_title', "Howie's grocery"), ('reporter_name', 'Jack'), ('price_value', 45.3), ('url', 'http://howies.com/products/milk/5'), ('product_title', u'Молоко Красная Цена у/паст. 1% 1л'.encode('utf-8')), ('merchant_title', "Howie's grocery"), ('reporter_name', 'Jack'), )) dict_list = multidict_to_list(multidict) self.assertEqual(2, len(dict_list))
def test_multidict_to_list(self): from price_watch.utilities import multidict_to_list from webob.multidict import MultiDict multidict = MultiDict( ( ("price_value", 55.6), ("url", "http://howies.com/products/milk/4"), ("product_title", u"Молоко Красная Цена у/паст. 3.2% 1л".encode("utf-8")), ("merchant_title", "Howie's grocery"), ("reporter_name", "Jack"), ("price_value", 45.3), ("url", "http://howies.com/products/milk/5"), ("product_title", u"Молоко Красная Цена у/паст. 1% 1л".encode("utf-8")), ("merchant_title", "Howie's grocery"), ("reporter_name", "Jack"), ) ) dict_list = multidict_to_list(multidict) self.assertEqual(2, len(dict_list))