From 0481cf995f63f98eca09986c79017d9e5bc35277 Mon Sep 17 00:00:00 2001 From: John Jarvis Date: Thu, 1 Nov 2012 22:17:05 -0400 Subject: [PATCH] Updating view for different certificate statuses, error --- lms/djangoapps/certificates/views.py | 30 ++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/lms/djangoapps/certificates/views.py b/lms/djangoapps/certificates/views.py index 450eb9e181..2475492524 100644 --- a/lms/djangoapps/certificates/views.py +++ b/lms/djangoapps/certificates/views.py @@ -1,10 +1,11 @@ import logging from certificates.models import GeneratedCertificate +from certificates.models import CertificateStatuses as status from django.views.decorators.csrf import csrf_exempt from django.http import HttpResponse import json -log = logging.getLogger("mitx.certificates") +logger = logging.getLogger(__name__) @csrf_exempt @@ -12,6 +13,10 @@ def update_certificate(request): """ Will update GeneratedCertificate for a new certificate or modify an existing certificate entry. + + See models.py for a state diagram of certificate states + + This view should only ever be accessed by the xqueue server """ if request.method == "POST": @@ -26,7 +31,7 @@ def update_certificate(request): key=xqueue_header['lms_key']) except GeneratedCertificate.DoesNotExist: - log.critical('Unable to lookup certificate\n' + logger.critical('Unable to lookup certificate\n' 'xqueue_body: {0}\n' 'xqueue_header: {1}'.format( xqueue_body, xqueue_header)) @@ -36,10 +41,23 @@ def update_certificate(request): 'content': 'unable to lookup key'}), mimetype='application/json') - cert.download_uuid = xqueue_body['download_uuid'] - cert.verify_uuid = xqueue_body['download_uuid'] - cert.download_url = xqueue_body['url'] - cert.status = 'downloadable' + if 'error' in xqueue_body: + cert.status = status.error + else: + if cert.state in [status.generating, status.regenerating]: + cert.download_uuid = xqueue_body['download_uuid'] + cert.verify_uuid = xqueue_body['verify_uuid'] + cert.download_url = xqueue_body['url'] + cert.status = status.downloadable + elif cert.state in [status.deleting]: + cert.status = status.deleted + else: + logger.critical('Invalid state for cert update: {0}'.format( + cert.state)) + return HttpResponse(json.dumps({ + 'return_code': 1, + 'content': 'invalid cert state'}), + mimetype='application/json') cert.save() return HttpResponse(json.dumps({'return_code': 0}), mimetype='application/json')