diff options
author | King | 2018-07-13 01:44:00 -0700 |
---|---|---|
committer | GitHub | 2018-07-13 01:44:00 -0700 |
commit | f0f4a882a796319f766ff67cd3f8133a04054dfd (patch) | |
tree | a5c8a3ab617107743864f9faacad341d0e74da00 /yaksh/models.py | |
parent | 661c9d82bb680e745cc6b498131a0793b954c436 (diff) | |
parent | 0af47ee9292132ab472e3e0bbae617d77437ff72 (diff) | |
download | online_test-f0f4a882a796319f766ff67cd3f8133a04054dfd.tar.gz online_test-f0f4a882a796319f766ff67cd3f8133a04054dfd.tar.bz2 online_test-f0f4a882a796319f766ff67cd3f8133a04054dfd.zip |
Merge pull request #480 from ankitjavalkar/toggle-mod
[Role based implementation] Allow moderator to switch between student and moderator roles
Diffstat (limited to 'yaksh/models.py')
-rw-r--r-- | yaksh/models.py | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index 1eca721..152289f 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 +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): @@ -982,6 +999,7 @@ class Profile(models.Model): institute = models.CharField(max_length=128) department = models.CharField(max_length=64) position = models.CharField(max_length=64) + is_moderator = models.BooleanField(default=False) timezone = models.CharField( max_length=64, default=pytz.utc.zone, @@ -1000,6 +1018,20 @@ 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): + group = Group.objects.get(name=group_name) + if self.is_moderator: + self.user.groups.add(group) + else: + self.user.groups.remove(group) + + def save(self, *args, **kwargs): + if self.pk is not None: + old_profile = Profile.objects.get(pk=self.pk) + if old_profile.is_moderator != self.is_moderator: + self._toggle_moderator_group(group_name=MOD_GROUP_NAME) + super(Profile, self).save(*args, **kwargs) + def __str__(self): return '%s' % (self.user.get_full_name()) |