from django.http import HttpResponseRedirect from rest_framework import viewsets from users.models import User from users.serializers import UserSerializer 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 from .serializers import UserPasswordResetSerializer, UserPasswordResetConfirmSerializer, RegisterSerializer from rest_framework.response import Response from django.shortcuts import render, redirect from django.template import loader from .forms import RegistrationForm 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 from django.contrib.auth import logout as django_logout class UserModelViewSet(viewsets.ModelViewSet): queryset = User.objects.all() serializer_class = UserSerializer 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(): data = self.request.data User.objects.create( email=data["email"], first_name=data["first_name"], last_name=data["last_name"], password=data["password"] ) return Response({"detail": "Konto zostało pomyślnie założone. Możesz się zalogować!"}, status=status.HTTP_200_OK) else: return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST) 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, ) token_generator = default_token_generator def logout(request): django_logout(request) return render(request, 'logout.html') def register_success(request): return render(request, 'registerSuccess.html') def register(request): 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"], password=form.cleaned_data["password1"] ) return redirect('register_success') else: context['registration_form'] = form else: form = RegistrationForm() context['registration_form'] = form return render(request, 'register.html', context) @login_required def login_success(request): return render(request, 'great.html') def login(request): 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 return render(request, 'login.html', context) @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')