-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* utilise la LoginView de Django * change le backend standard pour afficher une erreur spécifique pour les comptes inactifs (nécessaire en utilisant la LoginView de Django) * simplifie en fusionnant les erreurs pour mauvais nom d'utilisateur ou mot de passe * conserve le comportement de redirection originel
- Loading branch information
Showing
5 changed files
with
87 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,44 @@ | ||
from django.conf import settings | ||
from django.contrib import messages | ||
from django.contrib.auth import authenticate, login | ||
from django.contrib.auth.models import User | ||
from django.template.context_processors import csrf | ||
from django.urls import reverse, resolve, Resolver404, NoReverseMatch | ||
from django.shortcuts import redirect, render, get_object_or_404 | ||
from django.utils.translation import gettext_lazy as _ | ||
from django.contrib.auth.views import LoginView | ||
from django.urls import reverse, is_valid_path | ||
|
||
from zds.member.forms import LoginForm | ||
from zds.member.models import Profile | ||
from zds.member.views import get_client_ip | ||
from zds.utils.tokens import generate_token | ||
|
||
|
||
def login_view(request): | ||
"""Logs user in.""" | ||
next_page = request.GET.get("next", "/") | ||
if next_page in [reverse("member-login"), reverse("register-member"), reverse("member-logout")]: | ||
next_page = "/" | ||
csrf_tk = {"next_page": next_page} | ||
csrf_tk.update(csrf(request)) | ||
error = False | ||
class LoginView(LoginView): | ||
form_class = LoginForm | ||
template_name = "member/login.html" | ||
|
||
if request.method != "POST": | ||
form = LoginForm() | ||
else: | ||
form = LoginForm(request.POST) | ||
def dispatch(self, request, *args, **kwargs): | ||
self.request = request | ||
return super().dispatch(request, *args, **kwargs) | ||
|
||
if form.is_valid(): | ||
username = form.cleaned_data["username"] | ||
password = form.cleaned_data["password"] | ||
user = User.objects.filter(username=username).first() | ||
if user is None: | ||
messages.error( | ||
request, | ||
_("Ce nom d’utilisateur est inconnu. Si vous ne possédez pas de compte, vous pouvez vous inscrire."), | ||
) | ||
else: | ||
if not user.is_active: | ||
messages.error( | ||
request, | ||
_( | ||
"Vous n'avez pas encore activé votre compte, " | ||
"vous devez le faire pour pouvoir vous " | ||
"connecter sur le site. Regardez dans vos " | ||
"mails : {}." | ||
).format(user.email), | ||
) | ||
else: | ||
user = authenticate(username=username, password=password) | ||
if user is None: | ||
messages.error( | ||
request, | ||
_( | ||
"Le mot de passe saisi est incorrect. Cliquez sur le lien « Mot de passe oublié ? »" | ||
"si vous ne vous en souvenez plus." | ||
), | ||
) | ||
initial = {"username": username} | ||
def form_invalid(self, form): | ||
# Display errors in error/info bars instead of the form built-in display. | ||
for error in form.errors.values(): | ||
messages.error(self.request, error[0]) | ||
form.form_show_errors = False | ||
return super().form_invalid(form) | ||
|
||
form = LoginForm(initial=initial) | ||
form.helper.form_action += "?next=" + next_page | ||
csrf_tk["error"] = error | ||
csrf_tk["form"] = form | ||
return render(request, "member/login.html", {"form": form, "csrf_tk": csrf_tk}) | ||
else: | ||
profile = get_object_or_404(Profile, user=user) | ||
if not profile.can_read_now(): | ||
messages.error( | ||
request, | ||
_( | ||
"Vous n'êtes pas autorisé à vous connecter sur le site, vous avez été banni par un " | ||
"modérateur." | ||
), | ||
) | ||
else: | ||
login(request, user) | ||
request.session["get_token"] = generate_token() | ||
if "remember" not in request.POST: | ||
request.session.set_expiry(0) | ||
profile.last_ip_address = get_client_ip(request) | ||
profile.save() | ||
# Redirect the user if needed. | ||
# Set the cookie for Clem smileys. | ||
# (For people switching account or clearing cookies | ||
# after a browser session.) | ||
try: | ||
response = redirect(resolve(next_page).url_name) | ||
except Resolver404: | ||
response = redirect(reverse("homepage")) | ||
return response | ||
def form_valid(self, form): | ||
if "remember" not in self.request.POST: | ||
self.request.session.set_expiry(0) | ||
form.user_cache.profile.last_ip_address = get_client_ip(self.request) | ||
form.user_cache.profile.save() | ||
return super().form_valid(form) | ||
|
||
form.helper.form_action += "?next=" + next_page | ||
csrf_tk["error"] = error | ||
csrf_tk["form"] = form | ||
return render(request, "member/login.html", {"form": form, "csrf_tk": csrf_tk}) | ||
def get_success_url(self): | ||
"""In case of success, redirect to homepage for some special 'next' targets or non-existing pages.""" | ||
url = self.get_redirect_url() | ||
if self.is_special(url) or not is_valid_path(url): | ||
url = settings.LOGIN_REDIRECT_URL | ||
return url | ||
|
||
@staticmethod | ||
def is_special(url): | ||
"""Determine if `url` is a special case for redirection.""" | ||
names = ["member-login", "register-member", "member-logout"] | ||
urls = [reverse(name) for name in names] | ||
return url in urls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters