diff options
author | ankitjavalkar | 2018-05-16 17:53:55 +0530 |
---|---|---|
committer | ankitjavalkar | 2018-07-13 13:00:35 +0530 |
commit | 26ca38c6243eaa1d94bca68481eb60b2276ab7b2 (patch) | |
tree | 0e8a60727b9a951bd85780275bd742c635d27f82 /yaksh | |
parent | 661c9d82bb680e745cc6b498131a0793b954c436 (diff) | |
download | online_test-26ca38c6243eaa1d94bca68481eb60b2276ab7b2.tar.gz online_test-26ca38c6243eaa1d94bca68481eb60b2276ab7b2.tar.bz2 online_test-26ca38c6243eaa1d94bca68481eb60b2276ab7b2.zip |
Allow moderator to switch between student and moderator roles
Diffstat (limited to 'yaksh')
-rw-r--r-- | yaksh/models.py | 12 | ||||
-rw-r--r-- | yaksh/templates/yaksh/view_profile.html | 1 | ||||
-rw-r--r-- | yaksh/test_views.py | 117 | ||||
-rw-r--r-- | yaksh/urls.py | 1 | ||||
-rw-r--r-- | yaksh/views.py | 46 |
5 files changed, 142 insertions, 35 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index 1eca721..1199a92 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -8,7 +8,7 @@ from ruamel.yaml.comments import CommentedMap from random import sample from collections import Counter from django.db import models -from django.contrib.auth.models import User +from django.contrib.auth.models import User, Group from django.contrib.contenttypes.models import ContentType from taggit.managers import TaggableManager from django.utils import timezone @@ -982,6 +982,7 @@ class Profile(models.Model): institute = models.CharField(max_length=128) department = models.CharField(max_length=64) position = models.CharField(max_length=64) + is_moderator = models.BooleanField(default=False) timezone = models.CharField( max_length=64, default=pytz.utc.zone, @@ -1000,6 +1001,15 @@ class Profile(models.Model): os.chmod(user_dir, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) return user_dir + def _add_to_moderator_group(self, group_name='moderator'): + if self.is_moderator: + group = Group.objects.get(name=group_name) + self.user.groups.add(group) + + def save(self, *args, **kwargs): + super(Profile, self).save(*args, **kwargs) + self._add_to_moderator_group() + def __str__(self): return '%s' % (self.user.get_full_name()) diff --git a/yaksh/templates/yaksh/view_profile.html b/yaksh/templates/yaksh/view_profile.html index ce95226..1b75e6e 100644 --- a/yaksh/templates/yaksh/view_profile.html +++ b/yaksh/templates/yaksh/view_profile.html @@ -37,4 +37,5 @@ </tr> </table> <a class="btn btn-primary pull-right" href="{{ URL_ROOT }}/exam/editprofile/">Edit Profile</a> + <a class="btn btn-primary pull-left" href="{{ URL_ROOT }}/exam/toggle_moderator/">Toggle Moderator Role</a> {% endblock %} diff --git a/yaksh/test_views.py b/yaksh/test_views.py index 06a4fa3..b71020e 100644 --- a/yaksh/test_views.py +++ b/yaksh/test_views.py @@ -467,7 +467,7 @@ class TestMonitor(TestCase): password=self.user_plaintext_pass, first_name='first_name', last_name='last_name', - email='demo@test.com' + email='demo@test.com', ) Profile.objects.create( @@ -476,7 +476,8 @@ class TestMonitor(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) # Create Student @@ -661,7 +662,8 @@ class TestGradeUser(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) # Create Student @@ -877,7 +879,8 @@ class TestDownloadAssignment(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) # Add to moderator group @@ -1052,7 +1055,8 @@ class TestAddQuiz(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) # Create Student @@ -1354,7 +1358,8 @@ class TestAddTeacher(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) # Create Student @@ -1668,7 +1673,8 @@ class TestCourses(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) self.user2_plaintext_pass = 'demo2' @@ -1686,7 +1692,8 @@ class TestCourses(TestCase): institute='IIT', department='Aeronautical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) # Create Student @@ -1716,6 +1723,16 @@ class TestCourses(TestCase): email='demo_teacher@test.com' ) + Profile.objects.create( + user=self.teacher, + roll_number=10, + institute='IIT', + department='Aeronautical', + position='Moderator', + timezone='UTC', + is_moderator=True + ) + # Add to moderator group self.mod_group.user_set.add(self.user1) self.mod_group.user_set.add(self.user2) @@ -2087,7 +2104,8 @@ class TestAddCourse(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) # Create a teacher @@ -2106,7 +2124,8 @@ class TestAddCourse(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) # Create Student @@ -2266,7 +2285,8 @@ class TestCourseDetail(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) self.user2_plaintext_pass = 'demo2' @@ -2849,7 +2869,8 @@ class TestEnrollRequest(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) self.user2_plaintext_pass = 'demo2' @@ -2867,7 +2888,8 @@ class TestEnrollRequest(TestCase): institute='IIT', department='Aeronautical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) # Create Student @@ -3118,7 +3140,8 @@ class TestSelfEnroll(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) self.user2_plaintext_pass = 'demo2' @@ -3136,7 +3159,8 @@ class TestSelfEnroll(TestCase): institute='IIT', department='Aeronautical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) # Create Student @@ -3228,7 +3252,8 @@ class TestGrader(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) self.user2_plaintext_pass = 'demo2' @@ -3246,7 +3271,8 @@ class TestGrader(TestCase): institute='IIT', department='Aeronautical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) # Create Student @@ -3553,7 +3579,8 @@ class TestModeratorDashboard(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) self.mod_no_profile_plaintext_pass = 'demo2' @@ -3801,7 +3828,7 @@ class TestUserLogin(TestCase): self.assertTemplateUsed(response, "yaksh/activation_status.html") -class TestDownloadcsv(TestCase): +class TestDownloadCsv(TestCase): def setUp(self): self.client = Client() tzone = pytz.timezone("utc") @@ -3841,7 +3868,8 @@ class TestDownloadcsv(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) self.mod_group.user_set.add(self.user) self.course = Course.objects.create( @@ -4036,7 +4064,8 @@ class TestShowQuestions(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) self.mod_group.user_set.add(self.user) self.question = Question.objects.create( @@ -4352,7 +4381,8 @@ class TestShowStatistics(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) # Create Student @@ -4514,7 +4544,8 @@ class TestQuestionPaper(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) self.student_plaintext_pass = 'demo' @@ -4568,7 +4599,8 @@ class TestQuestionPaper(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) # Add to moderator group @@ -5262,7 +5294,8 @@ class TestLearningModule(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) # Create a student @@ -5282,7 +5315,17 @@ class TestLearningModule(TestCase): password=self.teacher_plaintext_pass, first_name='first_name', last_name='last_name', - email='demo@student.com' + email='demo@teacher.com', + ) + + Profile.objects.create( + user=self.teacher, + roll_number=10, + institute='IIT', + department='Chemical', + position='Moderator', + timezone='UTC', + is_moderator=True ) # Add to moderator group @@ -5643,7 +5686,8 @@ class TestLessons(TestCase): institute='IIT', department='Chemical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) # Create a student @@ -5656,6 +5700,15 @@ class TestLessons(TestCase): email='demo@student.com' ) + Profile.objects.create( + user=self.student, + roll_number=10, + institute='IIT', + department='Chemical', + position='student', + timezone='UTC' + ) + # Create a teacher to add to the course self.teacher_plaintext_pass = 'demo_teacher' self.teacher = User.objects.create_user( @@ -5666,6 +5719,16 @@ class TestLessons(TestCase): email='demo@student.com' ) + Profile.objects.create( + user=self.teacher, + roll_number=10, + institute='IIT', + department='Chemical', + position='Moderator', + timezone='UTC', + is_moderator=True + ) + # Add to moderator group self.mod_group.user_set.add(self.user) self.mod_group.user_set.add(self.teacher) diff --git a/yaksh/urls.py b/yaksh/urls.py index 6e6b8c1..8f3401b 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -8,6 +8,7 @@ urlpatterns = [ url(r'^update_email/$', views.update_email, name="update_email"), url(r'^activate/(?P<key>.+)$', views.activate_user, name="activate"), url(r'^new_activation/$', views.new_activation, name='new_activation'), + url(r'^toggle_moderator/$', views.toggle_moderator_role, name='toggle_moderator'), url(r'^quizzes/$', views.quizlist_user, name='quizlist_user'), url(r'^quizzes/(?P<enrolled>\w+)/$', views.quizlist_user, name='quizlist_user'), diff --git a/yaksh/views.py b/yaksh/views.py index c6b1184..7cfb03f 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -63,18 +63,27 @@ def my_render_to_response(request, template, context=None, **kwargs): return render(request, template, context, **kwargs) -def is_moderator(user): +def is_moderator(user, group_name='moderator'): """Check if the user is having moderator rights""" - if user.groups.filter(name='moderator').exists(): - return True + try: + group = Group.objects.get(name='moderator') + return user.profile.is_moderator and user in group.user_set.all() + except Profile.DoesNotExist: + return False + except Group.DoesNotExist: + return False -def add_to_group(users): +def add_as_moderator(users, group_name='moderator'): """ add users to moderator group """ - group = Group.objects.get(name="moderator") + try: + group = Group.objects.get(name=group_name) + except Group.DoesNotExist: + raise Http404('The Group {0} does not exist.'.format(group_name)) for user in users: if not is_moderator(user): - user.groups.add(group) + user.profile.is_moderator = True + user.profile.save() CSV_FIELDS = ['name', 'username', 'roll_number', 'institute', 'department', @@ -1757,6 +1766,29 @@ def search_teacher(request, course_id): @login_required @email_verified +def toggle_moderator_role(request): + """ Allow moderator to switch to student and back """ + + user = request.user + + try: + group = Group.objects.get(name='moderator') + except Group.DoesNotExist: + raise Http404('The Moderator group does not exist') + + if not user.profile.is_moderator: + raise Http404('You are not allowed to view this page!') + + if user not in group.user_set.all(): + group.user_set.add(user) + else: + group.user_set.remove(user) + + return my_redirect('/exam/') + + +@login_required +@email_verified def add_teacher(request, course_id): """ add teachers to the course """ @@ -1775,7 +1807,7 @@ def add_teacher(request, course_id): if request.method == 'POST': teacher_ids = request.POST.getlist('check') teachers = User.objects.filter(id__in=teacher_ids) - add_to_group(teachers) + add_as_moderator(teachers) course.add_teachers(*teachers) context['status'] = True context['teachers_added'] = teachers |