diff options
Diffstat (limited to 'yaksh/views.py')
-rw-r--r-- | yaksh/views.py | 46 |
1 files changed, 39 insertions, 7 deletions
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 |