delete.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334
  1. from django.views import View
  2. from django.contrib.auth.mixins import LoginRequiredMixin
  3. from django.contrib import messages
  4. from django.shortcuts import get_object_or_404, redirect
  5. from tickets.models import TicketList, Ticket
  6. from tickets.utils import SuperuserStaffRequiredMixin, UserCanReadTicketListMixin, UserCanReadTicketMixin, UserCanWriteTicketMixin
  7. class BaseDeleteView(LoginRequiredMixin, View):
  8. model = None
  9. success_message = None
  10. redirect_url = None
  11. def post(self, request, pk):
  12. object = get_object_or_404(self.model, pk=pk)
  13. if self.success_message:
  14. messages.success(self.request, self.success_message.format(object))
  15. object.delete()
  16. return redirect(self.redirect_url)
  17. class TicketListDeleteView(BaseDeleteView, SuperuserStaffRequiredMixin):
  18. model = TicketList
  19. success_message = 'The "{0.name}" list has been successfully deleted from "{0.group.name}" group.'
  20. redirect_url = "tickets:ticket_list_list"
  21. class TicketDeleteView(BaseDeleteView, UserCanWriteTicketMixin):
  22. model = Ticket
  23. success_message = 'The "{0.title}" ticket has been successfully deleted from {0.ticket_list} > {0.ticket_list.group}.'
  24. redirect_url = "tickets:ticket_list_list"