Commit 666828f7 by John Jarvis

Command for certificate whitelist

parent 7e033d02
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
import os
from optparse import make_option from optparse import make_option
from student.models import UserProfile
from certificates.models import CertificateWhitelist from certificates.models import CertificateWhitelist
from django.contrib.auth.models import User from django.contrib.auth.models import User
class Command(BaseCommand): class Command(BaseCommand):
help = """ help = """
...@@ -16,12 +15,12 @@ class Command(BaseCommand): ...@@ -16,12 +15,12 @@ class Command(BaseCommand):
$ ... cert_whitelist --add joe -c "MITx/6.002x/2012_Fall" $ ... cert_whitelist --add joe -c "MITx/6.002x/2012_Fall"
Remove a user from the whitelist for a course Remove a user from the whitelist for a course
$ ... cert_whitelist --del joe -c "MITx/6.002x/2012_Fall" $ ... cert_whitelist --del joe -c "MITx/6.002x/2012_Fall"
Print out who is whitelisted for a course Print out who is whitelisted for a course
$ ... cert_whitelist -c "MITx/6.002x/2012_Fall" $ ... cert_whitelist -c "MITx/6.002x/2012_Fall"
""" """
...@@ -37,7 +36,7 @@ class Command(BaseCommand): ...@@ -37,7 +36,7 @@ class Command(BaseCommand):
dest='del', dest='del',
default=False, default=False,
help='user to remove from the certificate whitelist'), help='user to remove from the certificate whitelist'),
make_option('-c', '--course-id', make_option('-c', '--course-id',
metavar='COURSE_ID', metavar='COURSE_ID',
dest='course_id', dest='course_id',
...@@ -50,7 +49,7 @@ class Command(BaseCommand): ...@@ -50,7 +49,7 @@ class Command(BaseCommand):
if not course_id: if not course_id:
raise CommandError("You must specify a course-id") raise CommandError("You must specify a course-id")
if options['add'] and options['del']: if options['add'] and options['del']:
raise CommandError("Either remove or add a user, not both") raise CommandError("Either remove or add a user, not both")
if options['add'] or options['del']: if options['add'] or options['del']:
user_str = options['add'] or options['del'] user_str = options['add'] or options['del']
...@@ -59,8 +58,9 @@ class Command(BaseCommand): ...@@ -59,8 +58,9 @@ class Command(BaseCommand):
else: else:
user = User.objects.get(username=user_str) user = User.objects.get(username=user_str)
cert_whitelist, created = CertificateWhitelist.objects.get_or_create( cert_whitelist, created = \
user=user, course_id=course_id) CertificateWhitelist.objects.get_or_create(
user=user, course_id=course_id)
if options['add']: if options['add']:
cert_whitelist.whitelist = True cert_whitelist.whitelist = True
elif options['del']: elif options['del']:
...@@ -68,6 +68,7 @@ class Command(BaseCommand): ...@@ -68,6 +68,7 @@ class Command(BaseCommand):
cert_whitelist.save() cert_whitelist.save()
whitelist = CertificateWhitelist.objects.all() whitelist = CertificateWhitelist.objects.all()
print "User whitelist for course {0}:\n{1}".format(course_id, print "User whitelist for course {0}:\n{1}".format(course_id,
'\n'.join([ "{0} {1} {2}".format(u.user.username, u.user.email, u.whitelist) for u in whitelist])) '\n'.join(["{0} {1} {2}".format(
u.user.username, u.user.email, u.whitelist)
for u in whitelist]))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment