aboutsummaryrefslogtreecommitdiffstats
path: root/account/extra.py
diff options
context:
space:
mode:
Diffstat (limited to 'account/extra.py')
-rw-r--r--account/extra.py16
1 files changed, 16 insertions, 0 deletions
diff --git a/account/extra.py b/account/extra.py
index 6ff19b2..8f034c9 100644
--- a/account/extra.py
+++ b/account/extra.py
@@ -86,3 +86,19 @@ class OverwriteStorage(FileSystemStorage):
return name
+### delete files associated with model FileField
+# Pre-delete signal function for deleting files a model
+# https://djangosnippets.org/snippets/2820/
+def file_cleanup(sender, instance, *args, **kwargs):
+ """
+ Deletes the file(s) associated with a model instance. The model
+ is not saved after deletion of the file(s) since this is meant
+ to be used with the pre_delete signal.
+ """
+ for field_name, _ in instance.__dict__.iteritems():
+ field = getattr(instance, field_name)
+ if (issubclass(field.__class__, FieldFile) and field.name):
+ # pass False so FileField does not save the model
+ field.delete(save=False)
+
+