Repository: e-valuation/EvaP

Issue:
importer: message linebreaks missing and a possible crash
#497 introduced two problems: one new error message does not have the required line breaks, and it added a get() that may throw due to multiple objects returned.

importer: message linebreaks missing and a possible crash
#497 introduced two problems: one new error message does not have the required line breaks, and it added a get() that may throw due to multiple objects returned.



Discussion:

Resolving patch:
diff --git a/evap/settings.py b/evap/settings.py
--- a/evap/settings.py
+++ b/evap/settings.py
@@ -210,7 +210,7 @@
 )
 
 # Absolute path to the directory static files should be collected to.
-STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
+STATIC_ROOT = os.path.join(BASE_DIR, "static_collected")
 
 
 ### User-uploaded files
diff --git a/evap/staff/importers.py b/evap/staff/importers.py
--- a/evap/staff/importers.py
+++ b/evap/staff/importers.py
@@ -182,15 +182,16 @@ def check_user_data_sanity(self):
             except UserProfile.DoesNotExist:
                 pass
                 
-            try:
-                user_same_name = UserProfile.objects.get(first_name=user_data.first_name, last_name=user_data.last_name)
-                if user_same_name.username != user_data.username:
-                    self.warnings.append(_(u"Warning: The existing user") + 
-                            u" {} ({} {} {}, {}) ".format(user_same_name.username, user_same_name.title or "", user_same_name.first_name, user_same_name.last_name, user_same_name.email) +
-                            _(u"has the same first and last name like ") +
-                            u" {} ({} {} {}, {})".format(user_data.username, user_data.title or "", user_data.first_name, user_data.last_name, user_data.email))
-            except UserProfile.DoesNotExist:
-                pass
+            users_same_name = UserProfile.objects.filter(first_name=user_data.first_name, last_name=user_data.last_name).exclude(username=user_data.username).all()
+            if len(users_same_name) > 0:
+                warningstring = _(u"An existing user has the same first and last name as a new user:") 
+                for user in users_same_name:
+                    warningstring += u"\n - {} ({} {} {}, {})".format(user.username, user.title or "", user.first_name, user.last_name, user.email)
+                    warningstring += _(u" (existing)")
+                warningstring += u"\n - {} ({} {} {}, {})".format(user_data.username, user_data.title or "", user_data.first_name, user_data.last_name, user_data.email)  
+                warningstring += _(u" (new)")
+                self.warnings.append(warningstring)
+
 
     def show_errors_and_warnings(self):
         for error in self.errors:
@@ -362,9 +363,9 @@ def process(cls, request, excel_file, test_run):
                 return
             importer.for_each_row_in_excel_file_do(importer.read_one_user)
             importer.consolidate_user_data()
+            importer.generate_external_usernames_if_external()
             importer.check_user_data_correctness()
             importer.check_user_data_sanity()
-            importer.generate_external_usernames_if_external()
 
             importer.show_errors_and_warnings()
             if importer.errors:


Test patch:
diff --git a/evap/staff/tests.py b/evap/staff/tests.py
--- a/evap/staff/tests.py
+++ b/evap/staff/tests.py
@@ -2,14 +2,16 @@
 from django_webtest import WebTest
 from webtest import AppError
 from django.test import Client
+from django.test.utils import override_settings
 from django.forms.models import inlineformset_factory
 from django.core import mail
 from django.core.exceptions import ObjectDoesNotExist
 from django.conf import settings
+from django.contrib.auth.models import Group
 
 from evap.evaluation.models import Semester, Questionnaire, UserProfile, Course, Contribution, TextAnswer, EmailTemplate
 from evap.staff.forms import CourseEmailForm, UserForm, SelectCourseForm, ReviewTextAnswerForm, \
-                            ContributionFormSet, ContributionForm, CourseForm
+                            ContributionFormSet, ContributionForm, CourseForm, ImportForm, UserImportForm
 from evap.rewards.models import RewardPointRedemptionEvent, SemesterActivation
 from evap.rewards.tools import reward_points_of_user
 
@@ -35,6 +37,43 @@ def __eq__(self, other):
     def __repr__(self):
         return "[%d..%d]" % (self.lowest, self.highest)
 
+@override_settings(INSTITUTION_EMAIL_DOMAINS=["institution.com", "student.institution.com"])
+class SampleXlsTests(WebTest):
+
+    def setUp(self):
+        semester = Semester(pk=1, name_de="Testsemester", name_en="test semester")
+        user = UserProfile(username="user")
+        group = Group(name="Staff")
+        group.save()
+        user.save()
+        user.groups = [group]
+        semester.save()
+
+    def test_sample_xls(self):
+        page = self.app.get("/staff/semester/1/import", user='user')
+
+        original_user_count = UserProfile.objects.all().count()
+
+        form = lastform(page)
+        form["vote_start_date"] = "2015-01-01"
+        form["vote_end_date"] = "2099-01-01"
+        form["excel_file"] = (os.path.join(settings.BASE_DIR, "static", "sample.xls"),)
+        form.submit(name="operation", value="import")
+
+        self.assertEqual(UserProfile.objects.count(), original_user_count + 4)
+
+    def test_sample_user_xls(self):
+        page = self.app.get("/staff/user/import", user='user')
+
+        original_user_count = UserProfile.objects.all().count()
+
+        form = lastform(page)
+        form["excel_file"] = (os.path.join(settings.BASE_DIR, "static", "sample_user.xls"),)
+        form.submit(name="operation", value="import")
+
+        self.assertEqual(UserProfile.objects.count(), original_user_count + 2)
+        
+
 
 class UsecaseTests(WebTest):
     fixtures = ['usecase-tests']
@@ -56,7 +95,7 @@ def test_import(self):
 
         self.assertEqual(semester.course_set.count(), 0, "New semester is not empty.")
 
-        # safe original user count
+        # save original user count
         original_user_count = UserProfile.objects.all().count()
 
         # import excel file
@@ -205,19 +244,12 @@ def test_users_are_deletable(self):
         self.assertFalse(UserProfile.objects.filter(username="contributor_user").get().can_staff_delete)
 
 
-
+@override_settings(INSTITUTION_EMAIL_DOMAINS=["example.com"])
 class URLTests(WebTest):
     fixtures = ['minimal_test_data']
     csrf_checks = False
     extra_environ = {'HTTP_ACCEPT_LANGUAGE': 'en'}
 
-    def setUp(self):
-        settings.INSTITUTION_EMAIL_DOMAINS.append("example.com")
-
-    def tearDown(self):
-        # settings are persistent between tests, so remove that again
-        settings.INSTITUTION_EMAIL_DOMAINS.remove("example.com")
-
     def get_assert_200(self, url, user):
         response = self.app.get(url, user=user)
         self.assertEqual(response.status_code, 200, 'url "{}" failed with user "{}"'.format(url, user))