Skip to content
Snippets Groups Projects
views.py 86.4 KiB
Newer Older
        response = render_to_response("email_change_successful.html", address_context)
Sarina Canelake's avatar
Sarina Canelake committed
    except Exception:  # pylint: disable=broad-except
        # If we get an unexpected exception, be sure to rollback the transaction
        transaction.rollback()
        raise
@ensure_csrf_cookie
def change_name_request(request):
    """ Log a request for a new name. """
        raise Http404
        pnc = PendingNameChange.objects.get(user=request.user.id)
    except PendingNameChange.DoesNotExist:
        pnc = PendingNameChange()
    pnc.user = request.user
    pnc.new_name = request.POST['new_name'].strip()
    pnc.rationale = request.POST['rationale']
    if len(pnc.new_name) < 2:
David Baumgold's avatar
David Baumgold committed
        return JsonResponse({
            "success": False,
            "error": _('Name required'),
        })  # TODO: this should be status code 400  # pylint: disable=fixme
    pnc.save()

    # The following automatically accepts name change requests. Remove this to
    # go back to the old system where it gets queued up for admin approval.
    accept_name_change_by_id(pnc.id)

David Baumgold's avatar
David Baumgold committed
    return JsonResponse({"success": True})
@ensure_csrf_cookie
def pending_name_changes(request):
    """ Web page which allows staff to approve or reject name changes. """
    if not request.user.is_staff:
        raise Http404

David Baumgold's avatar
David Baumgold committed
    students = []
    for change in PendingNameChange.objects.all():
        profile = UserProfile.objects.get(user=change.user)
        students.append({
            "new_name": change.new_name,
            "rationale": change.rationale,
            "old_name": profile.name,
            "email": change.user.email,
            "uid": change.user.id,
            "cid": change.id,
        })

    return render_to_response("name_changes.html", {"students": students})
@ensure_csrf_cookie
def reject_name_change(request):
    """ JSON: Name change process. Course staff clicks 'reject' on a given name change """
    if not request.user.is_staff:
        raise Http404

Matthew Mongeau's avatar
Matthew Mongeau committed
    try:
        pnc = PendingNameChange.objects.get(id=int(request.POST['id']))
Matthew Mongeau's avatar
Matthew Mongeau committed
    except PendingNameChange.DoesNotExist:
David Baumgold's avatar
David Baumgold committed
        return JsonResponse({
            "success": False,
            "error": _('Invalid ID'),
        })  # TODO: this should be status code 400  # pylint: disable=fixme
    pnc.delete()
David Baumgold's avatar
David Baumgold committed
    return JsonResponse({"success": True})
Sarina Canelake's avatar
Sarina Canelake committed
def accept_name_change_by_id(uid):
    """
    Accepts the pending name change request for the user represented
    by user id `uid`.
    """
Matthew Mongeau's avatar
Matthew Mongeau committed
    try:
Sarina Canelake's avatar
Sarina Canelake committed
        pnc = PendingNameChange.objects.get(id=uid)
Matthew Mongeau's avatar
Matthew Mongeau committed
    except PendingNameChange.DoesNotExist:
David Baumgold's avatar
David Baumgold committed
        return JsonResponse({
            "success": False,
            "error": _('Invalid ID'),
        })  # TODO: this should be status code 400  # pylint: disable=fixme
Sarina Canelake's avatar
Sarina Canelake committed
    user = pnc.user
    u_prof = UserProfile.objects.get(user=user)
Sarina Canelake's avatar
Sarina Canelake committed
    meta = u_prof.get_meta()
    if 'old_names' not in meta:
        meta['old_names'] = []
    meta['old_names'].append([u_prof.name, pnc.rationale, datetime.datetime.now(UTC).isoformat()])
Sarina Canelake's avatar
Sarina Canelake committed
    u_prof.set_meta(meta)
Sarina Canelake's avatar
Sarina Canelake committed
    u_prof.name = pnc.new_name
    u_prof.save()
    pnc.delete()

David Baumgold's avatar
David Baumgold committed
    return JsonResponse({"success": True})


@ensure_csrf_cookie
def accept_name_change(request):
    """ JSON: Name change process. Course staff clicks 'accept' on a given name change
    We used this during the prototype but now we simply record name changes instead
    of manually approving them. Still keeping this around in case we want to go
    back to this approval method.
    if not request.user.is_staff:
        raise Http404

    return accept_name_change_by_id(int(request.POST['id']))
@require_POST
@login_required
@ensure_csrf_cookie
def change_email_settings(request):
    """Modify logged-in user's setting for receiving emails from a course."""
    user = request.user

    course_id = request.POST.get("course_id")
    course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)
    receive_emails = request.POST.get("receive_emails")
    if receive_emails:
        optout_object = Optout.objects.filter(user=user, course_id=course_key)
        if optout_object:
            optout_object.delete()
        log.info(u"User {0} ({1}) opted in to receive emails from course {2}".format(user.username, user.email, course_id))
        track.views.server_track(request, "change-email-settings", {"receive_emails": "yes", "course": course_id}, page='dashboard')
    else:
        Optout.objects.get_or_create(user=user, course_id=course_key)
        log.info(u"User {0} ({1}) opted out of receiving emails from course {2}".format(user.username, user.email, course_id))
        track.views.server_track(request, "change-email-settings", {"receive_emails": "no", "course": course_id}, page='dashboard')

David Baumgold's avatar
David Baumgold committed
    return JsonResponse({"success": True})