Login

FileField mismatches

Author:
depaolim
Posted:
December 9, 2014
Language:
Python
Version:
1.6
Score:
0 (after 0 ratings)

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

  1. Template tag - list punctuation for a list of items by shapiromatron 6 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 6 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 1 month ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 2 months ago
  5. Help text hyperlinks by sa2812 1 year, 2 months ago

Comments

Please login first before commenting.