comparison mysite/pl-admin.py @ 1:0dcfcdf50c62

Initial import of Madeira project from the private repository.
author Brian Neal <bgneal@gmail.com>
date Mon, 06 Apr 2009 03:10:59 +0000
parents
children
comparison
equal deleted inserted replaced
0:df0370bfe3f0 1:0dcfcdf50c62
1 import getopt, sys
2
3 try:
4 import settings # Assumed to be in the same directory.
5 from django.core.management import setup_environ
6 except ImportError:
7 import sys
8 sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
9 sys.exit(1)
10
11
12 def precache(sizes=[], reset=False):
13 # setup django environment
14 setup_environ(settings)
15
16 # import models
17 from photologue.models import Photo, PhotoSize, PhotoSizeCache
18
19 cache = PhotoSizeCache()
20
21 print 'Caching photos, this may take a while...'
22
23 for photo in Photo.objects.all():
24 if len(sizes):
25 for size in sizes:
26 photosize = cache.sizes.get(size, None)
27 if photosize is None:
28 print '\nA photosize named "%s" was not found...' % size
29 else:
30 if reset:
31 photo.remove_size(photosize)
32 photo.create_size(photosize)
33 else:
34 for size in caches.sizes.values():
35 if reset:
36 Photo.remove_size(photosize)
37 photo.create_size(photosize)
38
39 print ' Complete.'
40 sys.exit(2)
41
42
43 def reset():
44 # setup django environment
45 setup_environ(settings)
46
47 # import models
48 from photologue.models import Photo, PhotoSize
49
50 print 'Reseting photo cache, this may take a while...'
51
52 for photo in Photo.objects.all():
53 photo.clear_cache()
54
55 print ' Complete.'
56 sys.exit(2)
57
58
59 def usage():
60 print """
61
62 pl-admin.py - Photologue administration script.
63
64 Available Commands:
65 pl-admin.py create Resizes and caches all defined photo sizes for each image.
66 pl-admin.py reset Removes all cached images.
67
68 Options:
69 --reset (-r) If calling create the script will clear the existing photo cache
70 before regenerating the specified size (or sizes)
71 --size (-s) The name of a photosize generate
72
73 Usage:
74 pl-admin.py [options] command
75
76 Examples:
77 pl-admin.py -r -s=thumbnail create
78 pl-admin.py -s=thumbnail -s=display create
79 pl-admin.py reset
80
81 """
82
83 def main():
84 try:
85 opts, args = getopt.getopt(sys.argv[1:], "hrs:",
86 ["help", "reset", "sizes="])
87 except getopt.GetoptError, err:
88 print str(err)
89 usage()
90 sys.exit(2)
91 r = False
92 s = []
93 for o, a in opts:
94 if o in ("-h", "--help"):
95 usage()
96 sys.exit(2)
97 if o in ("-r", "--reset"):
98 r = True
99 elif o in ("-s", "--sizes"):
100 s.append(a.strip('='))
101 else:
102 usage()
103 sys.exit(2)
104
105 if len(args) == 1:
106 command = args[0]
107 if command == 'create':
108 precache(s, r)
109 elif command == 'reset':
110 reset()
111
112 usage()
113
114
115 if __name__ == '__main__':
116 main()