import bleach from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib import messages from django.urls import reverse_lazy from django.shortcuts import redirect, get_object_or_404 from django.views import View from tickets.forms import TicketForm from tickets.utils import UserCanWriteTicketMixin from tickets.models import Ticket class TicketEditView(LoginRequiredMixin, UserCanWriteTicketMixin, View): def post(self, request, pk): ticket = get_object_or_404(Ticket, pk=pk) form = TicketForm(request.user, request.POST, instance=ticket) if form.is_valid(): ticket = form.save(commit=False) ticket.note = bleach.clean(form.cleaned_data["note"], strip=True) ticket.save() messages.success(self.request, f'The "{ticket.title}" ticket has been successfully edited in {ticket.ticket_list.group} > {ticket.ticket_list}.') else: messages.error(self.request, "Failed to edit the ticket.") return redirect(reverse_lazy('tickets:ticket_detail', kwargs={'pk': pk}))