Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup command #19

Closed
fabiocaccamo opened this issue Jun 25, 2015 · 14 comments
Closed

cleanup command #19

fabiocaccamo opened this issue Jun 25, 2015 · 14 comments

Comments

@fabiocaccamo
Copy link

It would be very nice to have a command that iterate over all file fields an delete all media-files (under a path specified by a setting) that are not used by any model-field anymore.
This would be very useful for existing projects where django-cleanup was not installed since the beginning.

@movinj
Copy link

movinj commented Aug 20, 2015

I agree!

@ghost
Copy link

ghost commented Sep 16, 2015

+1

@beruic
Copy link
Contributor

beruic commented Oct 9, 2015

This might help. I made it unrelated to this issue. It's a command that lists all FileFields available in your models:

from django.core.management.base import BaseCommand
from django.apps import apps
from django.db import models


class Command(BaseCommand):
    help = 'List all FieldFields in models'

    def handle(self, *args, **options):

        for model in apps.get_models():

            for field_name in model._meta.get_all_field_names():
                field = model._meta.get_field(field_name)

                if isinstance(field, models.FileField):
                    print(model.__name__ + ':: ' + str(field_name) + ':\t' + type(field).__name__)

If someone codes this, that code should be usable for django-castor as well.

@ghost
Copy link

ghost commented Oct 9, 2015

@beruic, this only finds FileFields in all models.

The Command must do the following:

  • Look for FileFields in models (your code does that)
  • Map all files inside the folders associated with FileFields
  • Look up if files belongs to existing objects, otherwise remove them

Also, keeping in mind that a few FileFiles can share the same folder, so you must check agains all of them before removing.

@beruic
Copy link
Contributor

beruic commented Oct 9, 2015

I know that :)

As I wrote "This might help", but it's not the solution.

I don't have the time to code it now, so I thought it better to throw my code here so someone else might benefit from it :)

@ghost
Copy link

ghost commented Oct 9, 2015

OK, actually the code is there already: https://github.com/un1t/django-cleanup/blob/master/django_cleanup/models.py#L90

@beruic
Copy link
Contributor

beruic commented Oct 9, 2015

That's a good way to do it, but perhaps the generator should be refactored to be generally available?
This is a way, but perhaps not the most beautiful way to do it:

Edit: Fixed error in code

def find_models_with_filefield():
    for model in get_models():
        opts = ensure_get_fields(model._meta)
        name = get_model_name(model)
        for field in opts.get_fields():
            if isinstance(field, models.FileField) and len(FIELDS[name]) == 0:
                FIELDS[name].append(field.name)

        if name in FIELDS:
            yield model

def connect_signals():
    if len(FIELDS) > 0:
            return

    for model in find_models_with_filefield():
        key = '{{}}_django_cleanup_{}'.format(get_model_name(model))
        post_init.connect(cache_original_post_init, sender=model,
                          dispatch_uid=key.format('post_init'))
        post_save.connect(delete_old_post_save, sender=model,
                          dispatch_uid=key.format('post_save'))
        post_delete.connect(delete_all_post_delete, sender=model,
                            dispatch_uid=key.format('post_delete'))

@fabiocaccamo
Copy link
Author

clean_media.py command
https://gist.github.com/fabiocaccamo/b007c2b2b09e2ca8412f

@gfairchild
Copy link

👍

@shtalinberg
Copy link

https://github.com/ledil/django-orphaned - You can also take ideas from this application

@uthapjhojho
Copy link

+1

@lanshark
Copy link

Is there anything I need to do when I first install this into an existing project? I'm not worried about it cleaning up orphans, but it does not seem to be doing anything when I set an existing (pre-installation) ImageField that had an image to None. The file remains in /media.

@vinnyrose
Copy link
Collaborator

@lanshark please open a new issue with details on what steps you have taken so far, as well as details on the configuration of the ImageField. And any details on third-party or custom fields or storage classes that you may be using.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants