From 8c2e55676b32bb982c2fb39ecf0f675b61f98fa7 Mon Sep 17 00:00:00 2001 From: John Jarvis Date: Thu, 1 Nov 2012 17:01:53 -0400 Subject: [PATCH] Cleanup --- lms/djangoapps/certificates/models.py | 27 ++++++++++++---------- lms/djangoapps/certificates/queue.py | 33 ++++++++++++--------------- lms/djangoapps/certificates/views.py | 5 ++-- 3 files changed, 31 insertions(+), 34 deletions(-) diff --git a/lms/djangoapps/certificates/models.py b/lms/djangoapps/certificates/models.py index 7541d31ef5..7fdea9c868 100644 --- a/lms/djangoapps/certificates/models.py +++ b/lms/djangoapps/certificates/models.py @@ -7,7 +7,7 @@ Certificates are created for a student and an offering of a course. When a certificate is generated, a unique ID is generated so that the certificate can be verified later. The ID is a UUID4, so that -it can't be easily guessed and so that it is unique. +it can't be easily guessed and so that it is unique. Certificates are generated in batches by a cron job, when a certificate is available for download the GeneratedCertificate @@ -16,14 +16,16 @@ on the course overview page. ''' + class CertificateStatuses(object): - unavailable='unavailable' - generating='generating' - regenerating='regenerating' - deleting='deleting' - deleted='deleted' - downloadable='downloadable' - error='error' + unavailable = 'unavailable' + generating = 'generating' + regenerating = 'regenerating' + deleting = 'deleting' + deleted = 'deleted' + downloadable = 'downloadable' + error = 'error' + class GeneratedCertificate(models.Model): user = models.ForeignKey(User) @@ -37,7 +39,8 @@ class GeneratedCertificate(models.Model): status = models.CharField(max_length=32, default='unavailable') class Meta: - unique_together= (('user', 'course_id'),) + unique_together = (('user', 'course_id'),) + def certificate_status_for_student(student, course_id): ''' @@ -48,9 +51,9 @@ def certificate_status_for_student(student, course_id): generating - A request has been made to generate a certificate, but it has not been generated yet. regenerating - A request has been made to regenerate a certificate, - but it has not been generated yet. + but it has not been generated yet. deleting - A request has been made to delete a certificate. - + deleted - The certificate has been deleted. downloadable - The certificate is available for download. @@ -67,7 +70,7 @@ def certificate_status_for_student(student, course_id): 'status': CertificateStatuses.downloadable, 'download_url': generated_certificate.download_url, } - else: + else: return {'status': generated_certificate.status} except GeneratedCertificate.DoesNotExist: pass diff --git a/lms/djangoapps/certificates/queue.py b/lms/djangoapps/certificates/queue.py index c254c19ebb..5aad41a8d4 100644 --- a/lms/djangoapps/certificates/queue.py +++ b/lms/djangoapps/certificates/queue.py @@ -1,27 +1,21 @@ -from django.utils.simplejson import dumps -from django.core.management.base import BaseCommand, CommandError from certificates.models import GeneratedCertificate from certificates.models import certificate_status_for_student from certificates.models import CertificateStatuses as status from courseware import grades, courses -from django.contrib.auth.models import User from django.test.client import RequestFactory from capa.xqueue_interface import XQueueInterface from capa.xqueue_interface import make_xheader, make_hashkey from django.conf import settings from requests.auth import HTTPBasicAuth from student.models import UserProfile -from django.conf import settings import json import random -import logging + class XQueueCertInterface(object): - log = logging.getLogger("mitx.certificates") - def __init__(self, request=None): if settings.XQUEUE_INTERFACE.get('basic_auth') is not None: @@ -36,14 +30,12 @@ class XQueueCertInterface(object): else: self.request = request - self.xqueue_interface = XQueueInterface( settings.XQUEUE_INTERFACE['url'], settings.XQUEUE_INTERFACE['django_auth'], requests_auth, ) - def regen_cert(self, student, course_id): """ @@ -86,15 +78,16 @@ class XQueueCertInterface(object): 'name': profile.name, } - key = cert.key - xheader = make_xheader('http://sandbox-jrjarvis-001.m.edx.org/certificate', key, 'test-pull') + # TODO - this needs to be read from settings + xheader = make_xheader( + 'http://sandbox-jrjarvis-001.m.edx.org/certificate', + key, 'test-pull') (error, msg) = self.xqueue_interface.send_to_queue(header=xheader, body=json.dumps(contents)) return cert_status - def remove_cert(self, student, course_id): """ @@ -121,7 +114,6 @@ class XQueueCertInterface(object): cert = GeneratedCertificate.objects.get( user=student, course_id=course_id) - username = cert.user.username cert.status = status.deleting cert.save() @@ -132,15 +124,16 @@ class XQueueCertInterface(object): 'username': cert.user.username, } - key = cert.key - xheader = make_xheader('http://sandbox-jrjarvis-001.m.edx.org/certificate', key, 'test-pull') + # TODO - this needs to be read from settings + xheader = make_xheader( + 'http://sandbox-jrjarvis-001.m.edx.org/certificate', + key, 'test-pull') (error, msg) = self.xqueue_interface.send_to_queue(header=xheader, body=json.dumps(contents)) return cert_status - def add_cert_to_queue(self, student, course_id): """ @@ -169,7 +162,6 @@ class XQueueCertInterface(object): cert_status = certificate_status_for_student( student, course_id)['status'] - if cert_status in VALID_STATUSES: # grade the student course = courses.get_course_by_id(course_id) @@ -195,10 +187,13 @@ class XQueueCertInterface(object): 'course_id': course_id, 'name': profile.name, } - xheader = make_xheader('http://sandbox-jrjarvis-001.m.edx.org/update_certificate?{0}'.format(key), key, 'test-pull') + # TODO - this needs to be read from settings + xheader = make_xheader( + 'http://sandbox-jrjarvis-001.m.edx.org/' + 'update_certificate?{0}'.format(key), key, 'test-pull') (error, msg) = self.xqueue_interface.send_to_queue( header=xheader, body=json.dumps(contents)) if error: - log.critical('Unable to send message') + raise Exception('Unable to send queue message') return cert_status diff --git a/lms/djangoapps/certificates/views.py b/lms/djangoapps/certificates/views.py index b71b648aec..450eb9e181 100644 --- a/lms/djangoapps/certificates/views.py +++ b/lms/djangoapps/certificates/views.py @@ -6,6 +6,7 @@ import json log = logging.getLogger("mitx.certificates") + @csrf_exempt def update_certificate(request): """ @@ -25,7 +26,7 @@ def update_certificate(request): key=xqueue_header['lms_key']) except GeneratedCertificate.DoesNotExist: - log.critical('Unable to lookup certificate\n' + log.critical('Unable to lookup certificate\n' 'xqueue_body: {0}\n' 'xqueue_header: {1}'.format( xqueue_body, xqueue_header)) @@ -42,5 +43,3 @@ def update_certificate(request): cert.save() return HttpResponse(json.dumps({'return_code': 0}), mimetype='application/json') - -