edgware/edgware/files/file_utils.py
2011-05-13 03:54:32 +05:30

91 lines
2.4 KiB
Python

from models import File, Category
import os
try:
import json
except:
import simplejson as json
def clean_nulls():
x = []
for f in File.objects.all():
try:
fil = f.file.url
if not fil.startswith("/static"):
print fil
except ValueError:
f.delete()
def clean_relative_links():
for f in File.objects.all():
if f.file.url.startswith("/srv"):
new_url = f.file.url.replace("/srv/edgware/edgware/static/", "")
print "%s CHANGED TO %s" % (f.file.url, new_url,)
f.file = new_url
f.save()
print "woohoo!"
def file_exists():
exists = 0
not_found = 0
for f in File.objects.all():
if os.path.exists(f.file.path):
exists += 1
else:
not_found +=1
print "exist: %d \n" % exists
print "do not exist: %d" % not_found
def manage_types():
wooga = {}
types = ['image', 'audio', 'text', 'video', 'other']
for t in types:
wooga[t] = 0
errors = 0
errorsArr = []
new_images = 0
for f in File.objects.all():
typ = f.type
try:
wooga[typ] += 1
except KeyError:
print f.file.url
# errorsArr.append(typ)
errors += 1
if f.file.url.upper().endswith("JPG"):
f.type = 'image'
f.save()
elif f.file.url.upper().endswith("OGV"):
f.type = 'video'
f.save()
elif f.file.url.upper().endswith("PDF"):
f.type = 'text'
f.save()
elif f.file.url.upper().endswith("DOC"):
f.type = "text"
f.save()
elif f.file.url.upper().endswith("RTF"):
f.type = "text"
f.save()
else:
f.type = "other"
# errorsArr.append(f.file.url[-3:])
wooga['errors'] = errors
wooga['errorsArray'] = errorsArr
wooga['new_images'] = new_images
print json.dumps(wooga, indent=4)
def get_files_per_category():
ret = {}
total = 0
for c in Category.objects.all():
no_of_files = c.file_set.count()
ret[c.name] = no_of_files
total += no_of_files
ret['total'] = total
print json.dumps(ret, indent=4)
def correct_types():
types = ['image', 'audio', 'text', 'video', 'other']