Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Delete the ingredient image from the disk if the entry is deleted from the db

Roland Geider4f3ca9d8aff6
diff --git a/wger/nutrition/signals.py b/wger/nutrition/signals.py
index d3e52b287..3bc71eef4 100644
--- a/wger/nutrition/signals.py
+++ b/wger/nutrition/signals.py
@@ -1,5 +1,3 @@
-# -*- coding: utf-8 -*-
-
# This file is part of wger Workout Manager.
#
# wger Workout Manager is free software: you can redistribute it and/or modify
@@ -14,6 +12,9 @@
#
# You should have received a copy of the GNU Affero General Public License
+# Standard Library
+import pathlib
+
# Django
from django.core.cache import cache
from django.db.models.signals import (
@@ -23,6 +24,7 @@ from django.db.models.signals import (
# wger
from wger.nutrition.models import (
+ Image,
Meal,
MealItem,
NutritionPlan,
@@ -43,3 +45,19 @@ post_save.connect(reset_nutritional_values_canonical_form, sender=Meal)
post_delete.connect(reset_nutritional_values_canonical_form, sender=Meal)
post_save.connect(reset_nutritional_values_canonical_form, sender=MealItem)
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.
+ """
+ if not instance.image:
+ return
+
+ path = pathlib.Path(instance.image.path)
+ if path.exists():
+ path.unlink()
+
+
+post_delete.connect(auto_delete_file_on_delete, sender=Image)