Consistency check on file fields between database and filesystem
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41  | from __future__ import print_function
import datetime
import os
import sys
from django.conf import settings
from django.db.models import get_models, FileField
all_files_on_fs = set()
for dirpath, dirnames, filenames in os.walk(settings.MEDIA_ROOT):
    rootpath, dirname = os.path.split(dirpath)
    all_files_on_fs |= set([os.path.join(dirname, name) for name in filenames])
all_files_on_db = {}
for model in get_models():
    for field in model._meta.fields:
        if isinstance(field, FileField):
            for pk, filepath in model.objects.values_list('pk', field.name):
                if filepath:
                    assert filepath not in all_files_on_db
                    all_files_on_db[filepath] = (model, pk)
with open("FileField_mismatches.txt", "w") as o:
    print (datetime.datetime.now(), file=o)
    print ("\n== Summary: ==", file=o)
    print ("FS Count:", len(all_files_on_fs), file=o)
    print ("DB Count:", len(all_files_on_db), file=o)
    print ("\n== Referenced in the DB but not existing on FS: ==", file=o)
    for fn, (model, pk) in all_files_on_db.items():
        if fn not in all_files_on_fs:
            print (model, pk, fn, file=o)
    print ("\n== Existing in FS but non referenced in the DB: ==", file=o)
    for fn in all_files_on_fs:
        if fn not in all_files_on_db:
            print (fn, file=o)
 | 
More like this
- Add Toggle Switch Widget to Django Forms by OgliariNatan 1 month, 4 weeks ago
 - get_object_or_none by azwdevops 5 months, 2 weeks ago
 - Mask sensitive data from logger by agusmakmun 7 months, 2 weeks ago
 - Template tag - list punctuation for a list of items by shapiromatron 1 year, 9 months ago
 - JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 9 months ago
 
Comments
Please login first before commenting.