diff options
Diffstat (limited to 'yaksh')
-rw-r--r-- | yaksh/models.py | 105 |
1 files changed, 101 insertions, 4 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index 6ee02e1..9a3b606 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -4,6 +4,7 @@ from random import sample, shuffle from itertools import islice, cycle from collections import Counter from django.db import models +from django.db.models import Q from django.contrib.auth.models import User from django.forms.models import model_to_dict from django.contrib.contenttypes.models import ContentType @@ -59,6 +60,23 @@ def get_model_class(model): ############################################################################### +class CourseManager(models.Manager): + + def create_trial_course(self,user): + trial_course = self.create(name="trial_course", + enrollment="open", + creator=user, + is_trial=True + ) + was_rejected=False + trial_course.enroll(was_rejected,user) + return trial_course + + def delete_trial_course(self, user): + trial_course = self.filter(creator=user,is_trial=True) + trial_course.delete() + +############################################################################### class Course(models.Model): """ Course for students""" name = models.CharField(max_length=128) @@ -70,7 +88,8 @@ class Course(models.Model): rejected = models.ManyToManyField(User, related_name='rejected') created_on = models.DateTimeField(auto_now_add=True) teachers = models.ManyToManyField(User, related_name='teachers') - + is_trial = models.BooleanField(default=False) + objects = CourseManager() def request(self, *users): self.requests.add(*users) @@ -111,7 +130,7 @@ class Course(models.Model): return True if self.enrollment == enrollment_methods[1][0] else False def get_quizzes(self): - return self.quiz_set.all() + return self.quiz_set.filter(is_trial=False) def activate(self): self.active = True @@ -283,7 +302,40 @@ class Answer(models.Model): ############################################################################### class QuizManager(models.Manager): def get_active_quizzes(self): - return self.filter(active=True) + return self.filter(active=True,is_trial=False) + + def create_trial_quiz(self,trial_course,user): + trial_quiz = self.create(course=trial_course, + duration=1000, + description="trial_quiz", + is_trial=True, + time_between_attempts=0 + ) + return trial_quiz + + def copy_original_quiz(self, original_quiz_id, user, mode): + was_rejected = False + trial_quiz = self.get(id=original_quiz_id) + trial_quiz.course.enroll(was_rejected,user) + trial_quiz.pk = None + trial_quiz.description += "trial_" + trial_quiz.is_trial = True + if mode == "godmode": + trial_quiz.duration = 1000 + trial_quiz.active = True + trial_quiz.start_date_time = datetime.now() + trial_quiz.end_date_time = datetime(2199, 1, 1, 0, 0, 0, 0) + trial_quiz.time_between_attempts = 0 + trial_quiz.save() + return trial_quiz + + def delete_trial_quiz(self, user): + trial_quiz = self.filter(Q(course__creator=user)|Q(course__teachers=user), + is_trial=True + ) + trial_quiz.delete() + + ############################################################################### class Quiz(models.Model): """A quiz that students will participate in. One can think of this @@ -329,6 +381,8 @@ class Quiz(models.Model): time_between_attempts = models.IntegerField("Number of Days",\ choices=days_between_attempts) + is_trial = models.BooleanField(default=False) + objects = QuizManager() class Meta: @@ -340,7 +394,7 @@ class Quiz(models.Model): def has_prerequisite(self): return True if self.prerequisite else False - + def __unicode__(self): desc = self.description or 'Quiz' return '%s: on %s for %d minutes' % (desc, self.start_date_time, @@ -348,6 +402,45 @@ class Quiz(models.Model): ############################################################################### +class QuestionPaperManager(models.Manager): + + def _copy_original_questionpaper(self, original_quiz_id): + trial_questionpaper = self.get(quiz_id = original_quiz_id) + trial_questions = {"fixed_questions": trial_questionpaper\ + .fixed_questions.all(), + "random_questions": trial_questionpaper\ + .random_questions.all() + } + trial_questionpaper.pk = None + trial_questionpaper.save() + return trial_questionpaper, trial_questions + + def _create_trial_questionpaper(self, trial_quiz): + trial_questionpaper=self.create(quiz=trial_quiz, + total_marks=10, + ) + return trial_questionpaper + + def add_details_trial_questionpaper(self, trial_quiz, + original_quiz_id=None, + questions_list=None): + if questions_list is not None and original_quiz_id is None: + trial_questionpaper = self._create_trial_questionpaper(trial_quiz) + trial_questionpaper.fixed_questions.add(*questions_list) + + else: + trial_questionpaper, trial_questions = self._copy_original_questionpaper\ + (original_quiz_id) + trial_questionpaper.quiz = trial_quiz + trial_questionpaper.fixed_questions\ + .add(*trial_questions["fixed_questions"]) + trial_questionpaper.random_questions\ + .add(*trial_questions["random_questions"]) + trial_questionpaper.save() + return trial_questionpaper + + +############################################################################### class QuestionPaper(models.Model): """Question paper stores the detail of the questions.""" @@ -366,6 +459,10 @@ class QuestionPaper(models.Model): # Total marks for the question paper. total_marks = models.FloatField() +# is_trial = models.BooleanField(default=False) + + objects = QuestionPaperManager() + def update_total_marks(self): """ Updates the total marks for the Question Paper""" marks = 0.0 |