ledil / django-orphaned Goto Github PK
View Code? Open in Web Editor NEWdelete all orphaned files created by django (FileField)
License: Other
delete all orphaned files created by django (FileField)
License: Other
Similar to the exclude path feature, the app can provide a setting for excluding files that are stored at the 'root' too.
For example, I have a .gitignore for avoid including this files into the git repository. When I run python manage.py deleteorphaned --info
I get as output:
/var/someuser/private/.gitignore
/var/someuser/private/orphan_file.tar.gz
Maybe adding a new subsetting: exclude
and using a similiar concept than in should_skip
method:
# deleteorphaned.py:28
skip_files = ORPHANED_APPS_MEDIABASE_DIRS[app].get('exclude', ())
# deleteorphaned.py:55
if basename not in exclude:
all_files.append(os.path.join(root, basename))
Settings example:
ORPHANED_APPS_MEDIABASE_DIRS = {
'myapp': {
'root': '/var/someuser/private/',
'exclude': ('.gitignore')
}
}
Sometypes a db has contenttypes for old models (i.e. models which were deleted or renamed). This apps tries to iterate overmodels going through ContentType objects and calling ct.model_class(), which returns None for stale content types.
That produces the following crash:
Traceback (most recent call last):
File "./manage.py", line 20, in <module>
execute_from_command_line()
File "/home/admin/venv-dev/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/home/admin/venv-dev/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/admin/venv-dev/local/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/admin/venv-dev/local/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
output = self.handle(*args, **options)
File "/home/admin/venv-dev/local/lib/python2.7/site-packages/django_orphaned/management/commands/deleteorphaned.py", line 32, in handle
for field in mc._meta.fields:
AttributeError: 'NoneType' object has no attribute '_meta'
Probably this can be fixed by adding a
if mc is None: continue
inside the
for field in mc._meta.fields:
loop
The last release published at the Python Package Index (PyPI) is django-orphaned 0.3[1], maybe it's time to create a new package and allow installing recent version using standar pip comand:
pip install django-orphaned
Thanks!
all_files.append(os.path.join(root.decode('utf-8'), basename.decode('utf-8')))
In my case all apps use a single media root directory and if I define them all in ORPHANED_APPS_MEDIABASE_DIRS
they just mark each other's files for deletion. All files. Am I doing something wrong?
Here's my structure:
media/
media/squarei/
media/squarei/defaults
media/squarei/defaults/example1.png
media/squarei/defaults/example2.png
media/squarei/user_upload.png
My config looks like this
ORPHANED_APPS_MEDIABASE_DIRS = {
'squarei': {
'root': MEDIA_ROOT,
'skip': (
os.path.join(MEDIA_ROOT, 'squarei/defaults'),
),
}
}
django_orphaned looks at the media/squarei
directory and decides it's empty, because no FileFields refer to it. But the skipped directory is in there, and is deleted when django_orphaned deletes media/squarei
$ python manage.py deleteorphaned --info
Traceback (most recent call last):
File "manage.py", line 8, in <module>
execute_from_command_line(sys.argv)
File "/home/foo/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/home/foo/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/foo/local/lib/python2.7/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/foo/local/lib/python2.7/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/home/foo/local/lib/python2.7/site-packages/django_orphaned/management/commands/deleteorphaned.py", line 38, in handle
files = mc.objects.all().values_list(*fields,flat=True)
File "/home/foo/local/lib/python2.7/site-packages/django/db/models/query.py", line 533, in values_list
raise TypeError("'flat' is not valid when values_list is called with more than one field.")
TypeError: 'flat' is not valid when values_list is called with more than one field.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.