Self hosted FLOSS fitness/workout, nutrition and weight tracker
Improve handling of deleted or uploaded media files
Roland Geider40ca8a0dee5e
diff --git a/pyproject.toml b/pyproject.tomlindex 13fbf3a50..4c42124e4 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -29,6 +29,7 @@ classifiers = [dependencies = ["bleach[css]~=6.3",+ "boto3~=1.42.71","celery[redis]~=5.6.0","crispy-bootstrap5~=2025.6","django-activity-stream~=2.0.0",diff --git a/settings/main.py b/settings/main.pyindex d258abf5a..db1ff3ba4 100644--- a/settings/main.py+++ b/settings/main.py@@ -265,7 +265,8 @@ LOGGING = {STORAGES = {'default': {'BACKEND': env.str(- 'DJANGO_STORAGES_DEFAULT_BACKEND', 'django.core.files.storage.FileSystemStorage'+ 'DJANGO_STORAGES_DEFAULT_BACKEND',+ 'django.core.files.storage.FileSystemStorage',),},# django.contrib.staticfiles.storage.StaticFilesStorage@@ -276,3 +277,22 @@ STORAGES = {),},}+++#+# S3 object storage config+# See https://wger.readthedocs.io/en/latest/production/docker.html#s3-object-storage+#+if env.bool('USE_S3_MEDIA_FILES', False):+ AWS_ACCESS_KEY_ID = env.str('AWS_ACCESS_KEY_ID')+ AWS_SECRET_ACCESS_KEY = env.str('AWS_SECRET_ACCESS_KEY')+ AWS_STORAGE_BUCKET_NAME = env.str('AWS_STORAGE_BUCKET_NAME')+ AWS_S3_REGION_NAME = env.str('AWS_S3_REGION_NAME')+ AWS_S3_DOMAIN = env.str('AWS_S3_DOMAIN')+ AWS_S3_ENDPOINT_URL = env.str('AWS_S3_ENDPOINT_URL', f'https://{AWS_S3_REGION_NAME}.{AWS_S3_DOMAIN}')+ AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.{AWS_S3_REGION_NAME}.{AWS_S3_DOMAIN}'+ AWS_QUERYSTRING_AUTH = False+ STORAGES['default']['BACKEND'] = 'storages.backends.s3boto3.S3Boto3Storage'++ if env.bool('USE_S3_URL_FOR_MEDIA', True):+ MEDIA_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/'diff --git a/settings/settings_global.py b/settings/settings_global.pyindex 14817caf4..91527550e 100644--- a/settings/settings_global.py+++ b/settings/settings_global.py@@ -361,23 +361,6 @@ THUMBNAIL_ALIASES = {},}-USE_S3 = os.getenv('USE_S3') == 'TRUE'--if USE_S3:- # aws settings- AWS_ACCESS_KEY_ID = os.getenv('AWS_ACCESS_KEY_ID')- AWS_SECRET_ACCESS_KEY = os.getenv('AWS_SECRET_ACCESS_KEY')- AWS_STORAGE_BUCKET_NAME = os.getenv('AWS_STORAGE_BUCKET_NAME')- AWS_DEFAULT_ACL = 'public-read'- AWS_S3_CUSTOM_DOMAIN = os.getenv('WGER_CDN_DOMAIN')- AWS_S3_OBJECT_PARAMETERS = {'CacheControl': 'max-age=31557600'}- # s3 static settings- AWS_LOCATION = 'static'- STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)- STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'-else:- STATIC_URL = '/static/'-## Django Rest Framework@@ -492,7 +475,7 @@ WGER_SETTINGS = {'USE_RECAPTCHA': False,'WGER_INSTANCE': 'https://wger.de',# Trophy system settings- 'TROPHIES_ENABLED': True, # Global toggle to enable/disable trophy system+ 'TROPHIES_ENABLED': True,'TROPHIES_INACTIVE_USER_DAYS': 30, # Days of inactivity before skipping trophy evaluation}diff --git a/uv.lock b/uv.lockindex c0eece7b0..3d872d5f4 100644--- a/uv.lock+++ b/uv.lock@@ -79,6 +79,34 @@ css = [{ name = "tinycss2" },]+[[package]]+name = "boto3"+version = "1.42.71"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "botocore" },+ { name = "jmespath" },+ { name = "s3transfer" },+]+sdist = { url = "https://files.pythonhosted.org/packages/7d/39/774ff22347856ebbe9da350045ad5851aa0524ee6e4832fdc98b27981801/boto3-1.42.71.tar.gz", hash = "sha256:500edd2699a3f479053bbfb407b06c231d1ff1e574f7c90d269d605a6a1f8160", size = 112773, upload-time = "2026-03-18T19:44:37.551Z" }+wheels = [+ { url = "https://files.pythonhosted.org/packages/e2/b6/b0b93090cfc3fdbdb21a0b18961508678a2b36a42e2b3a90994ac34e102c/boto3-1.42.71-py3-none-any.whl", hash = "sha256:a89fae01c4bc948671e99440ddc0f10bc73cc72d83218656057f730df0898eab", size = 140553, upload-time = "2026-03-18T19:44:35.317Z" },+]++[[package]]+name = "botocore"+version = "1.42.71"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "jmespath" },+ { name = "python-dateutil" },+ { name = "urllib3" },+]+sdist = { url = "https://files.pythonhosted.org/packages/37/65/a76ced7e1c7f61880ec474e301cb63c27fd47c09ae0b7e4ccaa3cd3b04c6/botocore-1.42.71.tar.gz", hash = "sha256:6b3796c76edeb78afee325a54e23508bbd57624faea1e4aeb8f6e9c1e1e79a0f", size = 14998263, upload-time = "2026-03-18T19:44:25.137Z" }+wheels = [+ { url = "https://files.pythonhosted.org/packages/86/b3/b970b62963b2391d10cd29402d3338bf49730e083aa9b2a688c8bf76af08/botocore-1.42.71-py3-none-any.whl", hash = "sha256:e6b7c611eeacbfa6a5a08cd56889b7a63eced48e99f8db9b270eeaf2c0b62796", size = 14671820, upload-time = "2026-03-18T19:44:20.997Z" },+]+[[package]]name = "celery"version = "5.6.2"@@ -1101,6 +1129,15 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/7f/ed/e3705d6d02b4f7aea715a353c8ce193efd0b5db13e204df895d38734c244/isort-7.0.0-py3-none-any.whl", hash = "sha256:1bcabac8bc3c36c7fb7b98a76c8abb18e0f841a3ba81decac7691008592499c1", size = 94672, upload-time = "2025-10-11T13:30:57.665Z" },]+[[package]]+name = "jmespath"+version = "1.1.0"+source = { registry = "https://pypi.org/simple" }+sdist = { url = "https://files.pythonhosted.org/packages/d3/59/322338183ecda247fb5d1763a6cbe46eff7222eaeebafd9fa65d4bf5cb11/jmespath-1.1.0.tar.gz", hash = "sha256:472c87d80f36026ae83c6ddd0f1d05d4e510134ed462851fd5f754c8c3cbb88d", size = 27377, upload-time = "2026-01-22T16:35:26.279Z" }+wheels = [+ { url = "https://files.pythonhosted.org/packages/14/2f/967ba146e6d58cf6a652da73885f52fc68001525b4197effc174321d70b4/jmespath-1.1.0-py3-none-any.whl", hash = "sha256:a5663118de4908c91729bea0acadca56526eb2698e83de10cd116ae0f4e97c64", size = 20419, upload-time = "2026-01-22T16:35:24.919Z" },+]+[[package]]name = "jsonschema"version = "4.26.0"@@ -1881,6 +1918,18 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/6d/78/097c0798b1dab9f8affe73da9642bb4500e098cb27fd8dc9724816ac747b/ruff-0.15.2-py3-none-win_arm64.whl", hash = "sha256:cabddc5822acdc8f7b5527b36ceac55cc51eec7b1946e60181de8fe83ca8876e", size = 10941649, upload-time = "2026-02-19T22:32:18.108Z" },]+[[package]]+name = "s3transfer"+version = "0.16.0"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "botocore" },+]+sdist = { url = "https://files.pythonhosted.org/packages/05/04/74127fc843314818edfa81b5540e26dd537353b123a4edc563109d8f17dd/s3transfer-0.16.0.tar.gz", hash = "sha256:8e990f13268025792229cd52fa10cb7163744bf56e719e0b9cb925ab79abf920", size = 153827, upload-time = "2025-12-01T02:30:59.114Z" }+wheels = [+ { url = "https://files.pythonhosted.org/packages/fc/51/727abb13f44c1fcf6d145979e1535a35794db0f6e450a0cb46aa24732fe2/s3transfer-0.16.0-py3-none-any.whl", hash = "sha256:18e25d66fed509e3868dc1572b3f427ff947dd2c56f844a5bf09481ad3f3b2fe", size = 86830, upload-time = "2025-12-01T02:30:57.729Z" },+]+[[package]]name = "six"version = "1.17.0"@@ -2115,6 +2164,7 @@ name = "wger"source = { editable = "." }dependencies = [{ name = "bleach", extra = ["css"] },+ { name = "boto3" },{ name = "celery", extra = ["redis"] },{ name = "crispy-bootstrap5" },{ name = "django" },@@ -2172,6 +2222,7 @@ docker = [[package.metadata]requires-dist = [{ name = "bleach", extras = ["css"], specifier = "~=6.3" },+ { name = "boto3", specifier = "~=1.42.71" },{ name = "celery", extras = ["redis"], specifier = "~=5.6.0" },{ name = "crispy-bootstrap5", specifier = "~=2025.6" },{ name = "django", specifier = "~=5.2.8" },diff --git a/wger/exercises/signals.py b/wger/exercises/signals.pyindex 47c5aa522..9ffaaae94 100644--- a/wger/exercises/signals.py+++ b/wger/exercises/signals.py@@ -14,9 +14,6 @@## You should have received a copy of the GNU Affero General Public License-# Standard Library-import pathlib-# Djangofrom django.db.models.signals import (post_delete,@@ -40,18 +37,17 @@ from wger.exercises.models import (@receiver(post_delete, sender=ExerciseImage)-def delete_exercise_image_on_delete(sender, instance, **kwargs):+def delete_exercise_image_on_delete(sender, instance: ExerciseImage, **kwargs):"""- Delete the image, along with its thumbnails, from the disk+ Delete the image along with its thumbnails"""-thumbnailer = get_thumbnailer(instance.image)thumbnailer.delete_thumbnails()instance.image.delete(save=False)@receiver(pre_save, sender=ExerciseImage)-def delete_exercise_image_on_update(sender, instance, **kwargs):+def delete_exercise_image_on_update(sender, instance: ExerciseImage, **kwargs):"""Delete the corresponding image from the filesystem when the ExerciseImageobject was edited@@ -66,9 +62,11 @@ def delete_exercise_image_on_update(sender, instance, **kwargs):new_file = instance.imageif not old_file == new_file:+ # Deletes the image as well as the thumbnailsthumbnailer = get_thumbnailer(instance.image)thumbnailer.delete_thumbnails()instance.image.delete(save=False)+ return None# Generate thumbnails when uploading a new image@@ -78,18 +76,16 @@ saved_file.connect(generate_aliases)@receiver(post_delete, sender=ExerciseVideo)def auto_delete_video_on_delete(sender, instance: ExerciseVideo, **kwargs):"""- Deletes file from filesystem when corresponding ExerciseVideo object is deleted+ Deletes file when corresponding ExerciseVideo object is deleted"""if instance.video:- path = pathlib.Path(instance.video.path)- if path.exists():- path.unlink()+ instance.video.delete(save=False)@receiver(pre_save, sender=ExerciseVideo)def delete_exercise_video_on_update(sender, instance: ExerciseVideo, **kwargs):"""- Deletes file from filesystem when corresponding ExerciseVideo object was edited+ Deletes file when corresponding ExerciseVideo object was edited"""if not instance.pk:return False@@ -100,16 +96,9 @@ def delete_exercise_video_on_update(sender, instance: ExerciseVideo, **kwargs):return Falsenew_file = instance.video- if old_file != new_file:- path = pathlib.Path(old_file.path)- if path.is_file():- path.unlink()---# Deletion log for exercise bases is handled in the model-# @receiver(pre_delete, sender=ExerciseBase)-# def add_deletion_log_base(sender, instance: ExerciseBase, **kwargs):-# pass+ if not old_file == new_file and old_file:+ old_file.delete(save=False)+ return None@receiver(pre_delete, sender=Translation)diff --git a/wger/gallery/models/image.py b/wger/gallery/models/image.pyindex 36e55a9f2..545ffe8f1 100644--- a/wger/gallery/models/image.py+++ b/wger/gallery/models/image.py@@ -15,7 +15,6 @@# Standard Library# Standard Library-import datetimeimport pathlibimport uuid@@ -92,20 +91,16 @@ class Image(models.Model):@receiver(models.signals.post_delete, sender=Image)def auto_delete_file_on_delete(sender, instance: Image, **kwargs):"""- Deletes file from filesystem- when corresponding `MediaFile` object is deleted.+ Deletes file when corresponding `MediaFile` object is deleted."""if instance.image:- path = pathlib.Path(instance.image.path)- if path.exists():- path.unlink()+ instance.image.delete(save=False)@receiver(models.signals.pre_save, sender=Image)def auto_delete_file_on_change(sender, instance: Image, **kwargs):"""- Deletes old file from filesystem- when corresponding `MediaFile` object is updated+ Deletes old file when corresponding `MediaFile` object is updatedwith new file."""if not instance.pk:@@ -117,7 +112,6 @@ def auto_delete_file_on_change(sender, instance: Image, **kwargs):return Falsenew_file = instance.image- if not old_file == new_file:- path = pathlib.Path(old_file.path)- if path.is_file():- path.unlink()+ if not old_file == new_file and old_file:+ old_file.delete(save=False)+ return Nonediff --git a/wger/nutrition/signals.py b/wger/nutrition/signals.pyindex 3bc71eef4..287ebd71b 100644--- a/wger/nutrition/signals.py+++ b/wger/nutrition/signals.py@@ -12,9 +12,6 @@## You should have received a copy of the GNU Affero General Public License-# Standard Library-import pathlib-# Djangofrom django.core.cache import cachefrom django.db.models.signals import (@@ -22,6 +19,9 @@ from django.db.models.signals import (post_save,)+# Third Party+from easy_thumbnails.files import get_thumbnailer+# wgerfrom wger.nutrition.models import (Image,@@ -49,15 +49,14 @@ post_delete.connect(reset_nutritional_values_canonical_form, sender=MealItem)def auto_delete_file_on_delete(sender, instance: Image, **kwargs):"""- Deletes file from filesystem- when corresponding `MediaFile` object is deleted.+ Delete the image along with its thumbnails"""if not instance.image:return- path = pathlib.Path(instance.image.path)- if path.exists():- path.unlink()+ thumbnailer = get_thumbnailer(instance.image)+ thumbnailer.delete_thumbnails()+ instance.image.delete(save=False)post_delete.connect(auto_delete_file_on_delete, sender=Image)