diff options
author | King | 2018-07-12 07:00:27 -0700 |
---|---|---|
committer | GitHub | 2018-07-12 07:00:27 -0700 |
commit | 550d41f4ead0a7142e99f57096caef9555006fc0 (patch) | |
tree | 8ca9e928517964f74536deb1fc304114e19c5b6b /yaksh/file_utils.py | |
parent | a850749942d163f9aedb19a1d19c4b8d7016745d (diff) | |
parent | 7b268a2daf288fec1163d7f2322c4eb7575da78d (diff) | |
download | online_test-550d41f4ead0a7142e99f57096caef9555006fc0.tar.gz online_test-550d41f4ead0a7142e99f57096caef9555006fc0.tar.bz2 online_test-550d41f4ead0a7142e99f57096caef9555006fc0.zip |
Merge pull request #487 from adityacp/video_lessons_download
Download course content for offline view
Diffstat (limited to 'yaksh/file_utils.py')
-rw-r--r-- | yaksh/file_utils.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/yaksh/file_utils.py b/yaksh/file_utils.py index 6c3fd5d..88fc46d 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,68 @@ 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, 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]: + file_path = os.sep.join((folder_path, 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): + """ 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) + return render |