from django.shortcuts import render
from .models import Enquete
from django.views.generic import CreateView, ListView, DetailView, UpdateView
from django.contrib.auth.mixins import LoginRequiredMixin
from .forms import *
from django.shortcuts import get_object_or_404, redirect


class AjoutEnqueteReunion(LoginRequiredMixin,CreateView):
    model = Enquete
    template_name = 'ajoutenquetereunion.html'
    form_class = CreateEnqueteReunionForm

    def get_object(self):
        return get_object_or_404(Reunion, id = self.kwargs['id'])

    def form_valid(self, form):
        reunion = self.get_object()
        form.instance.reunion = reunion
        form.instance.lieu = reunion.lieu
        form.instance.created_by = self.request.user
        print(form)
        return super().form_valid(form)


class AjoutEnquete(LoginRequiredMixin, CreateView):
    model = Enquete
    template_name = 'ajoutenquete.html'
    form_class = CreateEnqueteForm

    def form_valid(self, form):
        form.instance.created_by = self.request.user

        return super().form_valid(form)


class EnqueteDetail(LoginRequiredMixin,DetailView):
    model = Enquete
    template_name = 'detailenquete.html'
    context_object_name = 'enquete'

    def get_object(self, queryset=None):
        return get_object_or_404(Enquete, id=self.kwargs['id'])


class ListeEnqueteView(LoginRequiredMixin, ListView):
    model = Enquete
    context_object_name = 'enquetes'
    template_name = 'listenquete.html'
    paginate_by = 50

    def get_queryset(self):
        return Enquete.objects.all().order_by('-datesysteme')
