ticket_create.py 1.0 KB

123456789101112131415161718192021222324252627
  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
  6. from django.views import View
  7. from tickets.forms import TicketForm
  8. from tickets.utils import UserCanReadTicketListMixin
  9. class TicketCreateView(LoginRequiredMixin, UserCanReadTicketListMixin, View):
  10. def post(self, request, pk):
  11. form = TicketForm(request.user, request.POST)
  12. if form.is_valid():
  13. new_ticket = form.save(commit=False)
  14. new_ticket.created_by = request.user
  15. new_ticket.note = bleach.clean(form.cleaned_data["note"], strip=True)
  16. new_ticket.save()
  17. messages.success(self.request, f'The "{new_ticket.title}" ticket has been successfully created in {new_ticket.ticket_list.group} > {new_ticket.ticket_list}.')
  18. else:
  19. messages.error(self.request, "Failed to create the ticket.")
  20. return redirect(reverse_lazy('tickets:ticket_list_detail', kwargs={'pk': pk}))