from django.views import View
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib import messages
from django.shortcuts import get_object_or_404, redirect

from tickets.models import TicketList, Ticket
from tickets.utils import UserCanReadTicketListMixin, UserCanReadTicketMixin, UserCanWriteTicketMixin


class BaseDeleteView(LoginRequiredMixin, View):
    model = None
    success_message = None
    redirect_url = None

    def post(self, request, pk):
        object = get_object_or_404(self.model, pk=pk)
        
        if self.success_message:
            messages.success(self.request, self.success_message.format(object))

        object.delete()
        return redirect(self.redirect_url)


class TicketDeleteView(BaseDeleteView, UserCanWriteTicketMixin):
    model = Ticket
    success_message = 'The "{0.title}" ticket has been successfully deleted from {0.ticket_list} > {0.ticket_list.group}.'
    redirect_url = "tickets:ticket_list_list"
