Exemple #1
0
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', '')
    course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)

    user_id = request.session.get('_auth_user_id')
    software_name = request.POST.get('software')
    generate = request.POST.get('generate', False) == 'true'

    try:
        software = CourseSoftware.objects.get(name=software_name,
                                              course_id=course_key)
    except CourseSoftware.DoesNotExist:
        raise Http404

    try:
        user = User.objects.get(id=user_id)
    except User.DoesNotExist:
        raise Http404

    if generate:
        software_license = get_or_create_license(user, software)
    else:
        software_license = get_license(user, software)

    if software_license:
        response = {'serial': software_license.serial}
    else:
        response = {'error': 'No serial number found'}

    return HttpResponse(json.dumps(response), mimetype='application/json')
Exemple #2
0
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', '')
    course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)

    user_id = request.session.get('_auth_user_id')
    software_name = request.POST.get('software')
    generate = request.POST.get('generate', False) == 'true'

    try:
        software = CourseSoftware.objects.get(name=software_name,
                                              course_id=course_key)
    except CourseSoftware.DoesNotExist:
        raise Http404

    try:
        user = User.objects.get(id=user_id)
    except User.DoesNotExist:
        raise Http404

    if generate:
        software_license = get_or_create_license(user, software)
    else:
        software_license = get_license(user, software)

    if software_license:
        response = {'serial': software_license.serial}
    else:
        response = {'error': 'No serial number found'}

    return HttpResponse(json.dumps(response), content_type='application/json')