attachment_remove.py 1.0 KB

123456789101112131415161718192021222324252627
  1. from django.contrib import messages
  2. from django.contrib.auth.decorators import login_required
  3. from django.core.exceptions import PermissionDenied
  4. from django.shortcuts import get_object_or_404, redirect
  5. from django.urls import reverse
  6. from tickets.models import Attachment
  7. from tickets.utils import remove_attachment_file
  8. @login_required
  9. def remove_attachment(request, attachment_id):
  10. if request.method == "POST":
  11. attachment = get_object_or_404(Attachment, pk=attachment_id)
  12. if not (attachment.added_by == request.user or attachment.ticket.created_by == request.user) and (attachment.ticket.list.group in request.user.groups.all()):
  13. raise PermissionDenied
  14. if remove_attachment_file(attachment.id):
  15. messages.success(request, f"The attachment has been successfully deleted.")
  16. else:
  17. messages.error(request, f"Sorry, there was a problem deleting attachment.")
  18. return redirect(reverse("tickets:ticket_detail", kwargs={"pk": attachment.ticket.pk}))
  19. else:
  20. raise PermissionDenied