From a77e6bff4cdca45a445ce253c8703ad9d2386ac8 Mon Sep 17 00:00:00 2001 From: mipel Date: Fri, 17 Sep 2021 09:43:03 +0200 Subject: [PATCH] #18 File upload in certain folders * adds try-except for removing folders in case of non existence or changed folder names on the hard drive --- compensation/models.py | 12 ++++++++++-- ema/models.py | 6 +++++- intervention/models.py | 20 +++++++++++++++----- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/compensation/models.py b/compensation/models.py index 006a4637..2732f9c7 100644 --- a/compensation/models.py +++ b/compensation/models.py @@ -252,7 +252,11 @@ class CompensationDocument(AbstractDocument): # If a folder path has been set, we need to delete the whole folder! if folder_path is not None: - shutil.rmtree(folder_path) + try: + shutil.rmtree(folder_path) + except FileNotFoundError: + # Folder seems to be missing already... + pass class EcoAccount(AbstractCompensation): @@ -408,7 +412,11 @@ class EcoAccountDocument(AbstractDocument): # If a folder path has been set, we need to delete the whole folder! if folder_path is not None: - shutil.rmtree(folder_path) + try: + shutil.rmtree(folder_path) + except FileNotFoundError: + # Folder seems to be missing already... + pass class EcoAccountDeduction(BaseResource): diff --git a/ema/models.py b/ema/models.py index 3c0b5c25..f777a813 100644 --- a/ema/models.py +++ b/ema/models.py @@ -142,4 +142,8 @@ class EmaDocument(AbstractDocument): # If a folder path has been set, we need to delete the whole folder! if folder_path is not None: - shutil.rmtree(folder_path) + try: + shutil.rmtree(folder_path) + except FileNotFoundError: + # Folder seems to be missing already... + pass diff --git a/intervention/models.py b/intervention/models.py index 0f4fd677..f4a7e739 100644 --- a/intervention/models.py +++ b/intervention/models.py @@ -122,12 +122,19 @@ class RevocationDocument(AbstractDocument): # Always remove 'revocation' folder folder_path = self.file.path.split("/") - shutil.rmtree("/".join(folder_path[:-1])) + try: + shutil.rmtree("/".join(folder_path[:-1])) + except FileNotFoundError: + # Revocation subfolder seems to be missing already + pass if other_intervention_docs.count() == 0: # If there are no further documents for the intervention, we can simply remove the whole folder as well! - shutil.rmtree("/".join(folder_path[:-2])) - + try: + shutil.rmtree("/".join(folder_path[:-2])) + except FileNotFoundError: + # Folder seems to be missing already + pass class LegalData(UuidModel): """ @@ -415,5 +422,8 @@ class InterventionDocument(AbstractDocument): # If a folder path has been set, we need to delete the whole folder! if folder_path is not None: - shutil.rmtree(folder_path) - + try: + shutil.rmtree(folder_path) + except FileNotFoundError: + # Folder seems to be missing already... + pass