diff options
-rw-r--r-- | yaksh/evaluator_tests/test_simple_question_types.py | 4 | ||||
-rw-r--r-- | yaksh/live_server_tests/load_test.py | 12 | ||||
-rw-r--r-- | yaksh/management/commands/create_moderator.py | 20 | ||||
-rw-r--r-- | yaksh/models.py | 23 | ||||
-rw-r--r-- | yaksh/templatetags/test_custom_filters.py | 5 | ||||
-rw-r--r-- | yaksh/test_models.py | 4 | ||||
-rw-r--r-- | yaksh/test_views.py | 39 | ||||
-rw-r--r-- | yaksh/views.py | 6 |
8 files changed, 89 insertions, 24 deletions
diff --git a/yaksh/evaluator_tests/test_simple_question_types.py b/yaksh/evaluator_tests/test_simple_question_types.py index f7a6cf6..5edd545 100644 --- a/yaksh/evaluator_tests/test_simple_question_types.py +++ b/yaksh/evaluator_tests/test_simple_question_types.py @@ -1,6 +1,7 @@ import unittest from datetime import datetime, timedelta from django.utils import timezone +from django.contrib.auth.models import Group from textwrap import dedent import pytz from yaksh.models import User, Profile, Question, Quiz, QuestionPaper,\ @@ -8,7 +9,9 @@ from yaksh.models import User, Profile, Question, Quiz, QuestionPaper,\ StringTestCase, McqTestCase, ArrangeTestCase + def setUpModule(): + mod_group = Group.objects.create(name='moderator') # Create user profile # Create User 1 user = User.objects.create_user(username='demo_user_100', @@ -46,6 +49,7 @@ def setUpModule(): def tearDownModule(): User.objects.filter(username__in=["demo_user_100", "demo_user_101"])\ .delete() + Group.objects.all().delete() class IntegerQuestionTestCases(unittest.TestCase): diff --git a/yaksh/live_server_tests/load_test.py b/yaksh/live_server_tests/load_test.py index 520bebe..e5ac068 100644 --- a/yaksh/live_server_tests/load_test.py +++ b/yaksh/live_server_tests/load_test.py @@ -1,8 +1,10 @@ from threading import Thread from django.contrib.staticfiles.testing import StaticLiveServerTestCase +from django.contrib.auth.models import User, Group, Permission +from django.contrib.contenttypes.models import ContentType # Local imports -from yaksh.models import User, Profile, Course +from yaksh.models import User, Profile, Course, create_group from yaksh.code_server import ServerPool from yaksh import settings from .selenium_test import SeleniumTest @@ -26,6 +28,10 @@ class YakshSeleniumTests(StaticLiveServerTestCase): cls.code_server_thread = t = Thread(target=code_server_pool.run) t.start() + app_label = 'yaksh' + group_name = 'moderator' + cls.group = create_group(group_name, app_label) + cls.demo_student = User.objects.create_user( username='demo_student', password='demo_student', @@ -45,7 +51,8 @@ class YakshSeleniumTests(StaticLiveServerTestCase): cls.demo_mod_profile = Profile.objects.create( user=cls.demo_mod, roll_number=0, institute='IIT', - department='Chemical', position='Moderator' + department='Chemical', position='Moderator', + is_moderator=True ) course_obj = Course() @@ -61,6 +68,7 @@ class YakshSeleniumTests(StaticLiveServerTestCase): cls.demo_mod.delete() cls.demo_mod_profile.delete() cls.demo_course.delete() + cls.group.delete() cls.code_server_pool.stop() cls.code_server_thread.join() diff --git a/yaksh/management/commands/create_moderator.py b/yaksh/management/commands/create_moderator.py index 86489d5..96276b5 100644 --- a/yaksh/management/commands/create_moderator.py +++ b/yaksh/management/commands/create_moderator.py @@ -7,7 +7,9 @@ # django imports from django.core.management.base import BaseCommand, CommandError from django.contrib.auth.models import User, Group, Permission -from django.contrib.contenttypes.models import ContentType + +# local imports +from yaksh.models import create_group class Command(BaseCommand): @@ -19,19 +21,9 @@ class Command(BaseCommand): def handle(self, *args, **options): app_label = 'yaksh' - - try: - group = Group.objects.get(name='moderator') - except Group.DoesNotExist: - group = Group(name='moderator') - group.save() - # Get the models for the given app - content_types = ContentType.objects.filter(app_label=app_label) - # Get list of permissions for the models - permission_list = Permission.objects.filter( - content_type__in=content_types) - group.permissions.add(*permission_list) - group.save() + group_name = 'moderator' + group = create_group(group_name, app_label) + if group and isinstance(group, Group): self.stdout.write('Moderator group added successfully') if options['usernames']: diff --git a/yaksh/models.py b/yaksh/models.py index e9d4947..7e0fbb2 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, Group +from django.contrib.auth.models import User, Group, Permission from django.contrib.contenttypes.models import ContentType from taggit.managers import TaggableManager from django.utils import timezone @@ -92,6 +92,8 @@ test_status = ( FIXTURES_DIR_PATH = os.path.join(settings.BASE_DIR, 'yaksh', 'fixtures') +MOD_GROUP_NAME = 'moderator' + def get_assignment_dir(instance, filename): upload_dir = instance.question_paper.quiz.description.replace(" ", "_") @@ -135,6 +137,21 @@ def get_file_dir(instance, filename): upload_dir = instance.name.replace(" ", "_") return os.sep.join((upload_dir, filename)) +def create_group(group_name, app_label): + try: + group = Group.objects.get(name=group_name) + except Group.DoesNotExist: + group = Group(name=group_name) + group.save() + # Get the models for the given app + content_types = ContentType.objects.filter(app_label=app_label) + # Get list of permissions for the models + permission_list = Permission.objects.filter( + content_type__in=content_types) + group.permissions.add(*permission_list) + group.save() + return group + ############################################################################### class CourseManager(models.Manager): @@ -1001,7 +1018,7 @@ class Profile(models.Model): os.chmod(user_dir, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) return user_dir - def _toggle_moderator_group(self, group_name='moderator'): + def _toggle_moderator_group(self, group_name): group = Group.objects.get(name=group_name) if self.is_moderator: self.user.groups.add(group) @@ -1009,7 +1026,7 @@ class Profile(models.Model): self.user.groups.remove(group) def save(self, *args, **kwargs): - self._toggle_moderator_group() + self._toggle_moderator_group(group_name=MOD_GROUP_NAME) super(Profile, self).save(*args, **kwargs) def __str__(self): diff --git a/yaksh/templatetags/test_custom_filters.py b/yaksh/templatetags/test_custom_filters.py index e8d1d61..eb1f0fb 100644 --- a/yaksh/templatetags/test_custom_filters.py +++ b/yaksh/templatetags/test_custom_filters.py @@ -1,6 +1,7 @@ import unittest from datetime import datetime, timedelta from django.utils import timezone +from django.contrib.auth.models import Group import pytz # local imports @@ -15,6 +16,8 @@ from yaksh.templatetags.custom_filters import (completed, inprogress, def setUpModule(): + mod_group = Group.objects.create(name='moderator') + # Create user profile teacher = User.objects.create_user( username='teacher2000', password='demo', @@ -52,7 +55,7 @@ def setUpModule(): def tearDownModule(): User.objects.get(username="teacher2000").delete() - + Group.objects.all().delete() class CustomFiltersTestCases(unittest.TestCase): diff --git a/yaksh/test_models.py b/yaksh/test_models.py index 403dfb4..deb6654 100644 --- a/yaksh/test_models.py +++ b/yaksh/test_models.py @@ -1,4 +1,5 @@ import unittest +from django.contrib.auth.models import Group from yaksh.models import User, Profile, Question, Quiz, QuestionPaper,\ QuestionSet, AnswerPaper, Answer, Course, StandardTestCase,\ StdIOBasedTestCase, FileUpload, McqTestCase, AssignmentUpload,\ @@ -24,6 +25,8 @@ from yaksh import settings def setUpModule(): + mod_group = Group.objects.create(name='moderator') + # create user profile user = User.objects.create_user(username='creator', password='demo', @@ -111,6 +114,7 @@ def tearDownModule(): LearningUnit.objects.all().delete() LearningModule.objects.all().delete() AnswerPaper.objects.all().delete() + Group.objects.all().delete() ############################################################################### diff --git a/yaksh/test_views.py b/yaksh/test_views.py index b71020e..8a9d892 100644 --- a/yaksh/test_views.py +++ b/yaksh/test_views.py @@ -33,9 +33,11 @@ from yaksh.decorators import user_has_profile class TestUserRegistration(TestCase): def setUp(self): self.client = Client() + self.mod_group = Group.objects.create(name='moderator') def tearDown(self): self.registered_user.delete() + self.mod_group.delete() def test_register_user_post(self): self.client.post( @@ -63,6 +65,7 @@ class TestUserRegistration(TestCase): class TestProfile(TestCase): def setUp(self): self.client = Client() + self.mod_group = Group.objects.create(name='moderator') # Create User without profile self.user1_plaintext_pass = 'demo1' @@ -95,6 +98,7 @@ class TestProfile(TestCase): self.client.logout() self.user1.delete() self.user2.delete() + self.mod_group.delete() def test_user_has_profile_for_user_without_profile(self): """ @@ -302,6 +306,7 @@ class TestProfile(TestCase): class TestStudentDashboard(TestCase): def setUp(self): self.client = Client() + self.mod_group = Group.objects.create(name='moderator') # student self.student_plaintext_pass = 'student' @@ -365,6 +370,7 @@ class TestStudentDashboard(TestCase): self.client.logout() self.user.delete() self.course.delete() + self.mod_group.delete() def test_student_dashboard_denies_anonymous_user(self): """ @@ -562,6 +568,7 @@ class TestMonitor(TestCase): self.new_answer.delete() self.learning_module.delete() self.learning_unit.delete() + self.mod_group.delete() def test_monitor_denies_student(self): """ @@ -960,6 +967,7 @@ class TestDownloadAssignment(TestCase): self.course.delete() self.learning_module.delete() self.learning_unit.delete() + self.mod_group.delete() dir_name = self.quiz.description.replace(" ", "_") file_path = os.sep.join((settings.MEDIA_ROOT, dir_name)) if os.path.exists(file_path): @@ -1100,6 +1108,7 @@ class TestAddQuiz(TestCase): self.quiz.delete() self.exercise.delete() self.course.delete() + self.mod_group.delete() def test_add_quiz_denies_anonymous(self): """ @@ -1405,6 +1414,7 @@ class TestAddTeacher(TestCase): self.quiz.delete() self.pre_req_quiz.delete() self.course.delete() + self.mod_group.delete() def test_add_teacher_denies_anonymous(self): """ @@ -1570,6 +1580,7 @@ class TestRemoveTeacher(TestCase): self.quiz.delete() self.pre_req_quiz.delete() self.course.delete() + self.mod_group.delete() def test_remove_teacher_denies_anonymous(self): """ @@ -1788,6 +1799,7 @@ class TestCourses(TestCase): self.user2.delete() self.student.delete() self.teacher.delete() + self.mod_group.delete() def test_courses_denies_anonymous(self): """ @@ -2173,6 +2185,7 @@ class TestAddCourse(TestCase): self.quiz.delete() self.pre_req_quiz.delete() self.course.delete() + self.mod_group.delete() def test_add_course_denies_anonymous(self): """ @@ -2304,7 +2317,8 @@ class TestCourseDetail(TestCase): institute='IIT', department='Aeronautical', position='Moderator', - timezone='UTC' + timezone='UTC', + is_moderator=True ) # Create Student @@ -2351,6 +2365,7 @@ class TestCourseDetail(TestCase): self.user2.delete() self.student.delete() self.user1_course.delete() + self.mod_group.delete() def test_upload_users_with_correct_csv(self): # Given @@ -2917,6 +2932,7 @@ class TestEnrollRequest(TestCase): self.user2.delete() self.student.delete() self.course.delete() + self.mod_group.delete() def test_enroll_request_denies_anonymous(self): """ @@ -3188,6 +3204,7 @@ class TestSelfEnroll(TestCase): self.user2.delete() self.student.delete() self.course.delete() + self.mod_group.delete() def test_self_enroll_denies_anonymous(self): response = self.client.get( @@ -3321,6 +3338,7 @@ class TestGrader(TestCase): Quiz.objects.all().delete() QuestionPaper.objects.all().delete() AnswerPaper.objects.all().delete() + self.mod_group.delete() def test_grader_denies_anonymous(self): # Given @@ -3470,6 +3488,8 @@ class TestGrader(TestCase): class TestPasswordReset(TestCase): def setUp(self): + self.mod_group = Group.objects.create(name='moderator') + # Create User with profile self.user1_plaintext_pass = 'demo1' self.user1 = User.objects.create_user( @@ -3491,6 +3511,7 @@ class TestPasswordReset(TestCase): def tearDown(self): self.user1.delete() + self.mod_group.delete() def test_password_reset_post(self): """ @@ -3681,6 +3702,7 @@ class TestModeratorDashboard(TestCase): self.question_paper.delete() self.answerpaper.delete() self.new_answer.delete() + self.mod_group.delete() def test_moderator_dashboard_denies_student(self): """ @@ -3767,6 +3789,8 @@ class TestUserLogin(TestCase): def setUp(self): self.client = Client() + self.mod_group = Group.objects.create(name='moderator') + # Create Moderator with profile self.user1_plaintext_pass = 'demo1' self.user1 = User.objects.create_user( @@ -3790,6 +3814,7 @@ class TestUserLogin(TestCase): self.client.logout() settings.IS_DEVELOPMENT = True self.user1.delete() + self.mod_group.delete() def test_successful_user_login(self): """ @@ -3923,6 +3948,7 @@ class TestDownloadCsv(TestCase): self.student.delete() self.quiz.delete() self.course.delete() + self.mod_group.delete() def test_download_csv_denies_student(self): """ @@ -4109,6 +4135,13 @@ class TestShowQuestions(TestCase): yaml_question_2.encode("utf-8") ) + def tearDown(self): + self.client.logout() + User.objects.all().delete() + Profile.objects.all().delete() + Question.objects.all().delete() + Group.objects.all().delete() + def test_show_questions_denies_student(self): """ Check show questions denies student @@ -4459,6 +4492,7 @@ class TestShowStatistics(TestCase): self.question.delete() self.question_paper.delete() self.new_answer.delete() + self.mod_group.delete() def test_show_statistics_denies_student(self): """ @@ -4771,6 +4805,7 @@ class TestQuestionPaper(TestCase): self.question_paper.delete() self.learning_module.delete() self.learning_unit.delete() + self.mod_group.delete() def test_preview_questionpaper_correct(self): self.client.login( @@ -5388,6 +5423,7 @@ class TestLearningModule(TestCase): self.course.delete() self.learning_unit.delete() self.learning_module.delete() + self.mod_group.delete() def test_add_new_module_denies_non_moderator(self): self.client.login( @@ -5776,6 +5812,7 @@ class TestLessons(TestCase): self.learning_module2.delete() self.lesson.delete() self.lesson2.delete() + self.mod_group.delete() def test_edit_lesson_denies_non_moderator(self): """ Student should not be allowed to edit lesson """ diff --git a/yaksh/views.py b/yaksh/views.py index bda2774..ecd7efd 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -30,7 +30,7 @@ from yaksh.code_server import get_result as get_result_from_code_server from yaksh.models import ( Answer, AnswerPaper, AssignmentUpload, Course, FileUpload, Profile, QuestionPaper, QuestionSet, Quiz, Question, TestCase, User, - FIXTURES_DIR_PATH, Lesson, LessonFile, LearningUnit, LearningModule, + FIXTURES_DIR_PATH, MOD_GROUP_NAME, Lesson, LessonFile, LearningUnit, LearningModule, CourseStatus ) from yaksh.forms import ( @@ -63,7 +63,7 @@ def my_render_to_response(request, template, context=None, **kwargs): return render(request, template, context, **kwargs) -def is_moderator(user, group_name='moderator'): +def is_moderator(user, group_name=MOD_GROUP_NAME): """Check if the user is having moderator rights""" try: group = Group.objects.get(name=group_name) @@ -74,7 +74,7 @@ def is_moderator(user, group_name='moderator'): return False -def add_as_moderator(users, group_name='moderator'): +def add_as_moderator(users, group_name=MOD_GROUP_NAME): """ add users to moderator group """ try: group = Group.objects.get(name=group_name) |