views.py 2.93 KB
Newer Older
1
import logging
2
from certificates.models import GeneratedCertificate
3
from certificates.models import CertificateStatuses as status
4 5 6
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
import json
7

8
logger = logging.getLogger(__name__)
9

John Jarvis committed
10

11
@csrf_exempt
12 13 14 15
def update_certificate(request):
    """
    Will update GeneratedCertificate for a new certificate or
    modify an existing certificate entry.
16 17 18 19

    See models.py for a state diagram of certificate states

    This view should only ever be accessed by the xqueue server
20
    """
21

22
    if request.method == "POST":
23 24 25 26 27 28 29 30 31 32 33

        xqueue_body = json.loads(request.POST.get('xqueue_body'))
        xqueue_header = json.loads(request.POST.get('xqueue_header'))

        try:
            cert = GeneratedCertificate.objects.get(
                   user__username=xqueue_body['username'],
                   course_id=xqueue_body['course_id'],
                   key=xqueue_header['lms_key'])

        except GeneratedCertificate.DoesNotExist:
34
            logger.critical('Unable to lookup certificate\n'
35 36 37 38 39 40 41 42 43
                         'xqueue_body: {0}\n'
                         'xqueue_header: {1}'.format(
                                      xqueue_body, xqueue_header))

            return HttpResponse(json.dumps({
                            'return_code': 1,
                            'content': 'unable to lookup key'}),
                             mimetype='application/json')

44 45
        if 'error' in xqueue_body:
            cert.status = status.error
John Jarvis committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59
            if 'error_reason' in xqueue_body:

                # Hopefully we will record a meaningful error
                # here if something bad happened during the
                # certificate generation process
                #
                # example:
                #  (aamorm BerkeleyX/CS169.1x/2012_Fall)
                #  <class 'simples3.bucket.S3Error'>:
                #  HTTP error (reason=error(32, 'Broken pipe'), filename=None) :
                #  certificate_agent.py:175


                cert.error_reason = xqueue_body['error_reason']
60
        else:
John Jarvis committed
61
            if cert.status in [status.generating, status.regenerating]:
62 63 64 65
                cert.download_uuid = xqueue_body['download_uuid']
                cert.verify_uuid = xqueue_body['verify_uuid']
                cert.download_url = xqueue_body['url']
                cert.status = status.downloadable
John Jarvis committed
66
            elif cert.status in [status.deleting]:
67 68 69
                cert.status = status.deleted
            else:
                logger.critical('Invalid state for cert update: {0}'.format(
John Jarvis committed
70
                    cert.status))
71 72
                return HttpResponse(json.dumps({
                            'return_code': 1,
John Jarvis committed
73
                            'content': 'invalid cert status'}),
74
                             mimetype='application/json')
75 76 77
        cert.save()
        return HttpResponse(json.dumps({'return_code': 0}),
                             mimetype='application/json')