Add ajax endpoint to retrieve and assign course software licenses
This commit is contained in:
@@ -65,7 +65,7 @@ def _create_license(user, software):
|
||||
# table/rows with select_for_update to prevent race conditions
|
||||
with transaction.commit_on_success():
|
||||
selected = UserLicense.objects.select_for_update()
|
||||
license = selected.filter(user__isnull=True)[0]
|
||||
license = selected.filter(user__isnull=True, software=software)[0]
|
||||
license.user = user
|
||||
license.save()
|
||||
except IndexError:
|
||||
|
||||
@@ -1,9 +1,18 @@
|
||||
import logging
|
||||
import json
|
||||
import re
|
||||
from urlparse import urlparse
|
||||
from collections import namedtuple, defaultdict
|
||||
|
||||
|
||||
from mitxmako.shortcuts import render_to_string
|
||||
|
||||
from models import get_courses_licenses, get_or_create_license
|
||||
from django.contrib.auth.models import User
|
||||
from django.http import HttpResponse, Http404
|
||||
from django.views.decorators.csrf import requires_csrf_token, csrf_protect
|
||||
|
||||
from models import CourseSoftware
|
||||
from models import get_courses_licenses, get_or_create_license, get_license
|
||||
|
||||
|
||||
log = logging.getLogger("mitx.licenses")
|
||||
@@ -33,3 +42,45 @@ def get_licenses_by_course(user, courses):
|
||||
data_by_course[course_id] = render_to_string(template, context)
|
||||
|
||||
return data_by_course
|
||||
|
||||
|
||||
@requires_csrf_token
|
||||
def user_software_license(request):
|
||||
if request.method != 'POST' or not request.is_ajax():
|
||||
raise Http404
|
||||
|
||||
# get the course id from the referer
|
||||
url_path = urlparse(request.META.get('HTTP_REFERER', '')).path
|
||||
pattern = re.compile('^/courses/(?P<id>[^/]+/[^/]+/[^/]+)/.*/?$')
|
||||
match = re.match(pattern, url_path)
|
||||
|
||||
if not match:
|
||||
raise Http404
|
||||
course_id = match.groupdict().get('id', '')
|
||||
|
||||
user_id = request.session.get('_auth_user_id')
|
||||
software_name = request.POST.get('software')
|
||||
generate = request.POST.get('generate', False) == 'true'
|
||||
|
||||
print user_id, software_name, generate
|
||||
|
||||
try:
|
||||
software = CourseSoftware.objects.get(name=software_name,
|
||||
course_id=course_id)
|
||||
print software
|
||||
except CourseSoftware.DoesNotExist:
|
||||
raise Http404
|
||||
|
||||
user = User.objects.get(id=user_id)
|
||||
|
||||
if generate:
|
||||
license = get_or_create_license(user, software)
|
||||
else:
|
||||
license = get_license(user, software)
|
||||
|
||||
if license:
|
||||
response = {'serial': license.serial}
|
||||
else:
|
||||
response = {'error': 'No serial number found'}
|
||||
|
||||
return HttpResponse(json.dumps(response), mimetype='application/json')
|
||||
|
||||
@@ -154,6 +154,14 @@ if settings.COURSEWARE_ENABLED:
|
||||
url(r'^preview/chemcalc', 'courseware.module_render.preview_chemcalc',
|
||||
name='preview_chemcalc'),
|
||||
|
||||
# Software Licenses
|
||||
|
||||
# TODO: for now, this is the endpoint of an ajax replay
|
||||
# service that retrieve and assigns license numbers for
|
||||
# software assigned to a course. The numbers have to be loaded
|
||||
# into the database.
|
||||
url(r'^software-licenses$', 'licenses.views.user_software_license', name="user_software_license"),
|
||||
|
||||
url(r'^courses/(?P<course_id>[^/]+/[^/]+/[^/]+)/xqueue/(?P<userid>[^/]*)/(?P<id>.*?)/(?P<dispatch>[^/]*)$',
|
||||
'courseware.module_render.xqueue_callback',
|
||||
name='xqueue_callback'),
|
||||
|
||||
Reference in New Issue
Block a user