diff options
author | adityacp | 2020-02-28 10:37:07 +0530 |
---|---|---|
committer | adityacp | 2020-02-28 10:37:07 +0530 |
commit | 78cc032340a8f0c8e0d456770783f2c894935c04 (patch) | |
tree | 3b6af04c555e86c99b7e8fa5a5442765402e9af3 /yaksh/models.py | |
parent | 0d189e0156741685e6f6f1c605bf99b44d082b0b (diff) | |
download | online_test-78cc032340a8f0c8e0d456770783f2c894935c04.tar.gz online_test-78cc032340a8f0c8e0d456770783f2c894935c04.tar.bz2 online_test-78cc032340a8f0c8e0d456770783f2c894935c04.zip |
Bump django to the latest version
Diffstat (limited to 'yaksh/models.py')
-rw-r--r-- | yaksh/models.py | 91 |
1 files changed, 54 insertions, 37 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index 3894165..2ebfa38 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -258,7 +258,7 @@ class Lesson(models.Model): html_data = models.TextField(null=True, blank=True) # Creator of the lesson - creator = models.ForeignKey(User) + creator = models.ForeignKey(User, on_delete=models.CASCADE) # Activate/Deactivate Lesson active = models.BooleanField(default=True) @@ -329,7 +329,8 @@ class Lesson(models.Model): ############################################################################# class LessonFile(models.Model): - lesson = models.ForeignKey(Lesson, related_name="lesson") + lesson = models.ForeignKey(Lesson, related_name="lesson", + on_delete=models.CASCADE) file = models.FileField(upload_to=get_file_dir, default=None) def remove(self): @@ -477,7 +478,7 @@ class Quiz(models.Model): is_exercise = models.BooleanField(default=False) - creator = models.ForeignKey(User, null=True) + creator = models.ForeignKey(User, null=True, on_delete=models.CASCADE) objects = QuizManager() @@ -534,7 +535,7 @@ class Quiz(models.Model): except QuestionPaper.DoesNotExist: qp = None ans_ppr = AnswerPaper.objects.filter( - user=user, course=course, question_paper=qp + user_id=user.id, course_id=course.id, question_paper_id=qp ).order_by("-attempt_number") if ans_ppr.exists(): status = ans_ppr.first().status @@ -548,7 +549,7 @@ class Quiz(models.Model): except QuestionPaper.DoesNotExist: qp = None ans_ppr = AnswerPaper.objects.filter( - user=user, course=course, question_paper=qp + user_id=user.id, course_id=course.id, question_paper_id=qp ).order_by("-attempt_number") if ans_ppr.exists(): return any([paper.passed for paper in ans_ppr]) @@ -593,8 +594,10 @@ class LearningUnit(models.Model): """ Maintain order of lesson and quiz added in the course """ order = models.IntegerField() type = models.CharField(max_length=16) - lesson = models.ForeignKey(Lesson, null=True, blank=True) - quiz = models.ForeignKey(Quiz, null=True, blank=True) + lesson = models.ForeignKey(Lesson, null=True, blank=True, + on_delete=models.CASCADE) + quiz = models.ForeignKey(Quiz, null=True, blank=True, + on_delete=models.CASCADE) check_prerequisite = models.BooleanField(default=True) def get_lesson_or_quiz(self): @@ -607,10 +610,12 @@ class LearningUnit(models.Model): self.check_prerequisite = True def get_completion_status(self, user, course): - course_status = CourseStatus.objects.filter(user=user, course=course) + course_status = CourseStatus.objects.filter( + user_id=user.id, course_id=course.id + ) state = "not attempted" if course_status.exists(): - if self in course_status.first().completed_units.all(): + if course_status.first().completed_units.filter(id=self.id): state = "completed" elif self.type == "quiz": state = self.quiz.get_answerpaper_status(user, course) @@ -657,7 +662,8 @@ class LearningModule(models.Model): name = models.CharField(max_length=255) description = models.TextField(default=None, null=True, blank=True) order = models.IntegerField(default=0) - creator = models.ForeignKey(User, related_name="module_creator") + creator = models.ForeignKey(User, related_name="module_creator", + on_delete=models.CASCADE) check_prerequisite = models.BooleanField(default=True) check_prerequisite_passes = models.BooleanField(default=False) html_data = models.TextField(null=True, blank=True) @@ -834,7 +840,8 @@ class Course(models.Model): active = models.BooleanField(default=True) code = models.CharField(max_length=128, null=True, blank=True) hidden = models.BooleanField(default=False) - creator = models.ForeignKey(User, related_name='creator') + creator = models.ForeignKey(User, related_name='creator', + on_delete=models.CASCADE) students = models.ManyToManyField(User, related_name='students') requests = models.ManyToManyField(User, related_name='requests') rejected = models.ManyToManyField(User, related_name='rejected') @@ -863,7 +870,8 @@ class Course(models.Model): null=True ) - grading_system = models.ForeignKey(GradingSystem, null=True, blank=True) + grading_system = models.ForeignKey(GradingSystem, null=True, blank=True, + on_delete=models.CASCADE) objects = CourseManager() @@ -1117,9 +1125,10 @@ class CourseStatus(models.Model): completed_units = models.ManyToManyField(LearningUnit, related_name="completed_units") current_unit = models.ForeignKey(LearningUnit, related_name="current_unit", - null=True, blank=True) - course = models.ForeignKey(Course) - user = models.ForeignKey(User) + null=True, blank=True, + on_delete=models.CASCADE) + course = models.ForeignKey(Course, on_delete=models.CASCADE) + user = models.ForeignKey(User, on_delete=models.CASCADE) grade = models.CharField(max_length=255, null=True, blank=True) percentage = models.FloatField(default=0.0) percent_completed = models.IntegerField(default=0) @@ -1131,7 +1140,7 @@ class CourseStatus(models.Model): if self.is_course_complete(): self.calculate_percentage() if self.course.grading_system is None: - grading_system = GradingSystem.objects.get(name='default') + grading_system = GradingSystem.objects.get(name__contains='default') else: grading_system = self.course.grading_system grade = grading_system.get_grade(self.percentage) @@ -1173,14 +1182,14 @@ class CourseStatus(models.Model): ############################################################################### class ConcurrentUser(models.Model): - concurrent_user = models.OneToOneField(User) + concurrent_user = models.OneToOneField(User, on_delete=models.CASCADE) session_key = models.CharField(max_length=40) ############################################################################### class Profile(models.Model): """Profile for a user to store roll number and other details.""" - user = models.OneToOneField(User) + user = models.OneToOneField(User, on_delete=models.CASCADE) roll_number = models.CharField(max_length=20) institute = models.CharField(max_length=128) department = models.CharField(max_length=64) @@ -1258,7 +1267,8 @@ class Question(models.Model): snippet = models.TextField(blank=True) # user for particular question - user = models.ForeignKey(User, related_name="user") + user = models.ForeignKey(User, related_name="user", + on_delete=models.CASCADE) # Does this question allow partial grading partial_grading = models.BooleanField(default=False) @@ -1533,7 +1543,8 @@ class Question(models.Model): ############################################################################### class FileUpload(models.Model): file = models.FileField(upload_to=get_upload_dir, blank=True) - question = models.ForeignKey(Question, related_name="question") + question = models.ForeignKey(Question, related_name="question", + on_delete=models.CASCADE) extract = models.BooleanField(default=False) hide = models.BooleanField(default=False) @@ -1567,7 +1578,7 @@ class Answer(models.Model): """Answers submitted by the users.""" # The question for which user answers. - question = models.ForeignKey(Question) + question = models.ForeignKey(Question, on_delete=models.CASCADE) # The answer submitted by the user. answer = models.TextField(null=True, blank=True) @@ -1642,7 +1653,7 @@ class QuestionPaper(models.Model): """Question paper stores the detail of the questions.""" # Question paper belongs to a particular quiz. - quiz = models.ForeignKey(Quiz) + quiz = models.ForeignKey(Quiz, on_delete=models.CASCADE) # Questions that will be mandatory in the quiz. fixed_questions = models.ManyToManyField(Question) @@ -1928,10 +1939,10 @@ class AnswerPaperManager(models.Manager): def _get_answerpapers_for_quiz(self, questionpaper_id, course_id, status=False): if not status: - return self.filter(question_paper_id=questionpaper_id, + return self.filter(question_paper_id__in=questionpaper_id, course_id=course_id) else: - return self.filter(question_paper_id=questionpaper_id, + return self.filter(question_paper_id__in=questionpaper_id, course_id=course_id, status="completed") @@ -1973,14 +1984,15 @@ class AnswerPaperManager(models.Manager): .distinct() def get_user_all_attempts(self, questionpaper, user, course_id): - return self.filter(question_paper=questionpaper, user=user, + return self.filter(question_paper_id__in=questionpaper, user_id=user, course_id=course_id)\ .order_by('-attempt_number') def get_user_data(self, user, questionpaper_id, course_id, attempt_number=None): if attempt_number is not None: - papers = self.filter(user=user, question_paper_id=questionpaper_id, + papers = self.filter(user_id=user.id, + question_paper_id__in=questionpaper_id, course_id=course_id, attempt_number=attempt_number) else: @@ -1998,7 +2010,8 @@ class AnswerPaperManager(models.Manager): def get_user_best_of_attempts_marks(self, quiz, user_id, course_id): best_attempt = 0.0 - papers = self.filter(question_paper__quiz=quiz, course_id=course_id, + papers = self.filter(question_paper__quiz_id=quiz.id, + course_id=course_id, user=user_id).values("marks_obtained") if papers: best_attempt = max([marks["marks_obtained"] for marks in papers]) @@ -2010,15 +2023,15 @@ class AnswerPaper(models.Model): """A answer paper for a student -- one per student typically. """ # The user taking this question paper. - user = models.ForeignKey(User) + user = models.ForeignKey(User, on_delete=models.CASCADE) questions = models.ManyToManyField(Question, related_name='questions') # The Quiz to which this question paper is attached to. - question_paper = models.ForeignKey(QuestionPaper) + question_paper = models.ForeignKey(QuestionPaper, on_delete=models.CASCADE) # Answepaper will be unique to the course - course = models.ForeignKey(Course, null=True) + course = models.ForeignKey(Course, null=True, on_delete=models.CASCADE) # The attempt number for the question paper. attempt_number = models.IntegerField() @@ -2438,17 +2451,20 @@ class AssignmentUploadManager(models.Manager): ############################################################################## class AssignmentUpload(models.Model): - user = models.ForeignKey(User) - assignmentQuestion = models.ForeignKey(Question) + user = models.ForeignKey(User, on_delete=models.CASCADE) + assignmentQuestion = models.ForeignKey(Question, on_delete=models.CASCADE) assignmentFile = models.FileField(upload_to=get_assignment_dir) - question_paper = models.ForeignKey(QuestionPaper, blank=True, null=True) - course = models.ForeignKey(Course, null=True, blank=True) + question_paper = models.ForeignKey(QuestionPaper, blank=True, null=True, + on_delete=models.CASCADE) + course = models.ForeignKey(Course, null=True, blank=True, + on_delete=models.CASCADE) objects = AssignmentUploadManager() ############################################################################## class TestCase(models.Model): - question = models.ForeignKey(Question, blank=True, null=True) + question = models.ForeignKey(Question, blank=True, null=True, + on_delete=models.CASCADE) type = models.CharField(max_length=24, choices=test_case_types, null=True) @@ -2585,10 +2601,11 @@ class TestCaseOrder(models.Model): """ # Answerpaper of the user. - answer_paper = models.ForeignKey(AnswerPaper, related_name="answer_paper") + answer_paper = models.ForeignKey(AnswerPaper, related_name="answer_paper", + on_delete=models.CASCADE) # Question in an answerpaper. - question = models.ForeignKey(Question) + question = models.ForeignKey(Question, on_delete=models.CASCADE) # Order of the test case for a question. order = models.TextField() |