From 20b692ea468a280e3edb4a9e7f97543b5499025d Mon Sep 17 00:00:00 2001 From: adityacp Date: Tue, 26 Jun 2018 13:13:22 +0530 Subject: Changes in views, models, forms, urls, file_utils - Add new view function to download course content - Add new attribute to Lesson model - Add new model methods to add course, module and lesson content - Add validation in forms to check for lesson video file format - Add functions in file_utils to add static files and templates to the zip file --- yaksh/views.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'yaksh/views.py') diff --git a/yaksh/views.py b/yaksh/views.py index 3341aca..c5c5be1 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -2297,9 +2297,16 @@ def edit_lesson(request, lesson_id=None, course_id=None): context = {} if request.method == "POST": if "Save" in request.POST: - lesson_form = LessonForm(request.POST, instance=lesson) + lesson_form = LessonForm(request.POST, request.FILES, + instance=lesson) lesson_file_form = LessonFileForm(request.POST, request.FILES) lessonfiles = request.FILES.getlist('Lesson_files') + clear = request.POST.get("video_file-clear") + video_file = request.FILES.get("video_file") + if (clear or video_file) and lesson: + # Remove previous video file if new file is uploaded or + # if clear is selected + lesson.remove_file() if lesson_form.is_valid(): if lesson is None: lesson_form.instance.creator = user @@ -2314,6 +2321,7 @@ def edit_lesson(request, lesson_id=None, course_id=None): return my_redirect(redirect_url) else: context['lesson_form'] = lesson_form + context['error'] = lesson_form["video_file"].errors context['lesson_file_form'] = lesson_file_form if 'Delete' in request.POST: @@ -2808,3 +2816,30 @@ def get_user_data(request, course_id, student_id): context = Context(data) data = template.render(context) return HttpResponse(json.dumps({"user_data": data}), **response_kwargs) + + +@login_required +@email_verified +def download_course(request, course_id): + user = request.user + course = get_object_or_404(Course, pk=course_id) + if (not course.is_creator(user) and not course.is_teacher(user) and not + course.is_student(user)): + raise Http404("You are not allowed to download {0} course".format( + course.name)) + if not course.has_lessons(): + raise Http404("{0} course does not have any lessons".format( + course.name)) + file_name = string_io() + current_dir = os.path.dirname(__file__) + course_name = course.name.replace(" ", "_") + zip_file = zipfile.ZipFile(file_name, "w") + course.create_zip(zip_file, current_dir) + zip_file.close() + file_name.seek(0) + response = HttpResponse(content_type='application/zip') + response['Content-Disposition'] = 'attachment; filename={0}.zip'.format( + course_name + ) + response.write(file_name.read()) + return response -- cgit From ddd2981529798b8c59dec33e50ccf6e808f3bc19 Mon Sep 17 00:00:00 2001 From: adityacp Date: Tue, 26 Jun 2018 16:03:10 +0530 Subject: Fix course download to support Python 2 and 3 --- yaksh/views.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'yaksh/views.py') diff --git a/yaksh/views.py b/yaksh/views.py index c5c5be1..f159411 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -2830,16 +2830,13 @@ def download_course(request, course_id): if not course.has_lessons(): raise Http404("{0} course does not have any lessons".format( course.name)) - file_name = string_io() current_dir = os.path.dirname(__file__) course_name = course.name.replace(" ", "_") - zip_file = zipfile.ZipFile(file_name, "w") - course.create_zip(zip_file, current_dir) - zip_file.close() - file_name.seek(0) + zip_file = course.create_zip(current_dir) + zip_file.seek(0) response = HttpResponse(content_type='application/zip') response['Content-Disposition'] = 'attachment; filename={0}.zip'.format( course_name ) - response.write(file_name.read()) + response.write(zip_file.read()) return response -- cgit From c075bc00bc57507f2af4d47f7f2e5e063df6921a Mon Sep 17 00:00:00 2001 From: adityacp Date: Tue, 10 Jul 2018 15:47:46 +0530 Subject: Change in file_utils, forms, models, lesson.css, add_lesson.html, views - Add new function in the file_utils to render template - Add webm as supported video format in forms - Add help text in video_file field - Add lesson.css file for adding custom styling for lessons - Add static_files dict in download_course views function --- yaksh/views.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'yaksh/views.py') diff --git a/yaksh/views.py b/yaksh/views.py index f159411..497816c 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -2832,7 +2832,14 @@ def download_course(request, course_id): course.name)) current_dir = os.path.dirname(__file__) course_name = course.name.replace(" ", "_") - zip_file = course.create_zip(current_dir) + + # Static files required for styling in html template + static_files = {"js": ["bootstrap.js", "bootstrap.min.js", + "jquery-1.9.1.min.js", "video.js"], + "css": ["bootstrap.css", "bootstrap.min.css", + "video-js.css", "offline.css"], + "images": ["yaksh_banner.png"]} + zip_file = course.create_zip(current_dir, static_files) zip_file.seek(0) response = HttpResponse(content_type='application/zip') response['Content-Disposition'] = 'attachment; filename={0}.zip'.format( -- cgit