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 +++++++++++++++++++++++++++++------
yaksh/forms.py | 2 +-
yaksh/models.py | 13 +++++----
yaksh/static/yaksh/css/lesson.css | 10 +++++++
yaksh/templates/yaksh/add_lesson.html | 3 +-
yaksh/views.py | 9 +++++-
6 files changed, 75 insertions(+), 16 deletions(-)
create mode 100644 yaksh/static/yaksh/css/lesson.css
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
diff --git a/yaksh/forms.py b/yaksh/forms.py
index 66076b1..bfa5566 100644
--- a/yaksh/forms.py
+++ b/yaksh/forms.py
@@ -330,7 +330,7 @@ class LessonForm(forms.ModelForm):
file = self.cleaned_data.get("video_file")
if file:
extension = file.name.split(".")[-1]
- actual_extension = ["mp4", "ogv"]
+ actual_extension = ["mp4", "ogv", "webm"]
if extension not in actual_extension:
raise forms.ValidationError(
"Please upload video files in {0} format".format(
diff --git a/yaksh/models.py b/yaksh/models.py
index 34aee25..464eeb5 100644
--- a/yaksh/models.py
+++ b/yaksh/models.py
@@ -166,9 +166,11 @@ class Lesson(models.Model):
active = models.BooleanField(default=True)
# A video file
- video_file = models.FileField(upload_to=get_file_dir, default=None,
- null=True, blank=True
- )
+ video_file = models.FileField(
+ upload_to=get_file_dir, default=None,
+ null=True, blank=True,
+ help_text="Please upload video files in mp4, ogv, webm format"
+ )
def __str__(self):
return "{0}".format(self.name)
@@ -882,13 +884,14 @@ class Course(models.Model):
def is_student(self, user):
return user in self.students.all()
- def create_zip(self, path):
+ def create_zip(self, path, static_files):
zip_file_name = string_io()
with zipfile.ZipFile(zip_file_name, "a") as zip_file:
course_name = self.name.replace(" ", "_")
modules = self.get_learning_modules()
file_path = os.sep.join((path, "templates", "yaksh", "index.html"))
- write_static_files_to_zip(zip_file, course_name, path)
+ write_static_files_to_zip(zip_file, course_name, path,
+ static_files)
course_data = {"course": self, "modules": modules}
write_templates_to_zip(zip_file, file_path, course_data,
"index", course_name)
diff --git a/yaksh/static/yaksh/css/lesson.css b/yaksh/static/yaksh/css/lesson.css
new file mode 100644
index 0000000..7efb1ff
--- /dev/null
+++ b/yaksh/static/yaksh/css/lesson.css
@@ -0,0 +1,10 @@
+.helptext{
+ font-weight: bold;
+ color: #31708f;
+ background-color: #d9edf7;
+ border-color: #bce8f1;
+ padding: 6px;
+ margin-bottom: 20px;
+ border: 1px solid transparent;
+ border-radius: 4px;
+}
\ No newline at end of file
diff --git a/yaksh/templates/yaksh/add_lesson.html b/yaksh/templates/yaksh/add_lesson.html
index 9feaf30..ebc3ed7 100644
--- a/yaksh/templates/yaksh/add_lesson.html
+++ b/yaksh/templates/yaksh/add_lesson.html
@@ -9,7 +9,8 @@
{% endblock %}
{% block css %}
-
+
+
{% endblock %}
{% block content %}
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