SOITA/users/views.py

256 lines
7.9 KiB
Python
Raw Normal View History

2021-12-19 16:59:24 +01:00
from django.http import HttpResponseRedirect
2021-12-05 13:50:34 +01:00
from rest_framework import viewsets
from users.models import User
from users.serializers import UserSerializer
2021-12-11 22:01:14 +01:00
from rest_framework import views, exceptions, status, viewsets, permissions, generics
from tools.tools import PasswordResetShortcut, PasswordResetConfirmShortcut
from django.contrib.auth.tokens import default_token_generator
2021-12-12 23:04:19 +01:00
from .serializers import UserPasswordResetSerializer, UserPasswordResetConfirmSerializer, RegisterSerializer
from rest_framework.response import Response
2021-12-14 16:35:17 +01:00
from django.shortcuts import render, redirect
from django.template import loader
2021-12-20 20:37:32 +01:00
from .forms import RegistrationForm
2021-12-19 16:59:24 +01:00
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import login as auth_login
from config.authh import SettingsBackend
from django.contrib.auth.decorators import login_required
2022-01-13 23:24:24 +01:00
from django.contrib.auth import logout as django_logout
2022-01-27 23:21:11 +01:00
from rest_framework.renderers import TemplateHTMLRenderer
from rest_framework.views import APIView
2022-01-31 21:00:29 +01:00
from django.contrib.auth.hashers import make_password
2021-12-05 13:50:34 +01:00
class UserModelViewSet(viewsets.ModelViewSet):
queryset = User.objects.all()
serializer_class = UserSerializer
2021-12-11 22:01:14 +01:00
2021-12-12 23:04:19 +01:00
class RegisterViewSet(generics.GenericAPIView):
serializer_class = RegisterSerializer
permission_classes = (permissions.AllowAny, )
def post(self, request):
serializer = self.get_serializer(data=request.data)
if serializer.is_valid():
2021-12-12 23:20:04 +01:00
data = self.request.data
User.objects.create(
email=data["email"],
first_name=data["first_name"],
last_name=data["last_name"],
password=data["password"]
)
2021-12-14 00:18:30 +01:00
return Response({"detail": "Konto zostało pomyślnie założone. Możesz się zalogować!"}, status=status.HTTP_200_OK)
2021-12-12 23:04:19 +01:00
else:
return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)
2021-12-11 22:01:14 +01:00
class PasswordReset(PasswordResetShortcut, generics.GenericAPIView):
"""
```json
{
"email": "testowymail@gmail.com"
}
```
"""
serializer_class = UserPasswordResetSerializer
permission_classes = (permissions.AllowAny, )
class UserPasswordResetConfirmView(PasswordResetConfirmShortcut, generics.GenericAPIView):
"""
```json
{
"uid": "NYz",
"token": "asdasdasd",
"code": 123456,
"newPassword": "testowe",
"repeatNewPassword": "testowe"
}
```
"""
serializer_class = UserPasswordResetConfirmSerializer
permission_classes = (permissions.AllowAny, )
2021-12-14 16:35:17 +01:00
token_generator = default_token_generator
def logout(request):
2022-01-13 23:24:24 +01:00
django_logout(request)
2021-12-14 16:35:17 +01:00
return render(request, 'logout.html')
2021-12-15 16:46:50 +01:00
def register_success(request):
return render(request, 'registerSuccess.html')
2021-12-14 16:35:17 +01:00
def register(request):
2021-12-15 16:46:50 +01:00
context = {}
if request.POST:
form = RegistrationForm(request.POST)
if form.is_valid():
User.objects.create(
email=form.cleaned_data["email"],
first_name=form.cleaned_data["first_name"],
last_name=form.cleaned_data["last_name"],
2022-05-24 21:01:45 +02:00
password=form.cleaned_data["password1"],
type="standard"
2021-12-15 16:46:50 +01:00
)
return redirect('register_success')
else:
context['registration_form'] = form
else:
form = RegistrationForm()
context['registration_form'] = form
return render(request, 'register.html', context)
2021-12-19 16:59:24 +01:00
2022-01-13 23:24:24 +01:00
@login_required
2021-12-19 16:59:24 +01:00
def login_success(request):
return render(request, 'great.html')
2021-12-20 19:40:24 +01:00
def login(request):
2022-01-31 21:00:29 +01:00
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
except:
return render(request, 'login.html', {"messages": {"Podano błędne dane logowania lub użytkownik nie istnieje.": ""}})
2021-12-19 16:59:24 +01:00
return render(request, 'login.html', context)
2022-01-13 23:24:24 +01:00
@login_required
def account(request):
context = {}
context['user'] = request.user
return render(request, 'account.html', context)
@login_required
def changeEmail(request):
if request.POST:
email = request.POST.get("email")
u = request.user
u.email = email
u.save()
return redirect('account')
return render(request, 'changeEmail.html')
@login_required
def changeName(request):
if request.POST:
firstName = request.POST.get("firstName")
lastName = request.POST.get("lastName")
u = request.user
u.first_name = firstName
u.last_name = lastName
u.save()
return redirect('account')
return render(request, 'changeName.html')
2022-01-23 17:10:05 +01:00
@login_required
def changePassword(request):
if request.POST:
oldPassword = request.POST.get("oldPassword")
newPassword = request.POST.get("newPassword")
repeatPassword = request.POST.get("repeatPassword")
2022-01-31 21:00:29 +01:00
if not request.user.check_password(oldPassword):
message = {"Błędne hasło użytkownika.": ""}
return render(request, 'changePassword.html', {"messages": message})
if newPassword != repeatPassword:
message = {"Nowe hasła nie są identyczne.": ""}
return render(request, 'changePassword.html', {"messages": message})
2022-01-23 17:10:05 +01:00
2022-01-31 21:00:29 +01:00
request.user.password = make_password(newPassword)
request.user.save()
2022-01-23 17:10:05 +01:00
return redirect('account')
return render(request, 'changePassword.html')
2022-01-25 17:40:20 +01:00
def resetPassword(request):
"""
```json
{
"email": "test@test.com",
}
```
"""
if request.POST:
email = request.POST.get("email")
serializer_class = UserPasswordResetSerializer
permission_classes = (permissions.AllowAny,)
return redirect('resetPasswordConfirm')
return render(request, 'resetPassword.html')
2022-01-27 23:21:11 +01:00
class ResetPassword(generics.GenericAPIView):
renderer_classes = [TemplateHTMLRenderer]
template_name = 'resetPassword.html'
serializer_class = UserPasswordResetSerializer
permission_classes = (permissions.AllowAny, )
def get(self, request):
return render(request, 'resetPassword.html')
def post(self, request):
serializer = self.get_serializer(data=request.data)
serializer.is_valid()
return redirect('resetPasswordConfirm')
class ResetPasswordConfirm(generics.GenericAPIView):
renderer_classes = [TemplateHTMLRenderer]
template_name = 'resetPasswordConfirm.html'
serializer_class = UserPasswordResetConfirmSerializer
permission_classes = (permissions.AllowAny, )
def get(self, request):
return render(request, 'resetPasswordConfirm.html')
def post(self, request):
serializer = UserPasswordResetConfirmSerializer(data=request.data)
serializer.is_valid()
2022-01-31 21:00:29 +01:00
return redirect('/users/login')
2022-01-27 23:21:11 +01:00
2022-01-25 17:40:20 +01:00
def resetPasswordConfirm(request):
"""
```json
{
"uid": "NYz",
"token": "asdasdasd",
"code": 123456,
"newPassword": "testowe",
"repeatNewPassword": "testowe"
}
```
"""
if request.POST:
uid = request.POST.get("uid")
token = request.POST.get("token")
code = request.POST.get("code")
newPassword = request.POST.get("newPassword")
repeatPassword = request.POST.get("repeatNewPassword")
serializer_class = UserPasswordResetConfirmSerializer
permission_classes = (permissions.AllowAny,)
token_generator = default_token_generator
return redirect('login')
return render(request, 'resetPasswordConfirm.html')