diff options
author | adityacp | 2020-09-30 17:02:19 +0530 |
---|---|---|
committer | adityacp | 2020-09-30 17:02:19 +0530 |
commit | 86be5fd441b92a7679eb2b8673bfba2c188ba0ba (patch) | |
tree | df989862240f7027b017a44d6a46c0bfe9443cd7 /yaksh/models.py | |
parent | 2dcaa3847c4faefd14ee88b9a2370e1d83e747f0 (diff) | |
download | online_test-86be5fd441b92a7679eb2b8673bfba2c188ba0ba.tar.gz online_test-86be5fd441b92a7679eb2b8673bfba2c188ba0ba.tar.bz2 online_test-86be5fd441b92a7679eb2b8673bfba2c188ba0ba.zip |
Add table of contents for lesson with yaml upload
Diffstat (limited to 'yaksh/models.py')
-rw-r--r-- | yaksh/models.py | 72 |
1 files changed, 71 insertions, 1 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index 0c5a6f5..3fa4a04 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -10,7 +10,8 @@ from ruamel.yaml.comments import CommentedMap from random import sample from collections import Counter, defaultdict import glob - +import sys +import traceback try: from StringIO import StringIO as string_io except ImportError: @@ -256,6 +257,15 @@ def get_image_dir(instance, filename): )) +def is_valid_time_format(time): + try: + hh, mm, ss = time.split(":") + status = True + except ValueError: + status = False + return status + + ############################################################################### class CourseManager(models.Manager): @@ -2810,6 +2820,66 @@ class TOCManager(models.Manager): answer = attempted_answer.answer return answer, attempted_answer.correct + def add_contents(self, course_id, lesson_id, user, contents): + toc = [] + messages = [] + for content in contents: + name = content.get('name') or content.get('summary') + if "content_type" not in content or "time" not in content: + messages.append( + (False, + f"content_type or time key is missing in {name}") + ) + else: + content_type = content.pop('content_type') + time = content.pop('time') + if not is_valid_time_format(time): + messages.append( + (False, + 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 + )) + messages.append((True, f"{topic.name} added successfully")) + else: + content['user'] = user + test_cases = content.pop("testcase") + que_type = content.get('type') + if "files" in content: + content.pop("files") + if "tags" in content: + content.pop("tags") + if (que_type in ['code', 'upload']): + messages.append( + (False, f"{que_type} question is not allowed. " + f"{content.get('summary')} is not added") + ) + else: + que = Question.objects.create(**content) + for test_case in test_cases: + 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 + ) + toc.append(TableOfContents( + course_id=course_id, lesson_id=lesson_id, + time=time, content_object=que, + content=content_type + )) + messages.append( + (True, f"{que.summary} added successfully") + ) + if toc: + TableOfContents.objects.bulk_create(toc) + return messages + class TableOfContents(models.Model): toc_types = ((1, "Topic"), (2, "Graded Quiz"), (3, "Exercise"), (4, "Poll")) |