Exemplo n.º 1
0
def registration_activation_resend(request):
    """
    Resend activation email
    """

    form = ResendActivationMailForm(request.POST or None)

    if form.is_valid():
        username_or_email = request.POST['username_or_email']

        # get user
        if username_or_email.find('@') >= 0:
            user = get_object_or_None(User, email=username_or_email)
        else:
            user = get_object_or_None(User, username=username_or_email)

        # delete old activation
        activation = user.activation
        activation.delete()

        # generate new activation link
        activation = UserActivation.objects.create(user=user)

        # get activation link
        activation_url = activation.activation_link

        site = get_current_site(request)
        # send email
        message_data = {
            'site_name': site.name,
            'activation_url': activation_url,
            'username': user.username
        }

        simple_send_email(
            'accounts/emails/registration_subject.txt',
            'accounts/emails/registration_content.txt',
            [user.email],
            subject_data={'site_name': site.name},
            message_data=message_data)

        # redirect to success page
        redirect_url = create_url(
            path=reverse('registration_activation_resend_end'),
            params={'email': user.email})

        return redirect(redirect_url)

    return {'form': form}
Exemplo n.º 2
0
def registration(request):
    """
    Create new account
    """

    # if logged in, redirect to login success page
    if request.user.is_authenticated():
        return redirect(settings.REDIRECT_AFTER_LOGIN)

    form = RegistrationForm(request.POST or None)

    if form.is_valid():
        username = request.POST.get('username')
        password = request.POST.get('password')
        email = request.POST.get('email')

        # create user
        u = User(username=username, email=email, is_active=False)
        u.set_password(password)
        u.save()

        # get activation link
        activation_url = u.activation.activation_link

        # get site
        site = get_current_site(request)

        message_data = {
            'site_name': site.name,
            'activation_url': activation_url,
            'username': username
        }

        # send email
        simple_send_email(
            'accounts/emails/registration_subject.txt',
            'accounts/emails/registration_content.txt',
            [email],
            subject_data={'site_name': site.name},
            message_data=message_data)

        request.session['email'] = email
        # redirect to end registration page
        redirect_url = reverse('registration_end')
        return redirect(redirect_url)

    return {'form': form}
Exemplo n.º 3
0
def password_reset(request):
    """
    Password reset
    """
    if request.user.is_authenticated():
        return redirect('/')

    form = PasswordResetForm(request.POST or None)

    if form.is_valid():
        # get user
        username_or_email = request.POST['username_or_email']
        if username_or_email.find('@') >= 0:
            user = get_object_or_None(User, email=username_or_email)
        else:
            user = get_object_or_None(User, username=username_or_email)

        # check, if that user already requested password reset
        password_reset_obj = get_object_or_None(PasswordReset, user=user)
        if password_reset_obj:
            password_reset_obj.delete()

        # create password reset object
        pr = PasswordReset(user=user)
        pr.save()

        # get password reset link
        reset_url = pr.get_password_reset_link()

        # send email
        site = get_current_site(request)
        simple_send_email(
            'accounts/emails/password_reset_subject.txt',
            'accounts/emails/password_reset_content.txt',
            [user.email],
            subject_data={'site_name': site.name},
            message_data={
                'site_name': site.name,
                'reset_url': reset_url,
                'username': user.username,
                'reset_timeout': settings.PASSWORD_RESET_TIMEOUT_DAYS
            })
        return redirect('password_reset_end')

    return {'form': form}
Exemplo n.º 4
0
def email_change(request):
    """
    Show form for email change
    """

    form = ChangeEmailForm(request.POST or None, request=request)
    if form.is_valid():
        email = request.POST.get('email')

        # create activation object
        existed_email_activation = get_object_or_None(
            EmailActivation, user=request.user)
        if existed_email_activation:
            existed_email_activation.delete()
        user_activation = EmailActivation.objects.create(
            user=request.user, email=email)

        # get data for email
        activation_url = user_activation.activation_link
        site = get_current_site(request)
        username = request.user.username

        message_data = {
            'site_name': site.name,
            'activation_url': activation_url,
            'username': username
        }

        simple_send_email(
            'accounts/emails/email_change_subject.txt',
            'accounts/emails/email_change_content.txt',
            [email],
            subject_data={'site_name': site.name},
            message_data=message_data)

        request.session['old_email'] = request.user.email
        request.session['email'] = email
        return redirect('email_change_end')

    return {'form': form}