From 999ae564b8f5e492c82b2479252b7f683d316808 Mon Sep 17 00:00:00 2001 From: Hubert Jankowski Date: Mon, 31 Jan 2022 21:00:29 +0100 Subject: [PATCH] fixes --- templates/changePassword.html | 5 ++++ templates/login.html | 6 +++++ trials/views.py | 2 +- users/views.py | 45 +++++++++++++++++++++-------------- 4 files changed, 39 insertions(+), 19 deletions(-) diff --git a/templates/changePassword.html b/templates/changePassword.html index d17d21c..019f0f9 100644 --- a/templates/changePassword.html +++ b/templates/changePassword.html @@ -9,6 +9,11 @@





+ {% if messages %} + {% for message in messages %} +
{{ message|safe }}
+ {% endfor %} + {% endif %}
Wróć diff --git a/templates/login.html b/templates/login.html index c66b23e..f19e2fb 100644 --- a/templates/login.html +++ b/templates/login.html @@ -17,12 +17,18 @@

{% endfor %} + {% if messages %} + {% for message in messages %} +
{{ message|safe }}
+ {% endfor %} + {% endif %} + {% endblock %} diff --git a/trials/views.py b/trials/views.py index 6749aa3..3a490d3 100644 --- a/trials/views.py +++ b/trials/views.py @@ -262,7 +262,7 @@ class TestModelViewSet(viewsets.ModelViewSet): class TestTemplateView(TemplateView): PASSED = "Zaliczony" - FAILED = "Nie zaliczony" + FAILED = "Niezaliczony" UNKNOWN = "nieznany" PASSED = { diff --git a/users/views.py b/users/views.py index 75e5a52..e685bba 100644 --- a/users/views.py +++ b/users/views.py @@ -18,6 +18,7 @@ from django.contrib.auth.decorators import login_required from django.contrib.auth import logout as django_logout from rest_framework.renderers import TemplateHTMLRenderer from rest_framework.views import APIView +from django.contrib.auth.hashers import make_password class UserModelViewSet(viewsets.ModelViewSet): @@ -106,20 +107,23 @@ def login_success(request): def login(request): - context = {} - if request.POST: - form = AuthenticationForm(request.POST) - username = request.POST.get("username") - password = request.POST.get("password") + try: + context = {} + if request.POST: + form = AuthenticationForm(request.POST) + username = request.POST.get("username") + password = request.POST.get("password") - user = SettingsBackend().authenticate(request, email=username, password=password) - if user is not None: - auth_login(request, user) - return redirect('home') - context['login_form'] = form - else: - form = AuthenticationForm() - context['login_form'] = form + user = SettingsBackend().authenticate(request, email=username, password=password) + if user is not None: + auth_login(request, user) + return redirect('home') + context['login_form'] = form + else: + form = AuthenticationForm() + context['login_form'] = form + except: + return render(request, 'login.html', {"messages": {"Podano błędne dane logowania lub użytkownik nie istnieje.": ""}}) return render(request, 'login.html', context) @@ -162,11 +166,16 @@ def changePassword(request): oldPassword = request.POST.get("oldPassword") newPassword = request.POST.get("newPassword") repeatPassword = request.POST.get("repeatPassword") + if not request.user.check_password(oldPassword): + message = {"Błędne hasło użytkownika.": ""} + return render(request, 'changePassword.html', {"messages": message}) - # u = request.user - # u.first_name = firstName - # u.last_name = lastName - # u.save() + if newPassword != repeatPassword: + message = {"Nowe hasła nie są identyczne.": ""} + return render(request, 'changePassword.html', {"messages": message}) + + request.user.password = make_password(newPassword) + request.user.save() return redirect('account') return render(request, 'changePassword.html') @@ -216,7 +225,7 @@ class ResetPasswordConfirm(generics.GenericAPIView): def post(self, request): serializer = UserPasswordResetConfirmSerializer(data=request.data) serializer.is_valid() - return redirect('/users/login.html') + return redirect('/users/login') def resetPasswordConfirm(request):