Skip to content
Snippets Groups Projects
Commit 8c2e5567 authored by John Jarvis's avatar John Jarvis Committed by Carlos Andrés Rocha
Browse files

Cleanup

parent fceed0fc
No related branches found
Tags release-2021-05-04-10.16
No related merge requests found
......@@ -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
......
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")
class XQueueCertInterface(object):
def __init__(self, request=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
......@@ -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')
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment