Source

cciw-website / scripts / manage_invitations.py

The default branch has multiple heads

#!/usr/bin/env python2.5
import sys
import os
import socket
from optparse import OptionParser

from django.contrib.auth.models import User
from cciw.cciwmain.models import Camp
from cciw.officers.models import Invitation

parser = OptionParser(usage=
"""

       manage_invitations.py <year> <number> <username1> [<username2> ...]
       manage_invitations.py --remove <year> <number> <username1> [<username2> ...]
"""
)

parser.add_option("-r", "--remove", dest="remove", action="store_true", default=False, help="Remove users instead of adding")

def usage_and_exit():
    parser.print_usage()
    sys.exit(1)

def main():
    options, args = parser.parse_args()
    if len(args) < 2:
        usage_and_exit()

    year = int(args[0])
    campnum = int(args[1])

    camp = Camp.objects.get(year=year, number=campnum)
    users = []
    for uname in args[2:]:
        try:
            u = User.objects.get(username=uname)
        except User.DoesNotExist:
            print "Can't find user '%s'" % uname
        users.append(u)

    for user in users:
        if options.remove:
            try:
                inv = Invitation.objects.get(camp=camp.id, officer=user.id)
                inv.delete()
            except Invitation.DoesNotExist:
                pass
        else:
            try:
                Invitation.objects.get(camp=camp.id, officer=user.id)
            except Invitation.DoesNotExist:
                inv = Invitation(camp_id=camp.id, officer_id=user.id)
                inv.save()

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.