From 570b141055f9baa27c539842b14756838949ba60 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Wed, 26 Aug 2020 18:51:25 +0530 Subject: Avoid duplicate user entry with same email address during upload. Django allows multiple usernames with same email id. Preventing this, as we identify users with their email id or username. --- yaksh/fixtures/user_existing_email.csv | 2 ++ yaksh/fixtures/users_add_update_reject.csv | 4 ++-- yaksh/test_views.py | 27 +++++++++++++++++++++++++++ yaksh/views.py | 4 +++- 4 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 yaksh/fixtures/user_existing_email.csv diff --git a/yaksh/fixtures/user_existing_email.csv b/yaksh/fixtures/user_existing_email.csv new file mode 100644 index 0000000..ee5fcd0 --- /dev/null +++ b/yaksh/fixtures/user_existing_email.csv @@ -0,0 +1,2 @@ +firstname, lastname, email +abc, abc, demo_student@test.com diff --git a/yaksh/fixtures/users_add_update_reject.csv b/yaksh/fixtures/users_add_update_reject.csv index 1990179..2b8fcf6 100644 --- a/yaksh/fixtures/users_add_update_reject.csv +++ b/yaksh/fixtures/users_add_update_reject.csv @@ -1,4 +1,4 @@ firstname, lastname, email, institute,department,roll_no,remove,password,username test, test, test@g.com, TEST, TEST, TEST101, FALSE, TEST, test -test2, test, test@g.com, TEST, TEST, TEST101, FALSE, TEST, test2 -test2, test, test@g.com, TEST, TEST, TEST101, TRUE, TEST, test2 +test2, test, test2@g.com, TEST, TEST, TEST101, FALSE, TEST, test2 +test2, test, test2@g.com, TEST, TEST, TEST101, TRUE, TEST, test2 diff --git a/yaksh/test_views.py b/yaksh/test_views.py index a7ccac2..5876f03 100644 --- a/yaksh/test_views.py +++ b/yaksh/test_views.py @@ -2755,6 +2755,33 @@ class TestCourseDetail(TestCase): id=uploaded_users.first().id).exists() ) + def test_upload_existing_user_email(self): + # Given + self.client.login( + username=self.user1.username, password=self.user1_plaintext_pass) + csv_file_path = os.path.join(FIXTURES_DIR_PATH, + 'user_existing_email.csv') + csv_file = open(csv_file_path, 'rb') + upload_file = SimpleUploadedFile(csv_file_path, csv_file.read()) + csv_file.close() + + # When + response = self.client.post( + reverse('yaksh:upload_users', + kwargs={'course_id': self.user1_course.id}), + data={'csv_file': upload_file}) + + # Then + uploaded_users = User.objects.filter(email='demo_student@test.com') + self.assertEqual(response.status_code, 302) + messages = [m.message for m in get_messages(response.wsgi_request)] + self.assertIn('demo_student', messages[0]) + self.assertTrue( + self.user1_course.students.filter( + id=uploaded_users.first().id).exists() + ) + self.assertEqual(uploaded_users.count(), 1) + def test_upload_users_add_update_reject(self): # Given self.client.login( diff --git a/yaksh/views.py b/yaksh/views.py index 3adb536..9f8468d 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -2409,8 +2409,10 @@ def _read_user_csv(request, reader, course): messages.info(request, "{0} -- Missing Values".format(counter)) continue users = User.objects.filter(username=username) + if not users.exists(): + users = User.objects.filter(email=email) if users.exists(): - user = users[0] + user = users.last() if remove.strip().lower() == 'true': _remove_from_course(user, course) messages.info(request, "{0} -- {1} -- User rejected".format( -- cgit