From 31bc03510dae4479965a1a69c5430a114caaa784 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 16 Sep 2014 11:44:06 +0200 Subject: [PATCH] [BUGFIX] Make django replace pre-existing uploaded files while updating them. --- frontend/views.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/frontend/views.py b/frontend/views.py index abac806..e24a544 100644 --- a/frontend/views.py +++ b/frontend/views.py @@ -131,7 +131,8 @@ class UpdateProjectView(ProtectedFormView, SingleObjectMixin): original_filename = str(original_file) """ Put file into temporary folder for analysis """ - relative_path = default_storage.save('tmp/project_' + str(self.project_id) + '.ctt4', ContentFile(original_file.read())) + target_filename = 'tmp/project_' + str(self.project_id) + '.ctt4' + relative_path = default_storage.save(target_filename, ContentFile(original_file.read())) temp_sqlite = os.path.join(settings.MEDIA_ROOT, relative_path) """ Test if SQLite is a valid citavi project. """ @@ -145,9 +146,14 @@ class UpdateProjectView(ProtectedFormView, SingleObjectMixin): if citavi_project_valid == False: """ TODO: Put up an error message or something. """ pass - else: + else: + target_filename = 'citavi/project_' + str(self.project_id) + '.ctt4' + """ Remove eventually pre-existing citavi file. """ + if default_storage.exists(target_filename): + default_storage.delete(target_filename) + """ Actually store file in citavi folder """ - relative_path = default_storage.save('citavi/project_' + str(self.project_id) + '.ctt4', ContentFile(original_file.read())) + relative_path = default_storage.save(target_filename, ContentFile(original_file.read())) sqlite_path = os.path.join(settings.MEDIA_ROOT, relative_path) """ Store new original filename in Project """ @@ -157,5 +163,7 @@ class UpdateProjectView(ProtectedFormView, SingleObjectMixin): """ Refresh identities from citavi project. """ # TODO + #citavi_project = Citavi.CitaviProject(sqlite_path) + #del citavi_project return super(UpdateProjectView, self).form_valid(form)