Пример #1
0
 def assertPrefValid(self, user):
     """Ensure that the correct preference for the user is persisted"""
     pref = UserPreference.objects.get(user=user, key=NOTIFICATION_PREF_KEY)
     self.assertTrue(pref)  # check exists and only 1 (.get)
     # now coerce username to utf-8 encoded str, since we test with non-ascii unicdoe above and
     # the unittest framework has hard time coercing to unicode.
     # decrypt also can't take a unicode input, so coerce its input to str
     self.assertEqual(six.binary_type(user.username.encode('utf-8')), UsernameCipher().decrypt(str(pref.value)))
Пример #2
0
def opt_out_email_updates(request, token, course_id):
    """
    A view that let users opt out of any email updates.

    This meant is meant to be the target of an opt-out link or button.
    The `token` parameter must decrypt to a valid username.
    The `course_id` is the string course key of any course.

    Raises a 404 if there are any errors parsing the input.
    """
    try:
        username = UsernameCipher().decrypt(token.encode())
        user = User.objects.get(username=username)
        course_key = CourseKey.from_string(course_id)
        course = get_course_by_id(course_key, depth=0)
    except UnicodeDecodeError:
        raise Http404("base64url")
    except UsernameDecryptionException as exn:
        raise Http404(text_type(exn))
    except User.DoesNotExist:
        raise Http404("username")
    except InvalidKeyError:
        raise Http404("course")

    context = {
        'course': course,
        'cancelled': False,
        'confirmed': False,
    }

    if request.method == 'POST':
        if request.POST.get('submit') == 'confirm':
            Optout.objects.get_or_create(user=user, course_id=course.id)
            log.info(
                u"User %s (%s) opted out of receiving emails from course %s",
                user.username,
                user.email,
                course_id,
            )
            context['confirmed'] = True
        else:
            context['cancelled'] = True

    return render_to_response('bulk_email/unsubscribe.html', context)
Пример #3
0
def opt_out_email_updates(request, token, course_id):
    """
    A view that let users opt out of any email updates.

    This meant is meant to be the target of an opt-out link or button.
    The `token` parameter must decrypt to a valid username.
    The `course_id` is the string course key of any course.

    Raises a 404 if there are any errors parsing the input.
    """
    try:
        username = UsernameCipher().decrypt(token).decode("utf-8")
        user = User.objects.get(username=username)
        course_key = CourseKey.from_string(course_id)
        course = get_course_by_id(course_key, depth=0)
    except UnicodeDecodeError:
        raise Http404("base64url")  # lint-amnesty, pylint: disable=raise-missing-from
    except UsernameDecryptionException as exn:
        raise Http404(text_type(exn))  # lint-amnesty, pylint: disable=raise-missing-from
    except User.DoesNotExist:
        raise Http404("username")  # lint-amnesty, pylint: disable=raise-missing-from
    except InvalidKeyError:
        raise Http404("course")  # lint-amnesty, pylint: disable=raise-missing-from

    unsub_check = request.POST.get('unsubscribe', False)
    context = {
        'course': course,
        'unsubscribe': unsub_check
    }

    if request.method == 'GET':
        return render_to_response('bulk_email/confirm_unsubscribe.html', context)

    if request.method == 'POST' and unsub_check:
        Optout.objects.get_or_create(user=user, course_id=course_key)
        log.info(
            u"User %s (%s) opted out of receiving emails from course %s",
            user.username,
            user.email,
            course_id,
        )

    return render_to_response('bulk_email/unsubscribe_success.html', context)