bgneal@1: import getopt, sys bgneal@1: bgneal@1: try: bgneal@1: import settings # Assumed to be in the same directory. bgneal@1: from django.core.management import setup_environ bgneal@1: except ImportError: bgneal@1: import sys bgneal@1: 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: sys.exit(1) bgneal@1: bgneal@1: bgneal@1: def precache(sizes=[], reset=False): bgneal@1: # setup django environment bgneal@1: setup_environ(settings) bgneal@1: bgneal@1: # import models bgneal@1: from photologue.models import Photo, PhotoSize, PhotoSizeCache bgneal@1: bgneal@1: cache = PhotoSizeCache() bgneal@1: bgneal@1: print 'Caching photos, this may take a while...' bgneal@1: bgneal@1: for photo in Photo.objects.all(): bgneal@1: if len(sizes): bgneal@1: for size in sizes: bgneal@1: photosize = cache.sizes.get(size, None) bgneal@1: if photosize is None: bgneal@1: print '\nA photosize named "%s" was not found...' % size bgneal@1: else: bgneal@1: if reset: bgneal@1: photo.remove_size(photosize) bgneal@1: photo.create_size(photosize) bgneal@1: else: bgneal@1: for size in caches.sizes.values(): bgneal@1: if reset: bgneal@1: Photo.remove_size(photosize) bgneal@1: photo.create_size(photosize) bgneal@1: bgneal@1: print ' Complete.' bgneal@1: sys.exit(2) bgneal@1: bgneal@1: bgneal@1: def reset(): bgneal@1: # setup django environment bgneal@1: setup_environ(settings) bgneal@1: bgneal@1: # import models bgneal@1: from photologue.models import Photo, PhotoSize bgneal@1: bgneal@1: print 'Reseting photo cache, this may take a while...' bgneal@1: bgneal@1: for photo in Photo.objects.all(): bgneal@1: photo.clear_cache() bgneal@1: bgneal@1: print ' Complete.' bgneal@1: sys.exit(2) bgneal@1: bgneal@1: bgneal@1: def usage(): bgneal@1: print """ bgneal@1: bgneal@1: pl-admin.py - Photologue administration script. bgneal@1: bgneal@1: Available Commands: bgneal@1: pl-admin.py create Resizes and caches all defined photo sizes for each image. bgneal@1: pl-admin.py reset Removes all cached images. bgneal@1: bgneal@1: Options: bgneal@1: --reset (-r) If calling create the script will clear the existing photo cache bgneal@1: before regenerating the specified size (or sizes) bgneal@1: --size (-s) The name of a photosize generate bgneal@1: bgneal@1: Usage: bgneal@1: pl-admin.py [options] command bgneal@1: bgneal@1: Examples: bgneal@1: pl-admin.py -r -s=thumbnail create bgneal@1: pl-admin.py -s=thumbnail -s=display create bgneal@1: pl-admin.py reset bgneal@1: bgneal@1: """ bgneal@1: bgneal@1: def main(): bgneal@1: try: bgneal@1: opts, args = getopt.getopt(sys.argv[1:], "hrs:", bgneal@1: ["help", "reset", "sizes="]) bgneal@1: except getopt.GetoptError, err: bgneal@1: print str(err) bgneal@1: usage() bgneal@1: sys.exit(2) bgneal@1: r = False bgneal@1: s = [] bgneal@1: for o, a in opts: bgneal@1: if o in ("-h", "--help"): bgneal@1: usage() bgneal@1: sys.exit(2) bgneal@1: if o in ("-r", "--reset"): bgneal@1: r = True bgneal@1: elif o in ("-s", "--sizes"): bgneal@1: s.append(a.strip('=')) bgneal@1: else: bgneal@1: usage() bgneal@1: sys.exit(2) bgneal@1: bgneal@1: if len(args) == 1: bgneal@1: command = args[0] bgneal@1: if command == 'create': bgneal@1: precache(s, r) bgneal@1: elif command == 'reset': bgneal@1: reset() bgneal@1: bgneal@1: usage() bgneal@1: bgneal@1: bgneal@1: if __name__ == '__main__': bgneal@1: main()