summaryrefslogtreecommitdiff
path: root/yaksh/models.py
diff options
context:
space:
mode:
authorankitjavalkar2020-11-27 15:04:30 +0530
committerankitjavalkar2021-01-06 14:21:06 +0530
commit7cf18e744c9260ebd33f6233d0211a3c0aa3a782 (patch)
tree094b128225886a40c328af8d2103675bd85310f0 /yaksh/models.py
parent30dd519ba7a5277348960a696f3a7cbd91f3f72f (diff)
downloadonline_test-7cf18e744c9260ebd33f6233d0211a3c0aa3a782.tar.gz
online_test-7cf18e744c9260ebd33f6233d0211a3c0aa3a782.tar.bz2
online_test-7cf18e744c9260ebd33f6233d0211a3c0aa3a782.zip
Add a feature to upload and download
Diffstat (limited to 'yaksh/models.py')
-rw-r--r--yaksh/models.py50
1 files changed, 47 insertions, 3 deletions
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}"