diff options
author | Palaparthy Adityachandra | 2021-04-15 13:53:43 +0530 |
---|---|---|
committer | GitHub | 2021-04-15 13:53:43 +0530 |
commit | e53b86c23c064d70f86494da503392a598559453 (patch) | |
tree | 553c10fcffcfedc9d724a5250b2552eb946a5a49 | |
parent | 4e86fee70bee44fd7426cc25c6b097980699f398 (diff) | |
parent | 4756f86290426b5af8878500cf1bfd05e81f528e (diff) | |
download | online_test-e53b86c23c064d70f86494da503392a598559453.tar.gz online_test-e53b86c23c064d70f86494da503392a598559453.tar.bz2 online_test-e53b86c23c064d70f86494da503392a598559453.zip |
Merge pull request #837 from adityacp/fix_files_issue
Fix files issue
-rw-r--r-- | yaksh/models.py | 56 | ||||
-rw-r--r-- | yaksh/templates/yaksh/showquestions.html | 2 | ||||
-rw-r--r-- | yaksh/test_views.py | 6 | ||||
-rw-r--r-- | yaksh/views.py | 6 |
4 files changed, 26 insertions, 44 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index aa7c38d..fd48318 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -47,6 +47,7 @@ from django.forms.models import model_to_dict from django.db.models import Count from django.db.models.signals import pre_delete from django.db.models.fields.files import FieldFile +from django.core.files.base import ContentFile # Local Imports from yaksh.code_server import ( submit, get_result as get_result_from_code_server @@ -165,9 +166,9 @@ def dict_to_yaml(dictionary): def get_file_dir(instance, filename): if isinstance(instance, LessonFile): - upload_dir = instance.lesson.name.replace(" ", "_") + upload_dir = f"Lesson_{instance.lesson.id}" else: - upload_dir = instance.name.replace(" ", "_") + upload_dir = f"Lesson_{instance.id}" return os.sep.join((upload_dir, filename)) @@ -332,7 +333,7 @@ class Lesson(models.Model): return "{0}".format(self.name) def get_files(self): - return LessonFile.objects.filter(lesson=self) + return LessonFile.objects.filter(lesson_id=self.id) def _create_lesson_copy(self, user): lesson_files = self.get_files() @@ -341,20 +342,19 @@ class Lesson(models.Model): new_lesson.creator = user new_lesson.save() for _file in lesson_files: - file_name = os.path.basename(_file.file.name) - if os.path.exists(_file.file.path): - lesson_file = open(_file.file.path, "rb") - django_file = File(lesson_file) - lesson_file_obj = LessonFile() - lesson_file_obj.lesson = new_lesson - lesson_file_obj.file.save(file_name, django_file, save=True) + try: + file_name = os.path.basename(_file.file.name) + lesson_file = ContentFile(_file.file.read()) + new_lesson_file = LessonFile() + new_lesson_file.lesson_id=self.id + new_lesson_file.file.save(file_name, lesson_file, save=True) + new_lesson_file.save() + except FileNotFoundError: + pass return new_lesson def remove_file(self): - if self.video_file: - file_path = self.video_file.path - if os.path.exists(file_path): - os.remove(file_path) + self.video_file.delete() def _add_lesson_to_zip(self, next_unit, module, course, zip_file, path): lesson_name = self.name.replace(" ", "_") @@ -383,6 +383,7 @@ class Lesson(models.Model): write_templates_to_zip(zip_file, unit_file_path, lesson_data, lesson_name, sub_folder_name) +pre_delete.connect(file_cleanup, sender=Lesson) ############################################################################# class LessonFile(models.Model): @@ -390,13 +391,7 @@ class LessonFile(models.Model): on_delete=models.CASCADE) file = models.FileField(upload_to=get_file_dir, default=None) - def remove(self): - if os.path.exists(self.file.path): - os.remove(self.file.path) - if os.listdir(os.path.dirname(self.file.path)) == []: - os.rmdir(os.path.dirname(self.file.path)) - self.delete() - +pre_delete.connect(file_cleanup, sender=LessonFile) ############################################################################### class QuizManager(models.Manager): @@ -1596,13 +1591,11 @@ class Question(models.Model): files = FileUpload.objects.filter(question=self) files_list = [] for f in files: - zip_file.write(f.file.path, os.path.join("additional_files", - os.path.basename( - f.file.path - ) - ) - ) - files_list.append(((os.path.basename(f.file.path)), f.extract)) + zip_file.writestr( + os.path.join("additional_files", os.path.basename(f.file.name)), + f.file.read() + ) + files_list.append(((os.path.basename(f.file.name)), f.extract)) return files_list def _add_files_to_db(self, file_names, path): @@ -1695,13 +1688,6 @@ class FileUpload(models.Model): extract = models.BooleanField(default=False) hide = models.BooleanField(default=False) - def remove(self): - if os.path.exists(self.file.path): - os.remove(self.file.path) - if os.listdir(os.path.dirname(self.file.path)) == []: - os.rmdir(os.path.dirname(self.file.path)) - self.delete() - def set_extract_status(self): if self.extract: self.extract = False diff --git a/yaksh/templates/yaksh/showquestions.html b/yaksh/templates/yaksh/showquestions.html index aefb3a6..b9c1e78 100644 --- a/yaksh/templates/yaksh/showquestions.html +++ b/yaksh/templates/yaksh/showquestions.html @@ -43,7 +43,7 @@ </p> </div> <div class="card-body"> - <form action="" method="post" enctype="multipart/form-data"> + <form action="{% url 'yaksh:show_questions' %}" method="post" enctype="multipart/form-data"> {% csrf_token %} <div class="form-group col-md-6"> <a class="btn btn-info" href="{% url 'yaksh:download_yaml_template' %}"> diff --git a/yaksh/test_views.py b/yaksh/test_views.py index 793f9c8..8c79085 100644 --- a/yaksh/test_views.py +++ b/yaksh/test_views.py @@ -2234,11 +2234,7 @@ class TestCourses(TestCase): self.assertTrue(expected_lesson_files.exists()) self.assertEquals(expected_lesson_files[0].file.read(), file_content) - for lesson_file in self.all_files: - file_path = lesson_file.file.path - if os.path.exists(file_path): - os.remove(file_path) - shutil.rmtree(os.path.dirname(file_path)) + self.all_files.delete() def test_download_course_offline(self): """ Test to download course with lessons offline""" diff --git a/yaksh/views.py b/yaksh/views.py index cad6d82..4a6f462 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -1420,6 +1420,7 @@ def monitor(request, quiz_id=None, course_id=None, attempt_number=1): def _get_questions(user, question_type, marks): + questions = None if question_type is None and marks is None: return None if question_type: @@ -1447,6 +1448,7 @@ def _remove_already_present(questionpaper_id, questions): def _get_questions_from_tags(question_tags, user, active=True, questions=None): search_tags = [] + search = None for tags in question_tags: search_tags.extend(re.split('[; |, |\*|\n]', tags)) if questions: @@ -2719,9 +2721,7 @@ def edit_lesson(request, course_id=None, module_id=None, lesson_id=None): if 'Delete' in request.POST: remove_files_id = request.POST.getlist('delete_files') if remove_files_id: - files = LessonFile.objects.filter(id__in=remove_files_id) - for file in files: - file.remove() + LessonFile.objects.filter(id__in=remove_files_id).delete() messages.success( request, "Deleted files successfully" ) |