Saturday, March 30, 2013

Re: FileField delete and update handling

Please have a look at following code.

pre_save and post_delete signal handlers are used here to
automatically delete unused files.

Signal handlers are generic in nature as the use isinstance(field,
models.FileField) to identify file fields.

Source - https://github.com/un1t/django-cleanup/blob/master/django_cleanup/models.py.

import os
from django.db import models
from django.db.models.signals import pre_save, post_delete
from django.db.models.loading import cache


def find_models_with_filefield():
result = []
for app in cache.get_apps():
model_list = cache.get_models(app)
for model in model_list:
for field in model._meta.fields:
if isinstance(field, models.FileField):
result.append(model)
break
return result

def remove_old_files(sender, instance, **kwargs):
if not instance.id:
return

try:
old_instance = instance.__class__.objects.get(id=instance.id)
except instance.DoesNotExist:
return

for field in instance._meta.fields:
if not isinstance(field, models.FileField):
continue
old_file = getattr(old_instance, field.name)
new_file = getattr(instance, field.name)
if old_file and old_file != new_file and os.path.exists(old_file.path):
try:
os.remove(old_file.path)
except OSError:
pass

def remove_files(sender, instance, **kwargs):
for field in instance._meta.fields:
if not isinstance(field, models.FileField):
continue
file = getattr(instance, field.name)
if file and os.path.exists(file.path):
try:
os.remove(file.path)
except OSError:
pass


for model in find_models_with_filefield():
pre_save.connect(remove_old_files, sender=model)
post_delete.connect(remove_files, sender=model)

On Sat, Mar 30, 2013 at 9:57 PM, <temp4746@gmail.com> wrote:
> Hi,
>
> As it seems Django doesn't handle the update and delete of files from a
> FileField by itself leaving you them pretty hard to use by default.
>
> I tried search around but couldn't find a proper answer as to the correct
> way of handling this.
>
> I'm looking for the case where when the file is updated to a new name or
> deleted, it will be deleted properly from the file system, preferably not
> leaving empty directories too.
>
> Not having this done at the model level means all the generic views and
> admin pages are useless and require customization to delete and update the
> file name in them.
>
> The other option is a script that is going to iterate the entire directory
> and look for files not listed in the db, which is a really expensive and
> slow operation.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscribe@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--

Sincerely,
Pankaj Singh
http://about.me/psjinx

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment