Repository: e-valuation/EvaP

Issue:
Update example files
The example files must be updated so that they can be imported without errors.


Discussion:
i thought we had tests for that. if not, this issue includes writing tests for that :)
The tests for the sample files are in evap/evaluation/tests/test_misc.py, testing the semester and user import sample files. I guess they pass due to the overrided settings, but I didn't look into that. These tests should be moved to staff/tests/test_views.py I guess.

The course person import sample file is not tested. Currently it's the same as the user import file, so the file is actually checked, but in case it changes, this might lead to missing tests.
Assign please

I currently plan on doing it by opening a template sample file with xlrd, copying it to a xlwt workbook, modifying that so that the emails are correct and then serving the file in the HTTP response. This would happen in a new view (which will not be visible). The code will be less than 50 lines. Testing would happen in two layers:
1. Test that the template files are valid
2. Test that the substitution works

I think this would be the most efficient way.

Resolving patch:
diff --git a/evap/staff/urls.py b/evap/staff/urls.py
--- a/evap/staff/urls.py
+++ b/evap/staff/urls.py
@@ -64,6 +64,8 @@
     url(r"^template/$", RedirectView.as_view(url='/staff/', permanent=True)),
     url(r"^template/(\d+)$", views.template_edit, name="template_edit"),
 
-    url(r"faq/$", views.faq_index, name="faq_index"),
-    url(r"faq/(\d+)$", views.faq_section, name="faq_section"),
+    url(r"^faq/$", views.faq_index, name="faq_index"),
+    url(r"^faq/(\d+)$", views.faq_section, name="faq_section"),
+
+    url(r"^download_sample_xls/(.+)$", views.download_sample_xls, name="download_sample_xls")
 ]
diff --git a/evap/staff/views.py b/evap/staff/views.py
--- a/evap/staff/views.py
+++ b/evap/staff/views.py
@@ -1,8 +1,11 @@
 import csv
 import datetime
 import random
+from xlrd import open_workbook as open_workbook
+from xlutils.copy import copy as copy_workbook
 from collections import OrderedDict, defaultdict
 
+from django.conf import settings
 from django.contrib import messages
 from django.core.exceptions import PermissionDenied, SuspiciousOperation
 from django.db import IntegrityError, transaction
@@ -1249,3 +1252,27 @@ def faq_section(request, section_id):
     else:
         template_data = dict(formset=formset, section=section, questions=questions)
         return render(request, "staff_faq_section.html", template_data)
+
+
+@staff_required
+def download_sample_xls(request, filename):
+    email_placeholder = "institution.com"
+
+    if filename not in ["sample.xls", "sample_user.xls"]:
+        raise SuspiciousOperation("Invalid file name.")
+
+    read_book = open_workbook(settings.STATICFILES_DIRS[0] + "/" + filename, formatting_info=True)
+    write_book = copy_workbook(read_book)
+    for sheet_index in range(read_book.nsheets):
+        read_sheet = read_book.sheet_by_index(sheet_index)
+        write_sheet = write_book.get_sheet(sheet_index)
+        for row in range(read_sheet.nrows):
+            for col in range(read_sheet.ncols):
+                value = read_sheet.cell(row, col).value
+                if email_placeholder in value:
+                    write_sheet.write(row, col, value.replace(email_placeholder, settings.INSTITUTION_EMAIL_DOMAINS[0]))
+
+    response = HttpResponse(content_type="application/vnd.ms-excel")
+    response["Content-Disposition"] = "attachment; filename=\"{}\"".format(filename)
+    write_book.save(response)
+    return response


Test patch:
diff --git a/evap/staff/tests/test_views.py b/evap/staff/tests/test_views.py
--- a/evap/staff/tests/test_views.py
+++ b/evap/staff/tests/test_views.py
@@ -20,6 +20,31 @@ def helper_delete_all_import_files(user_id):
     for filename in glob.glob(file_filter):
         os.remove(filename)
 
+# Staff - Sample Files View
+class TestDownloadSampleXlsView(ViewTest):
+    test_users = ['staff']
+    url = '/staff/download_sample_xls/sample.xls'
+    email_placeholder = "institution.com"
+
+    @classmethod
+    def setUpTestData(cls):
+        mommy.make(UserProfile, username='staff', groups=[Group.objects.get(name='Staff')])
+
+    def test_sample_file_correctness(self):
+        page = self.app.get(self.url, user='staff')
+
+        found_institution_domain = False
+        book = xlrd.open_workbook(file_contents=page.body)
+        for sheet in book.sheets():
+            for row in sheet.get_rows():
+                for cell in row:
+                    value = cell.value
+                    self.assertNotIn(self.email_placeholder, value)
+                    if settings.INSTITUTION_EMAIL_DOMAINS[0] in value:
+                        found_institution_domain = True
+
+        self.assertTrue(found_institution_domain)
+
 
 # Staff - Root View
 class TestStaffIndexView(ViewTest):