From 7cf18e744c9260ebd33f6233d0211a3c0aa3a782 Mon Sep 17 00:00:00 2001 From: ankitjavalkar Date: Fri, 27 Nov 2020 15:04:30 +0530 Subject: Add a feature to upload and download --- yaksh/models.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) (limited to 'yaksh/models.py') diff --git a/yaksh/models.py b/yaksh/models.py index a29e910..bdac927 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -1381,7 +1381,7 @@ class Question(models.Model): # Solution for the question. solution = models.TextField(blank=True) - content = GenericRelation("TableOfContents") + content = GenericRelation("TableOfContents", related_query_name='questions') tc_code_types = { "python": [ @@ -2823,6 +2823,17 @@ class TOCManager(models.Manager): "student_id", flat=True).distinct().count() return data + def get_all_tocs_as_yaml(self, course_id, lesson_id, file_path): + all_tocs = TableOfContents.objects.filter( + course_id=course_id, lesson_id=lesson_id, + ) + if not all_tocs.exists(): + return None + for toc in all_tocs: + 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 +2940,7 @@ 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('type') model_class = get_model_class(test_case_type) model_class.objects.get_or_create( question=que, **test_case, type=test_case_type @@ -2971,6 +2982,39 @@ class TableOfContents(models.Model): content_name = self.content_object.summary return content_name + def get_toc_as_yaml(self, file_path): + data = {'content_type': self.content, 'time': self.time} + if self.topics.exists(): + content = self.topics.first() + data.update( + { + 'name': content.name, + 'description': content.description, + } + ) + elif self.questions.exists(): + content = self.questions.first() + tc_data = [] + for tc in content.get_test_cases(): + _tc_as_dict = model_to_dict( + tc, exclude=['id', 'testcase_ptr', 'question'], + ) + tc_data.append(_tc_as_dict) + data.update( + { + 'summary': content.summary, + 'type': content.type, + 'language': content.language, + 'description': content.description, + 'points': content.points, + 'testcase': tc_data, + } + ) + yaml_block = dict_to_yaml(data) + with open(file_path, "a") as yaml_file: + yaml_file.write(yaml_block) + return yaml_file + def __str__(self): return f"TOC for {self.lesson.name} with {self.get_content_display()}" @@ -2978,7 +3022,7 @@ class TableOfContents(models.Model): class Topic(models.Model): name = models.CharField(max_length=255) description = models.TextField(null=True, blank=True) - content = GenericRelation(TableOfContents) + content = GenericRelation(TableOfContents, related_query_name='topics') def __str__(self): return f"{self.name}" -- cgit