Ejemplo n.º 1
0
class Login(TemplateView):
    template_name = "auth/login.html"

    def get(self, request, *args, **kwargs):
        if self.request.user.is_authenticated:
            return redirect("delegate",
                            delegate_slug=self.request.user.delegate.slug)
        self.form = LoginForm()
        return super().get(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        self.form = LoginForm(request.POST)
        if self.form.is_valid():
            password = self.form.cleaned_data["password"]
            email = self.form.cleaned_data["email"]
            user = authenticate(email=email, password=password)
            if not user:
                self.form.add_error(
                    None,
                    "Please enter a correct email and password. Note that both fields "
                    "are case-sensitive.",
                )
            elif not user.is_active:
                self.form.add_error(None, "Sorry, this account is inactive.")
            else:
                login(request, user)
                return redirect("delegate", delegate_slug=user.delegate.slug)
        return super().render_to_response(self.get_context_data())

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        context.update({"form": self.form})

        return context
Ejemplo n.º 2
0
def login(request):
    if request.user.is_authenticated:
        return redirect("/")
    if request.method == "GET":
        form = LoginForm()
    if request.method == "POST":
        form = LoginForm(data=request.POST)
        if form.is_valid():
            user = auth.authenticate(request, **form.cleaned_data)
            if user is not None:
                auth.login(request, user)
                return redirect(request.GET.get("next", "/"))
            else:
                form.add_error('password', 'invalid password or username')

    ctx = {'form': form}
    return render(request, 'login.html', ctx)
Ejemplo n.º 3
0
def login(request):
    redirected_path = request.GET.get('next', '/')
    form = LoginForm(request.POST)

    if request.POST:
        form = LoginForm(request.POST)
        if form.is_valid():
            user = auth.authenticate(
                username=form.cleaned_data.get('login'),
                password=form.cleaned_data.get('password'))
            if user is not None:
                auth.login(request, user)
                return redirect(redirected_path)
            else:
                form.add_error(None, 'Wrong login or password')

    context['form'] = form
    return render(request, 'login.html', context)
Ejemplo n.º 4
0
def auth_login(request):
    """ the view for the login form
        -verifies the data entered is correct and authenticates the user
        -if authentication is sucessfull, reidrects to the homepage
        -otherwise shows the errors that occured"""
    if request.user.is_authenticated():
        return redirect(reverse('app:home'))
    if request.method == 'POST':
        login_form = LoginForm(request.POST)
        if login_form.is_valid():
            email = UserManager.normalize_email(login_form.cleaned_data["email"])
            password = login_form.cleaned_data["password"]
            user = authenticate(request, email=email, password=password)
            if user:
                login(request, user)
                return redirect(request.META['HTTP_REFERER'])
            login_form.add_error('email', 'Invalid email or password')
        return render(request, 'app/login.html', {'login_form': login_form,})
    return render(request, 'app/login.html', {'login_form': LoginForm()})
Ejemplo n.º 5
0
def login_view(request, username=None):
    """
    Displays login form and runs authentication process.
    If username is given, then the login field is auto-filled.

    name: accounts:login
    URL: /accounts/login/
         /accounts/login/<username>/
    """
    next_page = request.GET.get('next')
    form = LoginForm(request.POST or None, initial={'username': username})
    if form.is_valid():
        user = authenticate(username=form.cleaned_data['username'],
                            password=form.cleaned_data['password'])
        if (user is not None) and user.is_active:
            login(request, user)
            return redirect(next_page or 'accounts:profile')
        else:
            error = ValidationError('Nieprawidłowy login lub hasło',
                                    code='incorrect_credentials')
            form.add_error('username', error)
    return render(request, 'accounts/login.html', {'form': form, 'next': next})