From 48546a18d4d8d23280144bf84bb21320575067a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Schweigh=C3=B6fer?= Date: Mon, 23 Oct 2017 14:39:32 +0200 Subject: [PATCH] Fix isbn problem, add mail errors --- pyBuchaktion/admin.py | 35 +++++++++++++++++-- pyBuchaktion/models.py | 6 +++- .../pyBuchaktion/admin/order_error.html | 9 +++++ .../admin/order_order_selected_csv.html | 4 +++ tucan-export/tucan-export.py | 4 ++- 5 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 pyBuchaktion/templates/pyBuchaktion/admin/order_error.html diff --git a/pyBuchaktion/admin.py b/pyBuchaktion/admin.py index f466f06..b167de1 100644 --- a/pyBuchaktion/admin.py +++ b/pyBuchaktion/admin.py @@ -214,6 +214,7 @@ def export(self, request, queryset): # The admin action for rejecting all selected orders at once. def reject_selected(self, request, queryset): if request.POST.get('_proceed'): + errors = [] hint = request.POST.get('hint') sendmails = '_sendmails' in request.POST for order in queryset: @@ -222,7 +223,18 @@ def reject_selected(self, request, queryset): order.save() if sendmails: email = OrderRejectedMessage(order) - email.send() + try: + email.send() + except Exception: + errors += [order.student.email] + if len(errors) > 0: + context = dict( + self.admin_site.each_context(request), + title=_("Errors"), + intro=_("The following emails could not be sent"), + errors=errors + ) + return TemplateResponse(request, 'pyBuchaktion/admin/order_error.html', context) elif not request.POST.get('_cancel'): context = dict( self.admin_site.each_context(request), @@ -240,6 +252,7 @@ def reject_selected(self, request, queryset): # The admin action for marking all selected orders as arrived. def mark_arrived_selected(self, request, queryset): if request.POST.get('_proceed'): + errors=[] sendmails = '_sendmails' in request.POST hint = request.POST.get('hint', "") for order in queryset: @@ -248,7 +261,18 @@ def mark_arrived_selected(self, request, queryset): order.save() if sendmails: email = OrderArrivedMessage(order) - email.send() + try: + email.send() + except Exception: + errors += [order.student.email] + if len(errors) > 0: + context = dict( + self.admin_site.each_context(request), + title=_("Errors"), + intro=_("The following emails could not be sent"), + errors=errors + ) + return TemplateResponse(request, 'pyBuchaktion/admin/order_error.html', context) elif not request.POST.get('_cancel'): context = dict( self.admin_site.each_context(request), @@ -266,6 +290,7 @@ def mark_arrived_selected(self, request, queryset): # The admin action for ordering the selected books def order_selected(self, request, queryset): if request.POST.get('_proceed'): + errors=[] sendmails = '_sendmails' in request.POST hint = request.POST.get('hint', "") for order in queryset: @@ -274,12 +299,16 @@ def order_selected(self, request, queryset): order.save() if sendmails: email = OrderAcceptedMessage(order) - email.send() + try: + email.send() + except Exception: + errors += [order.student.email] context = dict( self.admin_site.each_context(request), title=_("Ordering: CSV-Export"), queryset=queryset, opts=self.opts, + errors=[], action_checkbox_name=helpers.ACTION_CHECKBOX_NAME, ) return TemplateResponse(request, 'pyBuchaktion/admin/order_order_selected_csv.html', context) diff --git a/pyBuchaktion/models.py b/pyBuchaktion/models.py index 1d9752b..20e5111 100644 --- a/pyBuchaktion/models.py +++ b/pyBuchaktion/models.py @@ -104,7 +104,11 @@ class Book(models.Model): # The default string output for a book as " (<author>) [ISBN: <isbn>)" def __str__(self): - return '%s (%s) [ISBN: %s]' % (self.title, self.author, mask(self.isbn_13)) + try: + isbn = mask(self.isbn_13) + except Exception: + isbn = self.isbn_13 + return '%s (%s) [ISBN: %s]' % (self.title, self.author, isbn) # Get the name of the current state from the options def statename(self): diff --git a/pyBuchaktion/templates/pyBuchaktion/admin/order_error.html b/pyBuchaktion/templates/pyBuchaktion/admin/order_error.html new file mode 100644 index 0000000..c85807f --- /dev/null +++ b/pyBuchaktion/templates/pyBuchaktion/admin/order_error.html @@ -0,0 +1,9 @@ +{% extends "pyBuchaktion/admin/admin_action_page.html" %} +{% load i18n l10n %} + +{% block content %} + {% if len(errors) > 0 %} + <p>{{ intro }}</p> + <ul>{{ errors|unordered_list }}</ul> + {% endif %} +{% endblock %} \ No newline at end of file diff --git a/pyBuchaktion/templates/pyBuchaktion/admin/order_order_selected_csv.html b/pyBuchaktion/templates/pyBuchaktion/admin/order_order_selected_csv.html index cf9b851..31f65c0 100644 --- a/pyBuchaktion/templates/pyBuchaktion/admin/order_order_selected_csv.html +++ b/pyBuchaktion/templates/pyBuchaktion/admin/order_order_selected_csv.html @@ -3,6 +3,10 @@ {% block content %} <form action="" method="post">{% csrf_token %} + {% if errors|length > 0 %} + <h4>{% trans "E-Mails that failed to send" %}</h4> + <ul>{{ errors|unordered_list }}</ul> + {% endif %} <p>{% trans "Copy the following list into the bulk-order mask at net-library!" %}</p> <textarea name="hint" rows="{{ queryset|length|add:1 }}">{{ queryset|net_csv }}</textarea><br/><br/> <div> diff --git a/tucan-export/tucan-export.py b/tucan-export/tucan-export.py index d30cd08..abade08 100755 --- a/tucan-export/tucan-export.py +++ b/tucan-export/tucan-export.py @@ -24,8 +24,10 @@ GOOGLE_BOOKS_BASE_PATH = '/books/v1/volumes?' # CSS Selectors. TUCAN_CC_SELECTOR = '#pageTopNavi ul a' -TUCAN_DEPT_SELECTOR = '#auditRegistration_list li[title="Dept. 20 - Computer Science"] a' +#TUCAN_DEPT_SELECTOR = '#auditRegistration_list li[title="Dept. 20 - Computer Science"] a' +TUCAN_DEPT_SELECTOR = '#auditRegistration_list li[title="FB20 - Informatik"] a' # TUCAN_DEPT_SELECTOR = '#auditRegistration_list li[title="FB04 - Mathematik"] a' + TUCAN_MODULE_CONTAINER_SELECTOR = '#auditRegistration_list li a' TUCAN_BREADCRUMBS_SELECTOR = '.pageElementTop > h2 > a' TUCAN_MODULE_COURSE_IDNAME_SELECTOR = '#pageContent form h1';