예제 #1
0
파일: test_util.py 프로젝트: lmyfzx/djblets
    def test_without_test_cookie(self):
        """Testing validate_test_cookie without test cookie set"""
        validate_test_cookie(self.form, self.request)

        self.assertIn('submit', self.form.errors)
        self.assertEqual(self.form.errors['submit'],
                         ['Cookies must be enabled.'])
예제 #2
0
def register(request, next_page, form_class=RegistrationForm,
             extra_context={},
             template_name="accounts/register.html"):
    if request.POST:
        form = form_class(data=request.POST, request=request)
        form.full_clean()
        validate_test_cookie(form, request)

        if form.is_valid():
            user = form.save()
            if user:
                # XXX Compatibility with Django 0.96 and 1.0
                formdata = getattr(form, "cleaned_data",
                                   getattr(form, "clean_data", None))
                user = auth.authenticate(username=formdata['username'],
                                         password=formdata['password1'])
                assert user
                auth.login(request, user)
                request.session.delete_test_cookie()

                return HttpResponseRedirect(next_page)
    else:
        form = form_class(request=request)

    request.session.set_test_cookie()

    context = {
        'form': form,
    }
    context.update(extra_context)

    return render_to_response(template_name, RequestContext(request, context))
예제 #3
0
파일: test_util.py 프로젝트: lmyfzx/djblets
    def test_with_test_cookie(self):
        """Testing validate_test_cookie with test cookie set"""
        self.request.session.set_test_cookie()

        validate_test_cookie(self.form, self.request)

        self.assertNotIn('submit', self.form.errors)
예제 #4
0
파일: views.py 프로젝트: Harikanrh/djblets
def register(request, next_page, form_class=RegistrationForm,
             extra_context={},
             template_name="accounts/register.html"):
    if request.POST:
        form = form_class(data=request.POST, request=request)
        form.full_clean()
        validate_test_cookie(form, request)

        if form.is_valid():
            user = form.save()
            if user:
                user = auth.authenticate(
                    username=form.cleaned_data['username'],
                    password=form.cleaned_data['password1'])
                assert user
                auth.login(request, user)
                try:
                    request.session.delete_test_cookie()
                except KeyError:
                    # Do nothing
                    pass

                return HttpResponseRedirect(next_page)
    else:
        form = form_class(request=request)

    request.session.set_test_cookie()

    context = {
        'form': form,
    }
    context.update(extra_context)

    return render_to_response(template_name, RequestContext(request, context))
예제 #5
0
def register(request,
             next_page,
             form_class=RegistrationForm,
             extra_context={},
             template_name="accounts/register.html"):
    if request.POST:
        form = form_class(data=request.POST, request=request)
        form.full_clean()
        validate_test_cookie(form, request)

        if form.is_valid():
            user = form.save()
            if user:
                # XXX Compatibility with Django 0.96 and 1.0
                formdata = getattr(form, "cleaned_data",
                                   getattr(form, "clean_data", None))
                user = auth.authenticate(username=formdata['username'],
                                         password=formdata['password1'])
                assert user
                auth.login(request, user)
                request.session.delete_test_cookie()

                return HttpResponseRedirect(next_page)
    else:
        form = form_class(request=request)

    request.session.set_test_cookie()

    context = {
        'form': form,
    }
    context.update(extra_context)

    return render_to_response(template_name, RequestContext(request, context))
예제 #6
0
def reset(request, key, next_page,
          form_template_name='accounts/reset_password.html',
          error_template_name='accounts/recovery_key_error.html'):
    session = get_recovery_session(key)
    if not session:
        return render_to_response(error_template_name, RequestContext(request))
    user = get_user(session.get('username'))

    if request.POST:
        form = ResetPasswordForm(request.POST)
        form.full_clean()
        validate_test_cookie(form, request)

        if not form.errors:
            # XXX Compatibility with Django 0.96 and 1.0
            formdata = getattr(form, "cleaned_data",
                               getattr(form, "clean_data", None))

            user.set_password(formdata['password1'])
            user.save()

            # Try to log in using the new password
            loginError = internal_login(request, user.username,
                                        formdata['password1'])
            if loginError:
                # This might happen if the account is deactivated.
                form.errors['submit'] = forms.util.ErrorList([loginError])
            else:
                # We're in successfully. Expire the recovery session.
                Session.objects.save(key, None, datetime.datetime.now())
                return HttpResponseRedirect(next_page)
    else:
        form = None

    request.session.set_test_cookie()
    return render_to_response(form_template_name, RequestContext(request, {
        'form_path': request.path,
        'username': user.username,
        'form': form,
        }))
예제 #7
0
def register(request,
             next_page,
             form_class=RegistrationForm,
             extra_context={},
             template_name="accounts/register.html"):
    if request.method == 'POST':
        form = form_class(data=request.POST, request=request)
        form.full_clean()
        validate_test_cookie(form, request)

        if form.is_valid():
            user = form.save()
            if user:
                user = auth.authenticate(
                    username=form.cleaned_data['username'],
                    password=form.cleaned_data['password1'])
                assert user
                auth.login(request, user)
                try:
                    request.session.delete_test_cookie()
                except KeyError:
                    # Do nothing
                    pass

                # Other components can listen to this signal to
                # perform additional tasks when a new user registers
                user_registered.send(sender=None, user=request.user)

                return HttpResponseRedirect(next_page)
    else:
        form = form_class(request=request)

    request.session.set_test_cookie()

    context = {
        'form': form,
    }
    context.update(extra_context)

    return render_to_response(template_name, RequestContext(request, context))
예제 #8
0
def register(request, next_page, form_class=RegistrationForm,
             extra_context=None, initial_values=None,
             form_kwargs=None, template_name="accounts/register.html"):
    """Handle registration of a new user.

    This works along with :py:class:`djblets.auth.forms.RegistrationForm`
    to register a new user. It will display a registration form, validate
    the user's new information, and then log them in.

    The registration form, next page, and context can all be customized by
    the caller.

    Args:
        request (HttpRequest):
            The HTTP request from the client.

        next_page (unicode):
            The URL to navigate to once registration is successful.

        form_class (Form subclass):
            The form that will handle registration, field validation, and
            creation of the user.

        extra_context (dict):
            Extra context variables to pass to the template when rendering.

        initial_values (dict):
            Initial values to set on the form when it is rendered.

        form_kwargs (dict):
            Additional keyword arguments to pass to the form class during
            instantiation.

        template_name (unicode):
            The name of the template containing the registration form.

    Returns:
        HttpResponse: The page's rendered response or redirect.
    """
    if initial_values is None:
        initial_values = {}

    if form_kwargs is None:
        form_kwargs = {}

    if request.method == 'POST':
        form = form_class(data=request.POST, request=request, **form_kwargs)
        form.full_clean()
        validate_test_cookie(form, request)

        if form.is_valid():
            user = form.save()
            if user:
                user = auth.authenticate(
                    username=form.cleaned_data['username'],
                    password=form.cleaned_data['password1'])
                assert user
                auth.login(request, user)
                try:
                    request.session.delete_test_cookie()
                except KeyError:
                    # Do nothing
                    pass

                # Other components can listen to this signal to
                # perform additional tasks when a new user registers
                user_registered.send(sender=None, user=request.user)

                return HttpResponseRedirect(next_page)
    else:
        form = form_class(initial=initial_values, request=request,
                          **form_kwargs)

    request.session.set_test_cookie()

    context = {
        'form': form,
    }

    if extra_context:
        context.update(extra_context)

    return render_to_response(template_name, RequestContext(request, context))