diff options
-rw-r--r-- | yaksh/models.py | 21 | ||||
-rw-r--r-- | yaksh/templates/yaksh/addteacher.html | 25 | ||||
-rw-r--r-- | yaksh/tests.py | 29 | ||||
-rw-r--r-- | yaksh/views.py | 47 |
4 files changed, 45 insertions, 77 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index cb9582f..a4ea6c3 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -4,7 +4,7 @@ from random import sample, shuffle from itertools import islice, cycle from collections import Counter from django.db import models -from django.contrib.auth.models import User, Group +from django.contrib.auth.models import User from taggit.managers import TaggableManager @@ -23,13 +23,6 @@ class Profile(models.Model): department = models.CharField(max_length=64) position = models.CharField(max_length=64) - def is_moderator(self, user): - return user.groups.filter(name='moderator').exists() - - def _add_to_group(self, user): - group = Group.objects.get(name="moderator") - user.groups.add(group) - languages = ( ("python", "Python"), ("bash", "Bash"), @@ -128,17 +121,7 @@ class Course(models.Model): self.active = False def add_teachers(self, *teachers): - added_teachers = list() - rejected_teachers = list() - for teacher in teachers: - if hasattr(teacher, 'profile'): - if not teacher.profile.is_moderator(teacher): - teacher.profile._add_to_group(teacher) - self.teachers.add(teacher) - added_teachers.append(teacher) - else: - rejected_teachers.append(teacher) - return added_teachers, rejected_teachers + self.teachers.add(*teachers) def get_teachers(self): return self.teachers.all() diff --git a/yaksh/templates/yaksh/addteacher.html b/yaksh/templates/yaksh/addteacher.html index 6bb5c9a..7e04f71 100644 --- a/yaksh/templates/yaksh/addteacher.html +++ b/yaksh/templates/yaksh/addteacher.html @@ -54,17 +54,17 @@ {% endif %} {% endif %} </form> -{% if status == True and teachers_added or teachers_rejected %} -<center> -<h4>Teacher(s) without profile are rejected</h4> +{% if status == True %} <div class="row"> - <div class="span6 wrap"> + <div class="span6 offset4 wrap"> <center><b><u>Teacher(s) Added</u></b></center><br> {% if teachers_added %} {% for teacher in teachers_added %} <div class="well"> <div class="row"> - <h5>{{ teacher.get_full_name }}</h5> + <div class="span3" style="width: auto;"> + <h5>{{ teacher.get_full_name }}</h5> + </div> </div> </div> {% endfor %} @@ -72,21 +72,6 @@ <center><b>No Teacher(s) Added</b></center> {% endif %} </div> - <div class="span6 wrap"> - <center><b><u>Teacher(s) Rejected</u></b></center><br> - {% if teachers_rejected %} - {% for teacher in teachers_rejected %} - <div class="well"> - <div class="row"> - <h5>{{ teacher.get_full_name }}</h5> - </div> - </div> - {% endfor %} - {% else %} - <center><b>No Teacher(s) Rejected</b></center> - {% endif %} - </div> </div> -</center> {% endif %} {% endblock %} diff --git a/yaksh/tests.py b/yaksh/tests.py index 831a0fb..cc3100e 100644 --- a/yaksh/tests.py +++ b/yaksh/tests.py @@ -21,17 +21,6 @@ def setUpModule(): Profile.objects.create(user=student, roll_number=3, institute='IIT', department='Chemical', position='Student') - student1 = User.objects.create_user(username='demo_user4', - password='demo', - email='demo@test.com') - Profile.objects.create(user=student1, roll_number=4, institute='IIT', - department='Chemical', position='Student') - - # create group - group = Group(name="moderator") - group.save() - student.groups.add(group) - # create a course course = Course.objects.create(name="Python Course", enrollment="Enroll Request", creator=user) @@ -79,12 +68,6 @@ class ProfileTestCases(unittest.TestCase): self.assertEqual(self.profile.department, 'Chemical') self.assertEqual(self.profile.position, 'Student') - def test_is_moderator(self): - result = self.user2.profile.is_moderator(self.user2) - self.assertTrue(result) - result = self.user1.profile.is_moderator(self.user1) - self.assertFalse(result) - ############################################################################### class QuestionTestCases(unittest.TestCase): def setUp(self): @@ -399,7 +382,6 @@ class CourseTestCases(unittest.TestCase): self.creator = User.objects.get(pk=1) self.student1 = User.objects.get(pk=2) self.student2 = User.objects.get(pk=3) - self.student3 = User.objects.get(pk=4) self.quiz1 = Quiz.objects.get(pk=1) self.quiz2 = Quiz.objects.get(pk=2) @@ -458,15 +440,14 @@ class CourseTestCases(unittest.TestCase): def test_add_teachers(self): """ Test to add teachers to a course""" - added_list, rejected_list = self.course.add_teachers(self.student1, self.student2) - self.assertSequenceEqual(added_list, [self.student2]) - self.assertSequenceEqual(rejected_list, [self.student1]) + self.course.add_teachers(self.student1, self.student2) + self.assertSequenceEqual(self.course.get_teachers(), [self.student1, self.student2]) def test_remove_teachers(self): """ Test to remove teachers from a course""" - self.course.add_teachers(self.student2, self.student3) - self.course.remove_teachers(self.student2) - self.assertSequenceEqual(self.course.get_teachers(), [self.student3]) + self.course.add_teachers(self.student1, self.student2) + self.course.remove_teachers(self.student1) + self.assertSequenceEqual(self.course.get_teachers(), [self.student2]) def test_is_teacher(self): """ Test to check if user is teacher""" diff --git a/yaksh/views.py b/yaksh/views.py index 3823976..4b87431 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -14,6 +14,7 @@ from django.http import Http404 from django.db.models import Sum, Max, Q from django.views.decorators.csrf import csrf_exempt from django.contrib.auth.decorators import login_required +from django.contrib.auth.models import Group from taggit.models import Tag from itertools import chain import json @@ -69,6 +70,13 @@ def has_profile(user): """ check if user has profile """ return True if hasattr(user, 'profile') else False +def add_teachers_to_group(teachers): + """ add teachers to moderator group """ + for teacher in teachers: + if not is_moderator(teacher): + group = Group.objects.get(name="moderator") + teacher.groups.add(group) + def index(request): """The start page. """ @@ -608,11 +616,14 @@ def courses(request): def course_detail(request, course_id): user = request.user ci = RequestContext(request) - course = Course.objects.filter(Q(creator=user)|Q(teachers=user), - pk=course_id).first() - if not is_moderator(user) or not course: + + if not is_moderator(user): raise Http404('You are not allowed to view this page') + course = get_object_or_404(Course, pk=course_id) + if not course.is_creator(user) and not course.is_teacher(user): + raise Http404('This course does not belong to you') + return my_render_to_response('yaksh/course_detail.html', {'course': course}, context_instance=ci) @@ -623,8 +634,11 @@ def enroll(request, course_id, user_id=None, was_rejected=False): ci = RequestContext(request) if not is_moderator(user): raise Http404('You are not allowed to view this page') - course = Course.objects.filter(Q(creator=user)|Q(teachers=user), - pk=course_id).first() + + course = get_object_or_404(Course, pk=course_id) + if not course.is_creator(user) and not course.is_teacher(user): + raise Http404('This course does not belong to you') + if request.method == 'POST': enroll_ids = request.POST.getlist('check') else: @@ -643,8 +657,11 @@ def reject(request, course_id, user_id=None, was_enrolled=False): ci = RequestContext(request) if not is_moderator(user): raise Http404('You are not allowed to view this page') - course = Course.objects.filter(Q(creator=user)|Q(teachers=user), - pk=course_id).first() + + course = get_object_or_404(Course, pk=course_id) + if not course.is_creator(user) and not course.is_teacher(user): + raise Http404('This course does not belong to you') + if request.method == 'POST': reject_ids = request.POST.getlist('check') else: @@ -662,8 +679,11 @@ def toggle_course_status(request, course_id): user = request.user if not is_moderator(user): raise Http404('You are not allowed to view this page') - course = Course.objects.filter(Q(creator=user)|Q(teachers=user), - pk=course_id).first() + + course = get_object_or_404(Course, pk=course_id) + if not course.is_creator(user) and not course.is_teacher(user): + raise Http404('This course does not belong to you') + if course.active: course.deactivate() else: @@ -1086,10 +1106,10 @@ def add_teacher(request, course_id): if request.method == 'POST': teacher_ids = request.POST.getlist('check') teachers = User.objects.filter(id__in=teacher_ids) - teachers_added, teachers_rejected = course.add_teachers(*teachers) + add_teachers_to_group(teachers) + course.add_teachers(*teachers) context['status'] = True - context['teachers_added'] = teachers_added - context['teachers_rejected'] = teachers_rejected + context['teachers_added'] = teachers return my_render_to_response('yaksh/addteacher.html', context, context_instance=ci) else: @@ -1122,6 +1142,5 @@ def remove_teachers(request, course_id): course = get_object_or_404(Course, creator=user, pk=course_id) if request.method == "POST": teacher_ids = request.POST.getlist('remove') - teachers = User.objects.filter(id__in=teacher_ids) - course.remove_teachers(*teachers) + course.remove_teachers(*teacher_ids) return my_redirect('/exam/manage/courses') |