cciw-website / scripts / add_photos.py

#!/usr/bin/env python2.5
from cciw.cciwmain.models import Photo, Gallery, Camp
from cciw.cciwmain.views.camps import get_gallery_for_camp
import sys
import re

def usage():
    return """
Usage: add_photos.py photo1.jpeg [photo2.jpeg...]
"""


camp_re = re.compile('^(?P<year>\d{4})-(?P<number>\d+)')

def main():
    if len(sys.argv) == 1:
        print usage()
        sys.exit(1)

    for photoname in sys.argv[1:]:
        # Parse the photoname
        m = camp_re.match(photoname)
        if m is None:
            raise Exception("Photo %s is not in the form yyyy-n-*" % photoname)

        try:
            camp = Camp.objects.get(year=int(m.groupdict()['year']),
                                    number=int(m.groupdict()['number']))
        except Camp.DoesNotExist:
            raise Exception("Camp %s could not be found" % m.group())

        gallery = get_gallery_for_camp(camp)

        if gallery is None:
            raise Exception(("A gallery for camp %s, %s does not exist.  " +
                            "It is not created until the day the camp ends.") % (camp.number, camp.year))

        try:
            p = Photo.objects.get(gallery=gallery, filename=photoname)
        except Photo.DoesNotExist:
            p = None
        if p is None:
            p = Photo.create_default_photo(photoname, gallery)
            print "Created photo %s." % photoname
        else:
            print "Warning: Photo %s already exists." % photoname

if __name__ == '__main__':
    main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.