'Allows support staff to alter a user\'s enrollment.'

@method_decorator(require_support_permission)
def post(self, request, username_or_email):

    try:
        user = User.objects.get((Q(username=username_or_email) | Q(email=username_or_email)))
        course_id = request.data['course_id']
        course_key = CourseKey.from_string(course_id)
        old_mode = request.data['old_mode']
        new_mode = request.data['new_mode']
        reason = request.data['reason']
        enrollment = CourseEnrollment.objects.get(user=user, course_id=course_key)
        if (enrollment.mode != old_mode):
            return HttpResponseBadRequest(u'User    {username}    is    not    enrolled    with    mode    {old_mode}.'.format(username=user.username, old_mode=old_mode))
        if (new_mode == CourseMode.CREDIT_MODE):
            return HttpResponseBadRequest(u'Enrollment    cannot    be    changed    to    credit    mode.')
    except KeyError as err:
        return HttpResponseBadRequest(u'The    field    {}    is    required.'.format(err.message))
    except InvalidKeyError:
        return HttpResponseBadRequest(u'Could    not    parse    course    key.')
    except (CourseEnrollment.DoesNotExist, User.DoesNotExist):
        return HttpResponseBadRequest(u'Could    not    find    enrollment    for    user    {username}    in    course    {course}.'.format(username=username_or_email, course=unicode(course_key)))
    try:
        with transaction.atomic():
            update_enrollment(user.username, course_id, mode=new_mode, include_expired=True)
            manual_enrollment = ManualEnrollmentAudit.create_manual_enrollment_audit(request.user, enrollment.user.email, ENROLLED_TO_ENROLLED, reason=reason, enrollment=enrollment)
            return JsonResponse(ManualEnrollmentSerializer(instance=manual_enrollment).data)
    except CourseModeNotFoundError as err:
        return HttpResponseBadRequest(err.message)
