diff options
author | adityacp | 2018-06-26 16:03:10 +0530 |
---|---|---|
committer | adityacp | 2018-06-26 16:03:10 +0530 |
commit | ddd2981529798b8c59dec33e50ccf6e808f3bc19 (patch) | |
tree | 946b2bf9b4eee1c82188cd438e3342e2fb2cc900 /yaksh | |
parent | a200623e1739cb85c5125b0c5115663489e51633 (diff) | |
download | online_test-ddd2981529798b8c59dec33e50ccf6e808f3bc19.tar.gz online_test-ddd2981529798b8c59dec33e50ccf6e808f3bc19.tar.bz2 online_test-ddd2981529798b8c59dec33e50ccf6e808f3bc19.zip |
Fix course download to support Python 2 and 3
Diffstat (limited to 'yaksh')
-rw-r--r-- | yaksh/file_utils.py | 9 | ||||
-rw-r--r-- | yaksh/models.py | 29 | ||||
-rw-r--r-- | yaksh/test_views.py | 4 | ||||
-rw-r--r-- | yaksh/views.py | 9 |
4 files changed, 29 insertions, 22 deletions
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)) diff --git a/yaksh/models.py b/yaksh/models.py index b97859d..34aee25 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -210,12 +210,12 @@ class Lesson(models.Model): if self.video_file: video_file = os.sep.join((sub_folder_name, os.path.basename( self.video_file.name))) - zip_file.write(self.video_file.path, video_file) + zip_file.writestr(video_file, self.video_file.read()) for lesson_file in lesson_files: if os.path.exists(lesson_file.file.path): filename = os.sep.join((sub_folder_name, os.path.basename( lesson_file.file.name))) - zip_file.write(lesson_file.file.path, filename) + zip_file.writestr(filename, lesson_file.file.read()) unit_file_path = os.sep.join(( path, "templates", "yaksh", "unit.html" )) @@ -228,7 +228,7 @@ class Lesson(models.Model): ############################################################################# class LessonFile(models.Model): lesson = models.ForeignKey(Lesson, related_name="lesson") - file = models.FileField(upload_to=get_file_dir) + file = models.FileField(upload_to=get_file_dir, default=None) def remove(self): if os.path.exists(self.file.path): @@ -882,16 +882,19 @@ class Course(models.Model): def is_student(self, user): return user in self.students.all() - def create_zip(self, zip_file, path): - 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) - course_data = {"course": self, "modules": modules} - write_templates_to_zip(zip_file, file_path, course_data, - "index", course_name) - for module in modules: - module._add_module_to_zip(self, zip_file, path) + def create_zip(self, path): + 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) + course_data = {"course": self, "modules": modules} + write_templates_to_zip(zip_file, file_path, course_data, + "index", course_name) + for module in modules: + module._add_module_to_zip(self, zip_file, path) + return zip_file_name def has_lessons(self): modules = self.get_learning_modules() diff --git a/yaksh/test_views.py b/yaksh/test_views.py index 41f0401..c1511c0 100644 --- a/yaksh/test_views.py +++ b/yaksh/test_views.py @@ -1743,6 +1743,10 @@ class TestCourses(TestCase): self.lesson = Lesson.objects.create( name="demo lesson", description="test description", creator=self.user1) + lesson_file = SimpleUploadedFile("file1.mp4", b"Test") + django_file = File(lesson_file) + self.lesson.video_file.save(lesson_file.name, django_file, + save=True) self.lesson_unit = LearningUnit.objects.create( order=1, type="lesson", lesson=self.lesson) 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 |