jumpserver/apps/terminal/filters.py

75 lines
2.3 KiB
Python
Raw Normal View History

2021-02-22 10:35:53 +00:00
from django_filters import rest_framework as filters
from django.db.models import QuerySet
2021-07-12 12:23:44 +00:00
from orgs.utils import current_org, filter_org_queryset
2021-02-22 10:35:53 +00:00
from terminal.models import Command, CommandStorage
class CommandFilter(filters.FilterSet):
date_from = filters.DateTimeFilter(method='do_nothing')
date_to = filters.DateTimeFilter(method='do_nothing')
session_id = filters.CharFilter(field_name='session')
command_storage_id = filters.UUIDFilter(method='do_nothing')
user = filters.CharFilter(lookup_expr='startswith')
input = filters.CharFilter(lookup_expr='icontains')
class Meta:
model = Command
fields = [
'asset', 'account', 'user', 'session', 'risk_level', 'input',
2021-02-22 10:35:53 +00:00
'date_from', 'date_to', 'session_id', 'risk_level', 'command_storage_id',
]
def do_nothing(self, queryset, name, value):
return queryset
@property
def qs(self):
qs = super().qs
2021-07-12 12:23:44 +00:00
qs = filter_org_queryset(qs)
2021-02-22 10:35:53 +00:00
qs = self.filter_by_timestamp(qs)
return qs
def filter_by_timestamp(self, qs: QuerySet):
date_from = self.form.cleaned_data.get('date_from')
date_to = self.form.cleaned_data.get('date_to')
filters = {}
if date_from:
date_from = date_from.timestamp()
filters['timestamp__gte'] = date_from
if date_to:
date_to = date_to.timestamp()
filters['timestamp__lte'] = date_to
qs = qs.filter(**filters)
return qs
class CommandFilterForStorageTree(CommandFilter):
asset = filters.CharFilter(method='do_nothing')
account = filters.CharFilter(method='do_nothing')
2021-02-22 10:35:53 +00:00
session = filters.CharFilter(method='do_nothing')
risk_level = filters.NumberFilter(method='do_nothing')
class Meta:
model = CommandStorage
fields = [
'asset', 'account', 'user', 'session', 'risk_level', 'input',
2021-02-22 10:35:53 +00:00
'date_from', 'date_to', 'session_id', 'risk_level', 'command_storage_id',
]
class CommandStorageFilter(filters.FilterSet):
real = filters.BooleanFilter(method='filter_real')
class Meta:
model = CommandStorage
fields = ['real', 'name', 'type', 'is_default']
2021-02-22 10:35:53 +00:00
def filter_real(self, queryset, name, value):
if value:
queryset = queryset.exclude(name='null')
return queryset