diff options
author | ankitjavalkar | 2021-01-17 01:21:56 +0530 |
---|---|---|
committer | GitHub | 2021-01-17 01:21:56 +0530 |
commit | 9b9ebb227bbaafca3daf5485c3bbe0d948d3e843 (patch) | |
tree | 9ae8e5ea5b08c0baf9fd84621055afdc3c8cd64a /yaksh | |
parent | b98d41e77d0abdd6cc5a2fb0cf89fe0cbc0fb985 (diff) | |
parent | 3966ae20fc68041721f97c45ff92102ed5729aee (diff) | |
download | online_test-9b9ebb227bbaafca3daf5485c3bbe0d948d3e843.tar.gz online_test-9b9ebb227bbaafca3daf5485c3bbe0d948d3e843.tar.bz2 online_test-9b9ebb227bbaafca3daf5485c3bbe0d948d3e843.zip |
Merge pull request #804 from ankitjavalkar/crs-upload
Course upload and download with MD file formats
Diffstat (limited to 'yaksh')
-rw-r--r-- | yaksh/models.py | 48 | ||||
-rw-r--r-- | yaksh/templates/yaksh/course_detail.html | 2 | ||||
-rw-r--r-- | yaksh/templates/yaksh/course_detail_options.html | 5 | ||||
-rw-r--r-- | yaksh/templates/yaksh/upload_download_course_md.html | 13 | ||||
-rw-r--r-- | yaksh/urls.py | 2 | ||||
-rw-r--r-- | yaksh/views.py | 41 |
6 files changed, 108 insertions, 3 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index f8458f3..a475493 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -1380,7 +1380,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": [ @@ -2860,6 +2860,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') @@ -3008,6 +3019,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()}" @@ -3015,7 +3059,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 %} <div class="jumbotron"> <h1 class="display-4">Manage Course</h1> 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 </a> </li> + <li class="nav-item"> + <a class="nav-link list-group-item {% if is_upload_download_md %} active {% endif %}" href="{% url 'yaksh:upload_download_course_md' course.id %}" data-toggle="tooltip" title="Upload / Download MD files" data-placement="top"> + Upload / Download MD + </a> + </li> </ul>
\ 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 @@ +<div> + <a href="{% url 'upload:download_course_md' course.id %}"> + <i class="fa fa-download"></i> Download + </a> + <br><br> + <form action="" method="POST" enctype="multipart/form-data"> + {% csrf_token %} + <input type="file" name="course_upload_md" required=""> + <button class="btn btn-outline-success" id="course_upload_md_btn" name="course_upload_md_btn"> + <i class="fa fa-upload"></i> Upload + </button> + </form> +</div>
\ 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/<int:course_id>/<int:questionpaper_id>/', views.upload_marks, name='upload_marks'), + path(r'manage/upload_download_course_md/<int:course_id>', + views.upload_download_course_md, name="upload_download_course_md"), ] diff --git a/yaksh/views.py b/yaksh/views.py index 643b2ae..50f9ded 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 ( @@ -2624,6 +2624,23 @@ def download_sample_toc(request): @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): user = request.user course = Course.objects.get(id=course_id) @@ -4147,3 +4164,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) |