Commit 83859e1d by John Jarvis

Cleanup and django-admin commands

parent f908ef71
# -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand
from certificates.models import GeneratedCertificate
from certificates.queue import XQueueCertInterface
from django.contrib.auth.models import User
from student.models import UserProfile
import sys
class Command(BaseCommand):
help = """
Looks for names that have unicode characters
and queues them up for a certificate request
"""
def handle(self, *args, **options):
course_id = 'BerkeleyX/CS169.1x/2012_Fall'
enrolled_students = User.objects.filter(
courseenrollment__course_id=course_id).prefetch_related(
"groups").order_by('username')
xq = XQueueCertInterface()
print "Looking for unusual names.."
for student in enrolled_students:
if certificate_status_for_student(
student, course_id)['status'] == 'unavailable':
continue
name = UserProfile.objects.get(user=student).name
for c in name:
if ord(c) >= 0x200:
ret = xq.add_cert(student, course_id)
if ret == 'generating':
print 'generating for {0}'.format(student)
break
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from certificates.models import GeneratedCertificate from certificates.models import GeneratedCertificate
from certificates.models import certificate_status_for_student
from certificates.queue import XQueueCertInterface from certificates.queue import XQueueCertInterface
from django.contrib.auth.models import User from django.contrib.auth.models import User
...@@ -13,22 +14,23 @@ class Command(BaseCommand): ...@@ -13,22 +14,23 @@ class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
course_id = 'BerkeleyX/CS169.1x/2012_Fall' help = """
Find all students that have need certificates
and put certificate requests on the queue
This is only for BerkeleyX/CS169.1x/2012_Fall
"""
enrolled_students = User.objects.filter( def handle(self, *args, **options):
course_id = 'BerkeleyX/CS169.1x/2012_Fall'
enrolled_students = User.objects.filter(id=444894,
courseenrollment__course_id=course_id).prefetch_related( courseenrollment__course_id=course_id).prefetch_related(
"groups").order_by('username') "groups").order_by('username')
xq = XQueueCertInterface() xq = XQueueCertInterface()
# TODO (this is for debugging, remove)
for c in GeneratedCertificate.objects.all():
c.delete()
count = 0
for student in enrolled_students: for student in enrolled_students:
ret = xq.add_cert(student, course_id) if certificate_status_for_student(
if ret == 'generating': student, course_id)['status'] == 'unavailable':
print 'generating for {0}'.format(student) ret = xq.add_cert(student, course_id)
count += 1 if ret == 'generating':
if count > 10: print 'generating for {0}'.format(student)
break
...@@ -37,6 +37,7 @@ State diagram: ...@@ -37,6 +37,7 @@ State diagram:
""" """
class CertificateStatuses(object): class CertificateStatuses(object):
unavailable = 'unavailable' unavailable = 'unavailable'
generating = 'generating' generating = 'generating'
...@@ -83,20 +84,23 @@ def certificate_status_for_student(student, course_id): ...@@ -83,20 +84,23 @@ def certificate_status_for_student(student, course_id):
deleted - The certificate has been deleted. deleted - The certificate has been deleted.
downloadable - The certificate is available for download. downloadable - The certificate is available for download.
notpassing - The student was graded but is not passing
If the status is "downloadable", the dictionary also contains If the status is "downloadable", the dictionary also contains
"download_url". "download_url".
If the student has been graded, the dictionary also contains their grade for the course. If the student has been graded, the dictionary also contains their
grade for the course.
''' '''
try: try:
generated_certificate = GeneratedCertificate.objects.get( generated_certificate = GeneratedCertificate.objects.get(
user=student, course_id=course_id) user=student, course_id=course_id)
d = {'status': generated_certificate.status, d = {'status': generated_certificate.status}
'grade' : generated_certificate.grade,} if generated_certificate.grade:
d['grade'] = generated_certificate.grade
if generated_certificate.status == CertificateStatuses.downloadable: if generated_certificate.status == CertificateStatuses.downloadable:
d['download_url'] = generated_certificate.download_url d['download_url'] = generated_certificate.download_url
return d return d
except GeneratedCertificate.DoesNotExist: except GeneratedCertificate.DoesNotExist:
......
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