12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import logging
- import os
- from django.contrib.auth.mixins import UserPassesTestMixin
- from django.shortcuts import get_object_or_404
- from tickets.models import *
- log = logging.getLogger(__name__)
- class SuperuserStaffRequiredMixin(UserPassesTestMixin):
- def test_func(self):
- return self.request.user.is_superuser or self.request.user.is_staff
- class UserCanReadTicketListMixin(UserPassesTestMixin):
- def test_func(self):
- ticket_list = get_object_or_404(TicketList.objects.select_related('group'), pk=self.kwargs.get('pk'))
- return ticket_list.group in self.request.user.groups.all()
- class UserCanReadTicketMixin(UserPassesTestMixin):
- def test_func(self):
- ticket = get_object_or_404(Ticket.objects.select_related('ticket_list', 'ticket_list__group'), pk=self.kwargs.get('pk'))
- return ticket.ticket_list.group in self.request.user.groups.all() or ticket.assigned_to == self.request.user
- class UserCanWriteTicketMixin(UserPassesTestMixin):
- def test_func(self):
- ticket = get_object_or_404(Ticket.objects.all(), pk=self.kwargs.get('pk'))
- return ticket.created_by == self.request.user
- def remove_attachment_file(attachment_id: int) -> bool:
- """Delete an Attachment object and its corresponding file from the filesystem."""
- try:
- attachment = Attachment.objects.get(id=attachment_id)
- if attachment.file:
- if os.path.isfile(attachment.file.path):
- os.remove(attachment.file.path)
- attachment.delete()
- return True
- except Attachment.DoesNotExist:
- log.info(f"Attachment {attachment_id} not found.")
- return False
|