From b1dfc14962fafab7d8e8a8cd80f640b12b26fa76 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Mon, 4 Jun 2018 16:46:01 +0200
Subject: [PATCH] there is a way to print selected vouchers

---
 smash/web/docx_helper.py                      |  9 +++---
 .../includes/subject_vouchers_box.html        | 14 ++++++++++
 smash/web/templates/subjects/edit.html        |  8 +++---
 smash/web/tests/view/test_mail.py             | 22 +++++++++++++++
 smash/web/urls.py                             |  2 ++
 smash/web/views/mails.py                      | 28 +++++++++++++++++++
 6 files changed, 74 insertions(+), 9 deletions(-)
 create mode 100644 smash/web/tests/view/test_mail.py

diff --git a/smash/web/docx_helper.py b/smash/web/docx_helper.py
index 3dfe5105..a35bfc83 100644
--- a/smash/web/docx_helper.py
+++ b/smash/web/docx_helper.py
@@ -24,15 +24,14 @@ def process_file(path_to_docx, path_to_new_docx, changes_to_apply):
 def merge_files(files, path_to_new_docx):
     merged_document = Document()
 
-    for index, file in enumerate(files):
-        sub_doc = Document(file)
-
-        for element in sub_doc.element.body:
-            merged_document.element.body.append(element)
+    for index, input_file in enumerate(files):
+        sub_doc = Document(input_file)
 
         # Don't add a page break if you've reached the last file.
         if index < len(files) - 1:
             sub_doc.add_page_break()
 
+        for element in sub_doc.element.body:
+            merged_document.element.body.append(element)
 
     merged_document.save(path_to_new_docx)
diff --git a/smash/web/templates/includes/subject_vouchers_box.html b/smash/web/templates/includes/subject_vouchers_box.html
index a17610a2..4cd7c2f4 100644
--- a/smash/web/templates/includes/subject_vouchers_box.html
+++ b/smash/web/templates/includes/subject_vouchers_box.html
@@ -13,6 +13,7 @@
                     <thead>
                     <tr>
 
+                        <th class="text-center">Select</th>
                         <th class="text-center">Number</th>
                         <th class="text-center">Type</th>
                         <th class="text-center">Issue date</th>
@@ -26,6 +27,7 @@
                     <tbody>
                     {% for voucher in subject.vouchers.all %}
                         <tr>
+                            <td><input type="checkbox" data-id="{{ voucher.id }}" class="voucher_checkbox"/></td>
                             <td>{{ voucher.number }}</td>
                             <td>{{ voucher.voucher_type }}</td>
                             <td>{{ voucher.issue_date }}</td>
@@ -39,6 +41,18 @@
                     {% endfor %}
                     </tbody>
                 </table>
+                <a href="#" data-url="{% url "web.views.mail_template_generate_for_vouchers" %}" onclick='
+                var checkboxes = $(".voucher_checkbox");
+                var url = $(this).data("url")+"?voucher_id=";
+
+                for (var i=0;i<checkboxes.length;i++){
+                    if ($(checkboxes[i]).is(":checked")) {
+                      url+=$(checkboxes[i]).data("id")+",";
+                    }
+                }
+                this.href = url;
+                '>Print vouchers</a>
+
             </div>
         </div>
 
diff --git a/smash/web/templates/subjects/edit.html b/smash/web/templates/subjects/edit.html
index 669f8630..73249d4f 100644
--- a/smash/web/templates/subjects/edit.html
+++ b/smash/web/templates/subjects/edit.html
@@ -109,10 +109,6 @@
             </div><!-- /.col-md-12 -->
         </div><!-- /.row -->
 
-        {% if study_subject.study.columns.vouchers %}
-            {% include 'includes/subject_vouchers_box.html' with subject=study_subject %}
-        {% endif %}
-
         {% include 'includes/mail_templates_box.html' with instance_id=study_subject.id %}
 
         {% include 'includes/contact_attempts_box.html' with subject=study_subject contact_attempts=contact_attempts %}
@@ -157,6 +153,10 @@
             </div><!-- /.modal-dialog -->
         </div><!-- /.modal -->
 
+        {% if study_subject.study.columns.vouchers %}
+            {% include 'includes/subject_vouchers_box.html' with subject=study_subject %}
+        {% endif %}
+
     {% endblock %}
 
 
diff --git a/smash/web/tests/view/test_mail.py b/smash/web/tests/view/test_mail.py
new file mode 100644
index 00000000..900c84a7
--- /dev/null
+++ b/smash/web/tests/view/test_mail.py
@@ -0,0 +1,22 @@
+import logging
+
+from django.urls import reverse
+
+from web.models import MailTemplate
+from web.models.constants import MAIL_TEMPLATE_CONTEXT_VOUCHER
+from web.tests.functions import create_voucher, get_resource_path
+from web.tests import LoggedInTestCase
+
+logger = logging.getLogger(__name__)
+
+
+class MailTests(LoggedInTestCase):
+    def test_generate_vouchers(self):
+        voucher = create_voucher()
+        MailTemplate(name="name", language=None,
+                     context=MAIL_TEMPLATE_CONTEXT_VOUCHER,
+                     template_file=get_resource_path('upcoming_appointment_FR.docx')).save()
+
+        page = reverse('web.views.mail_template_generate_for_vouchers') + "?voucher_id=" + str(voucher.id)
+        response = self.client.get(page)
+        self.assertEqual(response.status_code, 200)
diff --git a/smash/web/urls.py b/smash/web/urls.py
index 63496dee..ff3f13a9 100644
--- a/smash/web/urls.py
+++ b/smash/web/urls.py
@@ -164,6 +164,8 @@ urlpatterns = [
         name='web.views.mail_template_edit'),
     url(r'^mail_templates/(?P<mail_template_id>\d+)/generate/(?P<instance_id>\d+)$', views.mails.generate,
         name="web.views.mail_template_generate"),
+    url(r'^mail_templates/print_vouchers$', views.mails.generate_for_vouchers,
+        name="web.views.mail_template_generate_for_vouchers"),
 
     ####################
     # DAILY PLANNING   #
diff --git a/smash/web/views/mails.py b/smash/web/views/mails.py
index 8b7e8e89..32a1f3b0 100644
--- a/smash/web/views/mails.py
+++ b/smash/web/views/mails.py
@@ -11,6 +11,7 @@ from django.views.generic import DeleteView
 from django.views.generic import ListView
 from django.views.generic import UpdateView
 
+from web.docx_helper import merge_files
 from . import WrappedView
 from ..models import StudySubject, Visit, Appointment, MailTemplate, Voucher
 from ..models.constants import MAIL_TEMPLATE_CONTEXT_SUBJECT, MAIL_TEMPLATE_CONTEXT_VISIT, \
@@ -85,3 +86,30 @@ def generate(request, mail_template_id, instance_id):
     response['Content-Length'] = file_size
     response['Content-Disposition'] = 'attachment; filename={}.docx'.format(mail_template.name)
     return response
+
+
+def generate_for_vouchers(request):
+    ids = request.GET.get('voucher_id', '').split(',')
+    vouchers = []
+    for voucher_id in ids:
+        if voucher_id.isdigit():
+            vouchers.append(Voucher.objects.get(pk=int(voucher_id)))
+    templates = MailTemplate.get_voucher_mail_templates([])[0]
+
+    output_stream = StringIO.StringIO()
+
+    inputs = []
+    for template in templates:
+        for voucher in vouchers:
+            input_stream = StringIO.StringIO()
+            input_stream = template.apply(voucher, request.user, input_stream)
+            input_stream.seek(0)
+            inputs.append(input_stream)
+
+    merge_files(inputs, output_stream)
+    file_size = output_stream.tell()
+    output_stream.seek(0)
+    response = HttpResponse(FileWrapper(output_stream), content_type=MIMETYPE_DOCX)
+    response['Content-Length'] = file_size
+    response['Content-Disposition'] = 'attachment; filename=vouchers.docx'
+    return response
-- 
GitLab