from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseRedirect
from django.urls import reverse_lazy
from django.utils import timezone
from django.views.generic import CreateView, DeleteView, ListView, UpdateView

from gestion.mixins import ChefEventRequiredMixin, ChefRequiredMixin
from pads.models import Pad


class PadList(LoginRequiredMixin, ListView):
    model = Pad
    context_object_name = "pads"
    ordering = ["-date"]
    template_name = "pads/list.html"


class PadCreate(ChefEventRequiredMixin, CreateView):
    model = Pad
    fields = ["nom", "url"]
    template_name = "pads/create.html"
    success_url = reverse_lazy("pads:list")

    def form_valid(self, form):
        pad = form.save(commit=False)
        pad.date = timezone.now()
        pad.save()
        return HttpResponseRedirect(self.success_url)


class PadUpdate(ChefEventRequiredMixin, UpdateView):
    model = Pad
    fields = ["nom", "url"]
    template_name = "pads/update.html"
    success_url = reverse_lazy("pads:list")


class PadDelete(ChefEventRequiredMixin, DeleteView):
    model = Pad
    template_name = "pads/delete.html"
    success_url = reverse_lazy("pads:list")
