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/file_utils.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'yaksh/file_utils.py') diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index 6c3fd5d..6b0340f 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -3,6 +3,7 @@ import os import zipfile import tempfile import csv +from django.template import Context, Template def copy_files(file_paths): @@ -66,3 +67,28 @@ def is_csv(document): except (csv.Error, UnicodeDecodeError): return False, None return True, dialect + + +def write_static_files_to_zip(zipfile, course_name, current_dir): + relative_folders = ["css", "js", "images"] + 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"], + "images": ["yaksh_banner.png"]} + for folder in relative_folders: + folder_path = os.sep.join((current_dir, "static", "yaksh", folder)) + for file in static_files[folder]: + file_path = os.sep.join((folder_path, file)) + zipfile.write(file_path, os.sep.join((course_name, "static", + folder, file))) + + +def write_templates_to_zip(zipfile, template_path, data, filename, filepath): + with open(template_path) as f: + template_data = f.read() + template = Template(template_data) + context = Context(data) + render = template.render(context) + zipfile.writestr(os.sep.join((filepath, "{0}.html".format(filename))), + render) -- 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/file_utils.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'yaksh/file_utils.py') diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index 6b0340f..af0ee1e 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -80,8 +80,11 @@ def write_static_files_to_zip(zipfile, course_name, current_dir): folder_path = os.sep.join((current_dir, "static", "yaksh", folder)) for file in static_files[folder]: file_path = os.sep.join((folder_path, file)) - zipfile.write(file_path, os.sep.join((course_name, "static", - folder, file))) + with open(file_path, "rb") as f: + zipfile.writestr( + os.sep.join((course_name, "static", folder, file)), + f.read() + ) def write_templates_to_zip(zipfile, template_path, data, filename, filepath): @@ -91,4 +94,4 @@ def write_templates_to_zip(zipfile, template_path, data, filename, filepath): context = Context(data) render = template.render(context) zipfile.writestr(os.sep.join((filepath, "{0}.html".format(filename))), - render) + str(render)) -- cgit From 5bd270e21bfdf08bd73940bf471ebcf566fb9de6 Mon Sep 17 00:00:00 2001 From: adityacp Date: Tue, 10 Jul 2018 13:48:54 +0530 Subject: Changes in file_utils.py, courses.html, index.html, module.html, unit.html, offline.css - Remove unnecessary list from write_templates_to_zip function in file_utils - Remove styling from index.html, module.html, unit.html - Add offline.css for styling in index.html, module.html, unit.html --- yaksh/file_utils.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'yaksh/file_utils.py') diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index af0ee1e..8a94260 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -70,13 +70,12 @@ def is_csv(document): def write_static_files_to_zip(zipfile, course_name, current_dir): - relative_folders = ["css", "js", "images"] 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"], + "video-js.css", "offline.css"], "images": ["yaksh_banner.png"]} - for folder in relative_folders: + for folder in static_files.keys(): folder_path = os.sep.join((current_dir, "static", "yaksh", folder)) for file in static_files[folder]: file_path = os.sep.join((folder_path, file)) -- 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/file_utils.py | 54 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 8 deletions(-) (limited to 'yaksh/file_utils.py') diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index 8a94260..88fc46d 100644 --- a/yaksh/file_utils.py +++ b/yaksh/file_utils.py @@ -69,12 +69,25 @@ def is_csv(document): return True, dialect -def write_static_files_to_zip(zipfile, course_name, current_dir): - 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"]} +def write_static_files_to_zip(zipfile, course_name, current_dir, static_files): + """ Write static files to zip + + Parameters + ---------- + + zipfile : Zipfile object + zip file in which the static files need to be added + + course_name : str + Create a folder with course name + + current_dir: str + Path from which the static files will be taken + + static_files: dict + Dictionary containing static folders as keys and static files as + values + """ for folder in static_files.keys(): folder_path = os.sep.join((current_dir, "static", "yaksh", folder)) for file in static_files[folder]: @@ -87,10 +100,35 @@ def write_static_files_to_zip(zipfile, course_name, current_dir): def write_templates_to_zip(zipfile, template_path, data, filename, filepath): + """ Write template files to zip + + Parameters + ---------- + + zipfile : Zipfile object + zip file in which the template files need to be added + + template_path : str + Path from which the template file will be loaded + + data: dict + Dictionary containing context data required for template + + filename: str + Filename with which the template file should be named + + filepath: str + File path in zip where the template will be added + """ + rendered_template = render_template(template_path, data) + zipfile.writestr(os.sep.join((filepath, "{0}.html".format(filename))), + str(rendered_template)) + + +def render_template(template_path, data): with open(template_path) as f: template_data = f.read() template = Template(template_data) context = Context(data) render = template.render(context) - zipfile.writestr(os.sep.join((filepath, "{0}.html".format(filename))), - str(render)) + return render -- cgit