from django.contrib.auth import update_session_auth_hash
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.models import User
from django.contrib.auth.views import (LoginView, PasswordResetConfirmView,
                                       PasswordResetView)
from django.http import HttpResponse, HttpResponseNotAllowed
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import CreateView, DeleteView, UpdateView

from users.forms import (SignInForm, SignUpForm, UserPasswordResetForm,
                         UserProfileForm, UserSetPasswordForm)
from users.mixins import LockDuringEditMixin
from users.models import Lock, UserSession


class SignUp(CreateView):
    model = User
    form_class = SignUpForm
    template_name = "registration/signup.html"
    success_url = reverse_lazy("dashboard:dashboard")


class SignIn(LoginView):
    form_class = SignInForm


class Profile(LoginRequiredMixin, LockDuringEditMixin, UpdateView):
    model = User
    form_class = UserProfileForm
    template_name = "registration/profile.html"
    success_url = reverse_lazy("users:profile")

    def get_object(self):
        return self.request.user

    def form_valid(self, form):
        response = super().form_valid(form)
        update_session_auth_hash(self.request, self.object) # this will delete the current user session
        # and create anew
        UserSession.objects.create(user=self.object, session_id=self.request.session.session_key)
        return response


class UserPasswordResetView(PasswordResetView):
    form_class = UserPasswordResetForm


class UserPasswordResetConfirmView(PasswordResetConfirmView):
    form_class = UserSetPasswordForm


def unlock(request, pk):
    if request.method == "POST":
        lock = Lock.objects.filter(pk=pk).delete()
        return HttpResponse('')
    return HttpResponseNotAllowed(["POST"])

[((21, 18, 21, 53), 'django.urls.reverse_lazy', 'reverse_lazy', ({(21, 31, 21, 52): '"""dashboard:dashboard"""'}, {}), "('dashboard:dashboard')", False, 'from django.urls import reverse_lazy\n'), ((32, 18, 32, 47), 'django.urls.reverse_lazy', 'reverse_lazy', ({(32, 31, 32, 46): '"""users:profile"""'}, {}), "('users:profile')", False, 'from django.urls import reverse_lazy\n'), ((57, 11, 57, 43), 'django.http.HttpResponseNotAllowed', 'HttpResponseNotAllowed', ({(57, 34, 57, 42): "['POST']"}, {}), "(['POST'])", False, 'from django.http import HttpResponse, HttpResponseNotAllowed\n'), ((39, 8, 39, 59), 'django.contrib.auth.update_session_auth_hash', 'update_session_auth_hash', ({(39, 33, 39, 45): 'self.request', (39, 47, 39, 58): 'self.object'}, {}), '(self.request, self.object)', False, 'from django.contrib.auth import update_session_auth_hash\n'), ((41, 8, 41, 97), 'users.models.UserSession.objects.create', 'UserSession.objects.create', (), '', False, 'from users.models import Lock, UserSession\n'), ((56, 15, 56, 31), 'django.http.HttpResponse', 'HttpResponse', ({(56, 28, 56, 30): '""""""'}, {}), "('')", False, 'from django.http import HttpResponse, HttpResponseNotAllowed\n'), ((55, 15, 55, 41), 'users.models.Lock.objects.filter', 'Lock.objects.filter', (), '', False, 'from users.models import Lock, UserSession\n')]