from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.views.generic import ListView, CreateView
from .models import *
from django.views.generic.detail import DetailView
from . import forms
from django.shortcuts import redirect, render


class Userdetail(LoginRequiredMixin, DetailView):
    model = User
    template_name = 'userdetail.html'
    context_object_name = 'user'

    def get_object(self, queryset=None):
        return User.objects.get(id=self.kwargs.get("id"))


def upload_profile_photo(request):
    form = forms.UploadProfilePhotoForm(instance=request.user)
    if request.method == 'POST':
        form = forms.UploadProfilePhotoForm(request.POST, request.FILES, instance=request.user)
        if form.is_valid():
            form.save()
            return redirect('/')
    return render(request, 'upload_profile_photo.html', context={'form': form})


class UserCreateView(LoginRequiredMixin,CreateView):
    model = User
    form_class = forms.UserCreateForm
    template_name = 'user_form.html'
    success_url = reverse_lazy('utilisateur:user_list')


class ListUser(LoginRequiredMixin,ListView):
    model = User
    template_name = 'listuser.html'
    context_object_name = 'utilisateurs'
