示例#1
0
def login(request, template_name='emailauth/login.html',
    redirect_field_name=REDIRECT_FIELD_NAME):

    redirect_to = request.REQUEST.get(redirect_field_name, '')

    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            from django.contrib.auth import login
            login(request, form.get_user())
            if request.get_host() == 'testserver':
                if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
                    redirect_to = settings.LOGIN_REDIRECT_URL
                return HttpResponseRedirect(redirect_to)

            request.session.set_test_cookie()

            return HttpResponseRedirect(settings.LOGIN_URL + '?' + urlencode({
                'testcookiesupport': '',
                redirect_field_name: redirect_to,
            }))
    elif 'testcookiesupport' in request.GET:
        if request.session.test_cookie_worked():
            request.session.delete_test_cookie()
            if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
                redirect_to = settings.LOGIN_REDIRECT_URL
            return HttpResponseRedirect(redirect_to)
        else:
            form = LoginForm()
            errorlist = forms.util.ErrorList()
            errorlist.append(_("Your Web browser doesn't appear to "
                "have cookies enabled. Cookies are required for logging in."))
            form._errors = forms.util.ErrorDict()
            form._errors[forms.forms.NON_FIELD_ERRORS] = errorlist
    else:
        form = LoginForm()

    current_site = get_current_site(request)

    return render_to_response(template_name, {
            'form': form,
            redirect_field_name: redirect_to,
            'site_name': current_site.name,
        },
        context_instance=RequestContext(request))
示例#2
0
def request_password_reset(request,
    template_name='emailauth/request_password.html'):

    if request.method == 'POST':
        form = PasswordResetRequestForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data['email']
            user_email = UserEmail.objects.get(email=email)
            user_email.make_new_key()
            user_email.save()

            current_site = get_current_site(request)

            subject = render_to_string(
                'emailauth/request_password_email_subject.txt',
                {'site': current_site})
            # Email subject *must not* contain newlines
            subject = ''.join(subject.splitlines())

            message = render_to_string('emailauth/request_password_email.txt', {
                'reset_code': user_email.verification_key,
                'expiration_days': email_verification_days(),
                'site': current_site,
                'first_name': user_email.user.first_name,
            })

            django.core.mail.send_mail(subject, message,
                settings.DEFAULT_FROM_EMAIL, [email])

            return HttpResponseRedirect(
                reverse('ea_request_password_reset_continue',
                args=[email]))
    else:
        form = PasswordResetRequestForm()

    context = RequestContext(request)
    return render_to_response(template_name,
        {
            'form': form,
            'expiration_days': email_verification_days(),
        },
        context_instance=context)
示例#3
0
def register(request, callback=default_register_callback):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            email_obj = UserEmail.objects.create_unverified_email(
                form.cleaned_data['email'])
            current_site = get_current_site(request)
            first_name = form.cleaned_data['first_name']
            email_obj.send_verification_email(current_site, first_name)

            if callback is not None:
                callback(form, email_obj)

            email_obj.user.message_set.create(message='Welcome to %s.' % current_site.name)

            email_obj.save()
            return HttpResponseRedirect(reverse('ea_register_continue',
                args=[email_obj.email]))
    else:
        form = RegistrationForm()

    return render_to_response('emailauth/register.html', {'form': form},
        RequestContext(request))