From 9a2717b662c7dd3fca54e8b8f2e526454ad72d83 Mon Sep 17 00:00:00 2001 From: CruiseDevice Date: Fri, 19 Feb 2021 15:05:42 +0530 Subject: Fix upload assignment issue for multiple attempts --- yaksh/models.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'yaksh/models.py') diff --git a/yaksh/models.py b/yaksh/models.py index 11ddf8a..6a745d1 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -122,7 +122,7 @@ MOD_GROUP_NAME = 'moderator' def get_assignment_dir(instance, filename): folder_name = instance.course.name.replace(" ", "_") - sub_folder_name = instance.question_paper.quiz.description.replace( + sub_folder_name = instance.answer_paper.question_paper.quiz.description.replace( " ", "_") return os.sep.join((folder_name, sub_folder_name, instance.user.username, str(instance.assignmentQuestion.id), @@ -2661,9 +2661,10 @@ class AssignmentUploadManager(models.Manager): class AssignmentUpload(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) assignmentQuestion = models.ForeignKey(Question, on_delete=models.CASCADE) - assignmentFile = models.FileField(upload_to=get_assignment_dir, max_length=255) - question_paper = models.ForeignKey(QuestionPaper, blank=True, null=True, - on_delete=models.CASCADE) + assignmentFile = models.FileField(upload_to=get_assignment_dir, + max_length=255) + answer_paper = models.ForeignKey(AnswerPaper, blank=True, null=True, + on_delete=models.CASCADE) course = models.ForeignKey(Course, null=True, blank=True, on_delete=models.CASCADE) objects = AssignmentUploadManager() -- cgit From 3e23a631e9c8e25fd13c7f1f8b0fed54d0c34b8f Mon Sep 17 00:00:00 2001 From: CruiseDevice Date: Fri, 26 Feb 2021 08:37:27 +0530 Subject: Fix assignment download on grade user interface --- yaksh/models.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'yaksh/models.py') diff --git a/yaksh/models.py b/yaksh/models.py index 6a745d1..b80f98c 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -121,10 +121,10 @@ MOD_GROUP_NAME = 'moderator' def get_assignment_dir(instance, filename): - folder_name = instance.course.name.replace(" ", "_") - sub_folder_name = instance.answer_paper.question_paper.quiz.description.replace( - " ", "_") - return os.sep.join((folder_name, sub_folder_name, instance.user.username, + folder = str(instance.answer_paper.course.id) + sub_folder = str(instance.answer_paper.question_paper.quiz.id) + user = instance.answer_paper.user.username + return os.sep.join((folder, sub_folder, user, str(instance.assignmentQuestion.id), filename )) @@ -1439,7 +1439,7 @@ class Question(models.Model): for file in files] if self.type == "upload": assignment_files = AssignmentUpload.objects.filter( - assignmentQuestion=self, user=user + assignmentQuestion=self ) if assignment_files: metadata['assign_files'] = [(file.assignmentFile.path, False) @@ -2642,16 +2642,16 @@ class AssignmentUploadManager(models.Manager): def get_assignments(self, qp, que_id=None, user_id=None, course_id=None): if que_id and user_id: assignment_files = AssignmentUpload.objects.filter( - assignmentQuestion_id=que_id, user_id=user_id, - question_paper=qp, course_id=course_id + assignmentQuestion_id=que_id, answer_paper__user_id=user_id, + answer_paper__question_paper=qp, answer_paper__course_id=course_id ) file_name = User.objects.get(id=user_id).get_full_name() else: assignment_files = AssignmentUpload.objects.filter( - question_paper=qp, course_id=course_id + answer_paper__question_paper=qp, answer_paper__course_id=course_id ) file_name = "{0}_Assignment_files".format( - assignment_files[0].course.name + assignment_files[0].answer_paper.course.name ) return assignment_files, file_name @@ -2659,16 +2659,18 @@ class AssignmentUploadManager(models.Manager): ############################################################################## class AssignmentUpload(models.Model): - user = models.ForeignKey(User, on_delete=models.CASCADE) assignmentQuestion = models.ForeignKey(Question, on_delete=models.CASCADE) assignmentFile = models.FileField(upload_to=get_assignment_dir, max_length=255) answer_paper = models.ForeignKey(AnswerPaper, blank=True, null=True, on_delete=models.CASCADE) - course = models.ForeignKey(Course, null=True, blank=True, - on_delete=models.CASCADE) + upload_date = models.DateTimeField(auto_now=True) + objects = AssignmentUploadManager() + def __str__(self): + return f'Assignment File of the user {self.answer_paper.user}' + ############################################################################## class TestCase(models.Model): -- cgit From 2d46e6799672cc133829ab72a88c645e54087d98 Mon Sep 17 00:00:00 2001 From: CruiseDevice Date: Fri, 26 Feb 2021 12:54:37 +0530 Subject: Resolve comments --- yaksh/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'yaksh/models.py') diff --git a/yaksh/models.py b/yaksh/models.py index b80f98c..ca0f2d8 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -121,8 +121,8 @@ MOD_GROUP_NAME = 'moderator' def get_assignment_dir(instance, filename): - folder = str(instance.answer_paper.course.id) - sub_folder = str(instance.answer_paper.question_paper.quiz.id) + folder = str(instance.answer_paper.course_id) + sub_folder = str(instance.answer_paper.question_paper.quiz_id) user = instance.answer_paper.user.username return os.sep.join((folder, sub_folder, user, str(instance.assignmentQuestion.id), -- cgit From 085b1fae43a16cff87e1ab0cf3730e84d4943442 Mon Sep 17 00:00:00 2001 From: CruiseDevice Date: Wed, 3 Mar 2021 23:55:24 +0530 Subject: Fix failing test --- yaksh/models.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'yaksh/models.py') diff --git a/yaksh/models.py b/yaksh/models.py index ca0f2d8..30c69c3 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -121,15 +121,16 @@ MOD_GROUP_NAME = 'moderator' def get_assignment_dir(instance, filename): - folder = str(instance.answer_paper.course_id) - sub_folder = str(instance.answer_paper.question_paper.quiz_id) + course = instance.answer_paper.course + quiz = instance.answer_paper.question_paper.quiz + folder = f'{course.name.replace(" ", "_")}_{course.id}' + sub_folder = f'{quiz.description.replace(" ","_")}_{quiz.id}' user = instance.answer_paper.user.username return os.sep.join((folder, sub_folder, user, str(instance.assignmentQuestion.id), filename )) - def get_model_class(model): ctype = ContentType.objects.get(app_label="yaksh", model=model) model_class = ctype.model_class() -- cgit From ccc578beff8363ea19826d6c621dafabe210531f Mon Sep 17 00:00:00 2001 From: CruiseDevice Date: Thu, 4 Mar 2021 16:49:55 +0530 Subject: Minor Change in get_assignment_dir --- yaksh/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'yaksh/models.py') diff --git a/yaksh/models.py b/yaksh/models.py index 30c69c3..e88a9e4 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -123,8 +123,8 @@ MOD_GROUP_NAME = 'moderator' def get_assignment_dir(instance, filename): course = instance.answer_paper.course quiz = instance.answer_paper.question_paper.quiz - folder = f'{course.name.replace(" ", "_")}_{course.id}' - sub_folder = f'{quiz.description.replace(" ","_")}_{quiz.id}' + folder = f'Course_{course.id}' + sub_folder = f'Quiz_{quiz.id}' user = instance.answer_paper.user.username return os.sep.join((folder, sub_folder, user, str(instance.assignmentQuestion.id), -- cgit From b457c973b491d373ed9c5087b61bf29ce128d766 Mon Sep 17 00:00:00 2001 From: CruiseDevice Date: Mon, 15 Mar 2021 13:40:12 +0530 Subject: Replacing .id with _id in get_assignment_dir() - .id fetches entire model which is unnecessary. _id only fetches the primary key. --- yaksh/models.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'yaksh/models.py') diff --git a/yaksh/models.py b/yaksh/models.py index e88a9e4..4661ea7 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -121,13 +121,13 @@ MOD_GROUP_NAME = 'moderator' def get_assignment_dir(instance, filename): - course = instance.answer_paper.course - quiz = instance.answer_paper.question_paper.quiz - folder = f'Course_{course.id}' - sub_folder = f'Quiz_{quiz.id}' + course_id = instance.answer_paper.course_id + quiz_id = instance.answer_paper.question_paper.quiz_id + folder = f'Course_{course_id}' + sub_folder = f'Quiz_{quiz_id}' user = instance.answer_paper.user.username return os.sep.join((folder, sub_folder, user, - str(instance.assignmentQuestion.id), + str(instance.assignmentQuestion_id), filename )) -- cgit From 1793f445ec0cbc67df4b4cd3151859e01ed4b2a0 Mon Sep 17 00:00:00 2001 From: CruiseDevice Date: Mon, 15 Mar 2021 14:59:55 +0530 Subject: PEP8 fix --- yaksh/models.py | 54 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 18 deletions(-) (limited to 'yaksh/models.py') diff --git a/yaksh/models.py b/yaksh/models.py index 4661ea7..610e984 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -131,6 +131,7 @@ def get_assignment_dir(instance, filename): filename )) + def get_model_class(model): ctype = ContentType.objects.get(app_label="yaksh", model=model) model_class = ctype.model_class() @@ -1381,7 +1382,9 @@ class Question(models.Model): # Solution for the question. solution = models.TextField(blank=True) - content = GenericRelation("TableOfContents", related_query_name='questions') + content = GenericRelation( + "TableOfContents", related_query_name='questions' + ) tc_code_types = { "python": [ @@ -1838,7 +1841,8 @@ class QuestionPaper(models.Model): all_questions = questions return all_questions - def make_answerpaper(self, user, ip, attempt_num, course_id, special=False): + def make_answerpaper(self, + user, ip, attempt_num, course_id, special=False): """Creates an answer paper for the user to attempt the quiz""" try: ans_paper = AnswerPaper.objects.get(user=user, @@ -2035,6 +2039,7 @@ class AnswerPaperManager(models.Manager): ).order_by("id").values( "answerpaper__id", "question_id", "correct", "answer" ) + def _get_per_tc_data(answers, q_type): tc = [] for answer in answers["answer"]: @@ -2047,11 +2052,11 @@ class AnswerPaperManager(models.Manager): df = pd.DataFrame(answers) if not df.empty: for question in all_questions: - que = df[df["question_id"]==question.id].groupby( + que = df[df["question_id"] == question.id].groupby( "answerpaper__id").tail(1) if not que.empty: total_attempts = que.shape[0] - correct_attempts = que[que["correct"]==True].shape[0] + correct_attempts = que[que["correct"] == True].shape[0] per_tc_ans = {} if question.type in ["mcq", "mcc"]: per_tc_ans = _get_per_tc_data(que, question.type) @@ -2643,13 +2648,16 @@ class AssignmentUploadManager(models.Manager): def get_assignments(self, qp, que_id=None, user_id=None, course_id=None): if que_id and user_id: assignment_files = AssignmentUpload.objects.filter( - assignmentQuestion_id=que_id, answer_paper__user_id=user_id, - answer_paper__question_paper=qp, answer_paper__course_id=course_id + assignmentQuestion_id=que_id, + answer_paper__user_id=user_id, + answer_paper__question_paper=qp, + answer_paper__course_id=course_id ) file_name = User.objects.get(id=user_id).get_full_name() else: assignment_files = AssignmentUpload.objects.filter( - answer_paper__question_paper=qp, answer_paper__course_id=course_id + answer_paper__question_paper=qp, + answer_paper__course_id=course_id ) file_name = "{0}_Assignment_files".format( assignment_files[0].answer_paper.course.name @@ -2895,7 +2903,6 @@ class TOCManager(models.Manager): toc.get_toc_as_yaml(file_path) return file_path - def get_question_stats(self, toc_id): answers = LessonQuizAnswer.objects.get_queryset().filter( toc_id=toc_id).order_by('id') @@ -2929,7 +2936,7 @@ class TOCManager(models.Manager): if j not in mydata: mydata[j] = 1 else: - mydata[j] +=1 + mydata[j] += 1 data = mydata.copy() if is_percent: for key, value in data.items(): @@ -2975,17 +2982,20 @@ class TOCManager(models.Manager): if not is_valid_time_format(time): messages.append( (False, - f"Invalid time format in {name}. " + f"Invalid time format in {name}. " "Format should be 00:00:00") - ) + ) else: if content_type == 1: topic = Topic.objects.create(**content) toc.append(TableOfContents( - course_id=course_id, lesson_id=lesson_id, time=time, - content_object=topic, content=content_type + course_id=course_id, + lesson_id=lesson_id, time=time, + content_object=topic, content=content_type )) - messages.append((True, f"{topic.name} added successfully")) + messages.append( + (True, f"{topic.name} added successfully") + ) else: content['user'] = user test_cases = content.pop("testcase") @@ -3002,10 +3012,13 @@ class TOCManager(models.Manager): else: que = Question.objects.create(**content) for test_case in test_cases: - test_case_type = test_case.pop('test_case_type') + test_case_type = test_case.pop( + 'test_case_type' + ) model_class = get_model_class(test_case_type) model_class.objects.get_or_create( - question=que, **test_case, type=test_case_type + question=que, + **test_case, type=test_case_type ) toc.append(TableOfContents( course_id=course_id, lesson_id=lesson_id, @@ -3021,7 +3034,12 @@ class TOCManager(models.Manager): class TableOfContents(models.Model): - toc_types = ((1, "Topic"), (2, "Graded Quiz"), (3, "Exercise"), (4, "Poll")) + toc_types = ( + (1, "Topic"), + (2, "Graded Quiz"), + (3, "Exercise"), + (4, "Poll") + ) course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name='course') lesson = models.ForeignKey(Lesson, on_delete=models.CASCADE, @@ -3039,7 +3057,7 @@ class TableOfContents(models.Model): def get_toc_text(self): if self.content == 1: - content_name = self.content_object.name + content_name = self.content_object.name else: content_name = self.content_object.summary return content_name -- cgit