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 ++++++++++++++++++++-- yaksh/templates/yaksh/course_detail.html | 2 + yaksh/templates/yaksh/course_detail_options.html | 5 +++ .../templates/yaksh/upload_download_course_md.html | 13 ++++++ yaksh/urls.py | 2 + yaksh/views.py | 41 +++++++++++++++++- 6 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 yaksh/templates/yaksh/upload_download_course_md.html (limited to 'yaksh') 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}" diff --git a/yaksh/templates/yaksh/course_detail.html b/yaksh/templates/yaksh/course_detail.html index 9f75a68..8661aea 100644 --- a/yaksh/templates/yaksh/course_detail.html +++ b/yaksh/templates/yaksh/course_detail.html @@ -55,6 +55,8 @@ {% include "yaksh/addteacher.html" %} {% elif is_teachers %} {% include "yaksh/course_teachers.html" %} + {% elif is_upload_download_md %} + {% include "yaksh/upload_download_course_md.html" %} {% else %}

Manage Course

diff --git a/yaksh/templates/yaksh/course_detail_options.html b/yaksh/templates/yaksh/course_detail_options.html index 84f78ce..f9393ed 100644 --- a/yaksh/templates/yaksh/course_detail_options.html +++ b/yaksh/templates/yaksh/course_detail_options.html @@ -43,4 +43,9 @@ Current Teachers/TAs + \ No newline at end of file diff --git a/yaksh/templates/yaksh/upload_download_course_md.html b/yaksh/templates/yaksh/upload_download_course_md.html new file mode 100644 index 0000000..072ae4c --- /dev/null +++ b/yaksh/templates/yaksh/upload_download_course_md.html @@ -0,0 +1,13 @@ +
+ +  Download + +

+
+ {% csrf_token %} + + +
+
\ No newline at end of file diff --git a/yaksh/urls.py b/yaksh/urls.py index e93d80a..b7d8ff6 100644 --- a/yaksh/urls.py +++ b/yaksh/urls.py @@ -271,4 +271,6 @@ urlpatterns = [ views.download_sample_toc, name='download_sample_toc'), path('manage/upload_marks///', views.upload_marks, name='upload_marks'), + path(r'manage/upload_download_course_md/', + views.upload_download_course_md, name="upload_download_course_md"), ] diff --git a/yaksh/views.py b/yaksh/views.py index 11a77b8..f3e8668 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -40,7 +40,7 @@ from yaksh.models import ( StdIOBasedTestCase, StringTestCase, TestCase, User, get_model_class, FIXTURES_DIR_PATH, MOD_GROUP_NAME, Lesson, LessonFile, LearningUnit, LearningModule, CourseStatus, question_types, Post, Comment, - Topic, TableOfContents, LessonQuizAnswer, MicroManager + Topic, TableOfContents, LessonQuizAnswer, MicroManager, dict_to_yaml ) from stats.models import TrackLesson from yaksh.forms import ( @@ -2619,6 +2619,23 @@ def download_sample_toc(request): return response +@login_required +@email_verified +def download_toc(request, course_id, lesson_id): + user = request.user + tmp_file_path = tempfile.mkdtemp() + yaml_path = os.path.join(tmp_file_path, "lesson_toc.yaml") + TableOfContents.objects.get_all_tocs_as_yaml(course_id, lesson_id, yaml_path) + + with open(yaml_path, 'r') as yml_file: + response = HttpResponse(yml_file.read(), content_type='text/yaml') + response['Content-Disposition'] = ( + 'attachment; filename="lesson_toc.yaml"' + ) + return response + + + @login_required @email_verified def duplicate_course(request, course_id): @@ -4144,3 +4161,25 @@ def _read_marks_csv(request, reader, course, question_paper, question_ids): messages.info(request, 'Updated successfully for user: {0}, question: {1}'.format( username, question.summary)) + + +@login_required +@email_verified +def upload_download_course_md(request, course_id): + course = get_object_or_404(Course, pk=course_id) + if request.method == "POST": + from upload.views import upload_course_md + status, msg = upload_course_md(request) + if status: + messages.success(request, "MD File Successfully uploaded to course") + else: + messages.warning(request, "{0}".format(msg)) + return redirect( + 'yaksh:course_detail', course.id + ) + else: + context = { + 'course': course, + 'is_upload_download_md': True, + } + return my_render_to_response(request, 'yaksh/course_detail.html', context) -- cgit