diff options
-rw-r--r-- | yaksh/models.py | 24 | ||||
-rw-r--r-- | yaksh/templates/yaksh/addteacher.html | 40 | ||||
-rw-r--r-- | yaksh/tests.py | 19 | ||||
-rw-r--r-- | yaksh/urls.py | 2 | ||||
-rw-r--r-- | yaksh/views.py | 57 |
5 files changed, 98 insertions, 44 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index 65b4dc0..6db1298 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 +from django.contrib.auth.models import User, Group from taggit.managers import TaggableManager @@ -23,6 +23,14 @@ class Profile(models.Model): department = models.CharField(max_length=64) position = models.CharField(max_length=64) + def is_moderator(self, user): + return True if user.groups.filter(name='moderator').exists() else False + + def _add_to_group(self, user): + group = Group.objects.get(name="moderator") + user.groups.add(group) + user.profile.position = "Faculty" + user.profile.save() languages = ( ("python", "Python"), @@ -118,8 +126,18 @@ class Course(models.Model): def deactivate(self): self.active = False - def add_teachers(self, *users): - self.teachers.add(*users) + 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 def __unicode__(self): return self.name diff --git a/yaksh/templates/yaksh/addteacher.html b/yaksh/templates/yaksh/addteacher.html index 2cb0625..b438262 100644 --- a/yaksh/templates/yaksh/addteacher.html +++ b/yaksh/templates/yaksh/addteacher.html @@ -35,11 +35,7 @@ <th>Position</th> {% for teacher in teachers %} <tr> - <td> - {% if teacher != user %} - <input type="checkbox" name="check" value="{{ teacher.id }}"> - {% endif %} - </td> + <td><input type="checkbox" name="check" value="{{ teacher.id }}"></td> <td>{{ teacher.first_name }}</td> <td>{{ teacher.last_name }}</td> <td>{{ teacher.email }}</td> @@ -53,7 +49,35 @@ {% endif %} {% endif %} </form> -{% if status == True %} -<center><h3>Added Teacher(s) to this course</h3></center> +{% if status == True and teachers_added or teachers_rejected %} +<center> +<h4>Note: Teacher(s) without profile are rejected</h4> +<div class="row"> + <div class="span6 wrap"> + <center><b><u>Teacher(s) Added</u></b></center><br> + {% for teacher in teachers_added %} + <div class="well"> + <div class="row"> + <div class="span3" style="padding-top:10px"> + <h5>{{ teacher.get_full_name }}</h5> + </div> + </div> + </div> + {% endfor %} + </div> + <div class="span6 wrap"> + <center><b><u>Teacher(s) Rejected</u></b></center><br> + {% for teacher in teachers_rejected %} + <div class="well"> + <div class="row"> + <div class="span4" style="padding-top:10px"> + <h5>{{ teacher.get_full_name }}</h5> + </div> + </div> + </div> + {% endfor %} + </div> +</div> +</center> {% endif %} -{% endblock %}
\ No newline at end of file +{% endblock %} diff --git a/yaksh/tests.py b/yaksh/tests.py index d6d1d91..d69882d 100644 --- a/yaksh/tests.py +++ b/yaksh/tests.py @@ -3,7 +3,7 @@ from yaksh.models import User, Profile, Question, Quiz, QuestionPaper,\ QuestionSet, AnswerPaper, Answer, TestCase, Course import json from datetime import datetime, timedelta - +from django.contrib.auth.models import Group def setUpModule(): # create user profile @@ -21,6 +21,11 @@ def setUpModule(): Profile.objects.create(user=student, roll_number=3, 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) @@ -57,6 +62,7 @@ class ProfileTestCases(unittest.TestCase): def setUp(self): self.user = User.objects.get(pk=1) self.profile = Profile.objects.get(pk=1) + self.user1 = User.objects.get(pk=3) def test_user_profile(self): """ Test user profile""" @@ -67,6 +73,11 @@ class ProfileTestCases(unittest.TestCase): self.assertEqual(self.profile.department, 'Chemical') self.assertEqual(self.profile.position, 'Student') + def test_is_moderator(self): + result = self.user1.profile.is_moderator(self.user1) + self.assertTrue(result) + result = self.user.profile.is_moderator(self.user) + self.assertFalse(result) ############################################################################### class QuestionTestCases(unittest.TestCase): @@ -437,3 +448,9 @@ class CourseTestCases(unittest.TestCase): def test_get_quizzes(self): """ Test get_quizzes method of Courses""" self.assertSequenceEqual(self.course.get_quizzes(), [self.quiz1, self.quiz2]) + + 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]) diff --git a/yaksh/urls.py b/yaksh/urls.py index 9554750..7f42faf 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -84,7 +84,7 @@ urlpatterns += [ url(r'manage/enroll/rejected/(?P<course_id>\d+)/$', views.enroll, {'was_rejected': True}), url(r'manage/enrolled/reject/(?P<course_id>\d+)/$', - views.reject, {'was_enrolled': True}) + views.reject, {'was_enrolled': True}), url(r'^manage/searchteacher/(?P<course_id>\d+)/$', views.search_teacher), url(r'^manage/addteacher/(?P<course_id>\d+)/$', views.add_teacher), url(r'^manage/viewcourse/$', views.view_courses) diff --git a/yaksh/views.py b/yaksh/views.py index 245713f..a56182b 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -14,7 +14,6 @@ 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 @@ -609,9 +608,8 @@ def course_detail(request, course_id): ci = RequestContext(request) if not is_moderator(user): raise Http404('You are not allowed to view this page') - course = get_object_or_404(Course, creator=user, pk=course_id) - return my_render_to_response('yaksh/course_detail.html', {'course': course}, - context_instance=ci) + course = Course.objects.filter(Q(creator=user)|Q(teachers=user), pk=course_id).first() + return my_render_to_response('yaksh/course_detail.html', {'course': course}) @login_required @@ -620,7 +618,7 @@ 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 = get_object_or_404(Course, creator=user, pk=course_id) + course = Course.objects.filter(Q(creator=user)|Q(teachers=user), pk=course_id).first() if request.method == 'POST': enroll_ids = request.POST.getlist('check') else: @@ -639,7 +637,7 @@ 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 = get_object_or_404(Course, creator=user, pk=course_id) + course = Course.objects.filter(Q(creator=user)|Q(teachers=user), pk=course_id).first() if request.method == 'POST': reject_ids = request.POST.getlist('check') else: @@ -657,7 +655,7 @@ 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)[0] + course = Course.objects.filter(Q(creator=user)|Q(teachers=user), pk=course_id).first() if course.active: course.deactivate() else: @@ -836,8 +834,7 @@ def download_csv(request, questionpaper_id): if not is_moderator(user): raise Http404('You are not allowed to view this page!') quiz = Quiz.objects.get(questionpaper=questionpaper_id) - course = Course.objects.filter(teachers=user, id=quiz.course.id) - if quiz.course.creator != user and not course: + if quiz.course.creator != user and user not in quiz.course.teachers.all(): raise Http404('The question paper does not belong to your course') papers = AnswerPaper.objects.get_latest_attempts(questionpaper_id) if not papers: @@ -984,6 +981,10 @@ def view_profile(request): """ view moderators and users profile """ user = request.user ci = RequestContext(request) + + if not is_moderator(user): + raise Http404('You are not allowed to view this page!') + context = {} if has_profile(user): return my_render_to_response('yaksh/view_profile.html', {'user':user}) @@ -1040,53 +1041,44 @@ def search_teacher(request, course_id): if request.method == 'POST': u_name = request.POST.get('uname') if len(u_name) == 0: - context['success'] = False return my_render_to_response('yaksh/addteacher.html', context, context_instance=ci) else: teachers = User.objects.filter(Q(username__icontains=u_name)| Q(first_name__icontains=u_name)|Q(last_name__icontains=u_name)| - Q(email__icontains=u_name)).exclude(is_superuser=1) + Q(email__icontains=u_name)).exclude(Q(id=user.id)|Q(is_superuser=1)) context['success'] = True context['teachers'] = teachers return my_render_to_response('yaksh/addteacher.html', context, context_instance=ci) else: - context['success'] = False return my_render_to_response('yaksh/addteacher.html', context, context_instance=ci) @login_required def add_teacher(request, course_id): - """ add teachers to the course also add students to - moderator group if a student is allotted to course """ + """ add teachers to the course """ user = request.user ci = RequestContext(request) + + if not is_moderator(user): + raise Http404('You are not allowed to view this page!') + context = {} course = get_object_or_404(Course, creator=user, pk=course_id) context['course'] = course - if not user.is_authenticated() or not is_moderator(user): - raise Http404('You are not allowed to view this page!') if request.method == 'POST': teacher_ids = request.POST.getlist('check') - if not teacher_ids: - return my_render_to_response('yaksh/addteacher.html', context, - context_instance=ci) - else: - teachers = User.objects.filter(id__in=teacher_ids) - course.add_teachers(*teachers) - groupname = Group.objects.get(name='moderator') - groupname.user_set.add(*teachers) - context['status'] = True - for teacher in teachers: - if teacher.profile.position.lower() == "student": - teacher.profile.position = "Faculty" - teacher.profile.save() - return my_render_to_response('yaksh/addteacher.html', context, - context_instance=ci) + teachers = User.objects.filter(id__in=teacher_ids) + teachers_added, teachers_rejected = course.add_teachers(*teachers) + context['teachers_added'] = teachers_added + context['teachers_rejected'] = teachers_rejected + context['status'] = True + return my_render_to_response('yaksh/addteacher.html', context, + context_instance=ci) else: return my_render_to_response('yaksh/addteacher.html', context, context_instance=ci) @@ -1097,6 +1089,9 @@ def view_courses(request): user = request.user ci = RequestContext(request) + if not is_moderator(user): + raise Http404('You are not allowed to view this page!') + courses = Course.objects.filter(teachers=user) return my_render_to_response('yaksh/viewcourse.html', {'courses': courses}, context_instance=ci) |