2024-04-11 08:35:00 +00:00
|
|
|
from django.db.models import Value, F, Q
|
2022-09-09 07:38:59 +00:00
|
|
|
from django.db.models.functions import Concat
|
2024-04-08 10:56:59 +00:00
|
|
|
from django_filters import rest_framework as filters
|
2022-06-28 09:19:33 +00:00
|
|
|
|
2021-08-25 11:02:50 +00:00
|
|
|
from common.drf.filters import BaseFilterSet
|
2022-06-23 05:52:28 +00:00
|
|
|
from tickets.models import (
|
2024-04-11 08:35:00 +00:00
|
|
|
Ticket, ApplyAssetTicket,
|
2022-06-23 05:52:28 +00:00
|
|
|
ApplyLoginTicket, ApplyLoginAssetTicket, ApplyCommandTicket
|
|
|
|
)
|
2021-08-25 11:02:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TicketFilter(BaseFilterSet):
|
|
|
|
assignees__id = filters.UUIDFilter(method='filter_assignees_id')
|
2022-09-09 07:38:59 +00:00
|
|
|
relevant_asset = filters.CharFilter(method='filter_relevant_asset')
|
|
|
|
relevant_command = filters.CharFilter(method='filter_relevant_command')
|
2024-04-08 10:56:59 +00:00
|
|
|
applicant_username_name = filters.CharFilter(method='filter_applicant_username_name')
|
2021-08-25 11:02:50 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Ticket
|
|
|
|
fields = (
|
2022-06-23 05:52:28 +00:00
|
|
|
'id', 'title', 'type', 'status', 'state', 'applicant', 'assignees__id'
|
2021-08-25 11:02:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def filter_assignees_id(self, queryset, name, value):
|
2022-06-28 09:19:33 +00:00
|
|
|
return queryset.filter(
|
|
|
|
ticket_steps__ticket_assignees__assignee__id=value
|
|
|
|
)
|
2022-06-23 05:52:28 +00:00
|
|
|
|
2022-09-09 07:38:59 +00:00
|
|
|
def filter_relevant_asset(self, queryset, name, value):
|
|
|
|
asset_ids = ApplyAssetTicket.objects.annotate(
|
|
|
|
asset_str=Concat(
|
2022-12-05 07:03:21 +00:00
|
|
|
F('apply_assets__name'), Value('('),
|
|
|
|
F('apply_assets__address'), Value(')')
|
2022-09-09 07:38:59 +00:00
|
|
|
)
|
|
|
|
).filter(
|
|
|
|
asset_str__icontains=value
|
|
|
|
).values_list('id', flat=True)
|
|
|
|
|
|
|
|
login_asset_ids = ApplyLoginAssetTicket.objects.annotate(
|
|
|
|
asset_str=Concat(
|
2022-12-05 07:03:21 +00:00
|
|
|
F('apply_login_asset__name'), Value('('),
|
|
|
|
F('apply_login_asset__address'), Value(')')
|
2022-09-09 07:38:59 +00:00
|
|
|
)
|
|
|
|
).filter(
|
|
|
|
asset_str__icontains=value
|
|
|
|
).values_list('id', flat=True)
|
|
|
|
|
|
|
|
command_ids = ApplyCommandTicket.objects.filter(
|
|
|
|
apply_run_asset__icontains=value
|
|
|
|
).values_list('id', flat=True)
|
|
|
|
|
|
|
|
ticket_ids = list(set(list(asset_ids) + list(login_asset_ids) + list(command_ids)))
|
|
|
|
return queryset.filter(id__in=ticket_ids)
|
|
|
|
|
|
|
|
def filter_relevant_command(self, queryset, name, value):
|
|
|
|
command_ids = ApplyCommandTicket.objects.filter(
|
|
|
|
apply_run_command__icontains=value
|
|
|
|
).values_list('id', flat=True)
|
|
|
|
return queryset.filter(id__in=list(command_ids))
|
|
|
|
|
2024-04-08 10:56:59 +00:00
|
|
|
def filter_applicant_username_name(self, queryset, name, value):
|
2024-03-28 10:28:49 +00:00
|
|
|
return queryset.filter(
|
2024-04-08 10:56:59 +00:00
|
|
|
Q(applicant__name__icontains=value) |
|
|
|
|
Q(applicant__username__icontains=value)
|
2024-03-28 10:28:49 +00:00
|
|
|
)
|
|
|
|
|
2022-06-23 05:52:28 +00:00
|
|
|
|
|
|
|
class ApplyAssetTicketFilter(BaseFilterSet):
|
|
|
|
class Meta:
|
|
|
|
model = ApplyAssetTicket
|
|
|
|
fields = ('id',)
|
|
|
|
|
|
|
|
|
|
|
|
class ApplyLoginTicketFilter(BaseFilterSet):
|
|
|
|
class Meta:
|
|
|
|
model = ApplyLoginTicket
|
|
|
|
fields = ('id',)
|
|
|
|
|
|
|
|
|
|
|
|
class ApplyLoginAssetTicketFilter(BaseFilterSet):
|
|
|
|
class Meta:
|
|
|
|
model = ApplyLoginAssetTicket
|
|
|
|
fields = ('id',)
|
|
|
|
|
|
|
|
|
|
|
|
class ApplyCommandTicketFilter(BaseFilterSet):
|
|
|
|
class Meta:
|
|
|
|
model = ApplyCommandTicket
|
|
|
|
fields = ('id',)
|