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