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 SuperuserStaffRequiredMixin, 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 TicketListDeleteView(BaseDeleteView, SuperuserStaffRequiredMixin, UserCanReadTicketListMixin): model = TicketList success_message = 'The "{0.name}" list has been successfully deleted from "{0.group.name}" group.' redirect_url = "tickets:ticket_list_list" class TicketDeleteView(BaseDeleteView, UserCanReadTicketMixin, 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"