From b3ddbb7c4352bc6ff2353c4531a88aa9a3189373 Mon Sep 17 00:00:00 2001
From: Alessandro <alessandro.brugnetti@uclouvain.be>
Date: Mon, 8 Jan 2024 17:06:36 +0100
Subject: [PATCH] =?UTF-8?q?OSIS-8819:=20Suppression=20des=20documents=20ph?=
 =?UTF-8?q?ysiques=20apr=C3=A8s=20expiration?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 osis_document/tasks.py | 11 ++++++++---
 setup.py               |  4 ++--
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/osis_document/tasks.py b/osis_document/tasks.py
index 7b521d0..e5e3380 100644
--- a/osis_document/tasks.py
+++ b/osis_document/tasks.py
@@ -6,7 +6,7 @@
 #    The core business involves the administration of students, teachers,
 #    courses, programs and so on.
 #
-#    Copyright (C) 2015-2021 Université catholique de Louvain (http://www.uclouvain.be)
+#    Copyright (C) 2015-2024 Université catholique de Louvain (http://www.uclouvain.be)
 #
 #    This program is free software: you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
@@ -23,6 +23,7 @@
 #    see http://www.gnu.org/licenses/.
 #
 # ##############################################################################
+import os
 from datetime import timedelta
 
 from django.conf import settings
@@ -44,7 +45,7 @@ def cleanup_old_uploads():
     # Clean Upload which are stale 2 case:
     #   - State "REQUESTED" and delay in temporary storage expired (= Upload not confirm by client)
     #   - Upload expired (= Storage delay expired - XLS export, PDF one time generation, ...)
-    Upload.objects.filter(
+    upload_expired_qs = Upload.objects.filter(
         Q(
             status=FileStatus.REQUESTED.name,
             uploaded_at__lte=now() - timedelta(seconds=settings.OSIS_DOCUMENT_TEMP_UPLOAD_MAX_AGE)
@@ -52,7 +53,11 @@ def cleanup_old_uploads():
         Q(
             expires_at__lte=now()
         ),
-    ).delete()
+    )
+    files_expired = [upload_expired.file for upload_expired in upload_expired_qs]
+    upload_expired_qs.delete()
+    for file in files_expired:
+        file.delete(save=False)
 
     # Clean Token expired
     Token.objects.filter(expires_at__lte=now()).delete()
diff --git a/setup.py b/setup.py
index 781bde7..f67fb49 100644
--- a/setup.py
+++ b/setup.py
@@ -6,7 +6,7 @@
 #    The core business involves the administration of students, teachers,
 #    courses, programs and so on.
 #
-#    Copyright (C) 2015-2023 Université catholique de Louvain (http://www.uclouvain.be)
+#    Copyright (C) 2015-2024 Université catholique de Louvain (http://www.uclouvain.be)
 #
 #    This program is free software: you can redistribute it and/or modify
 #    it under the terms of the GNU General Public License as published by
@@ -27,7 +27,7 @@
 
 setup(
     name='OSIS Document',
-    version='0.9.11',
+    version='0.9.12',
     description='Document management API and widget',
     url='http://github.com/uclouvain/osis-document',
     author='Université catholique de Louvain',