From e7c18d17c8b5ed193e4e34bdfe2072951d996530 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Wed, 11 Jul 2018 17:19:56 +0530 Subject: Fix tests --- yaksh/models.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'yaksh/models.py') 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): -- cgit