delete.py 981 B

12345678910111213141516171819202122232425262728
  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 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 TicketDeleteView(BaseDeleteView, UserCanWriteTicketMixin):
  18. model = Ticket
  19. success_message = 'The "{0.title}" ticket has been successfully deleted from {0.ticket_list} > {0.ticket_list.group}.'
  20. redirect_url = "tickets:ticket_list_list"