ticket_edit.py 1.1 KB

12345678910111213141516171819202122232425262728
  1. import bleach
  2. from django.contrib.auth.mixins import LoginRequiredMixin
  3. from django.contrib import messages
  4. from django.urls import reverse_lazy
  5. from django.shortcuts import redirect, get_object_or_404
  6. from django.views import View
  7. from tickets.forms import TicketForm
  8. from tickets.utils import UserCanWriteTicketMixin
  9. from tickets.models import Ticket
  10. class TicketEditView(LoginRequiredMixin, UserCanWriteTicketMixin, View):
  11. def post(self, request, pk):
  12. ticket = get_object_or_404(Ticket, pk=pk)
  13. form = TicketForm(request.user, request.POST, instance=ticket)
  14. if form.is_valid():
  15. ticket = form.save(commit=False)
  16. ticket.note = bleach.clean(form.cleaned_data["note"], strip=True)
  17. ticket.save()
  18. messages.success(self.request, f'The "{ticket.title}" ticket has been successfully edited in {ticket.ticket_list.group} > {ticket.ticket_list}.')
  19. else:
  20. messages.error(self.request, "Failed to edit the ticket.")
  21. return redirect(reverse_lazy('tickets:ticket_detail', kwargs={'pk': pk}))