2024-02-26 10:51:40 +00:00
|
|
|
from django.apps import apps
|
|
|
|
from django.utils import translation
|
|
|
|
|
2023-11-16 06:41:35 +00:00
|
|
|
from django_filters import rest_framework as drf_filters
|
2020-05-08 08:48:26 +00:00
|
|
|
from rest_framework import filters
|
|
|
|
from rest_framework.compat import coreapi, coreschema
|
2022-08-18 03:48:58 +00:00
|
|
|
from common.drf.filters import BaseFilterSet
|
2024-01-31 09:17:34 +00:00
|
|
|
from common.sessions.cache import user_session_manager
|
2023-11-16 06:41:35 +00:00
|
|
|
from orgs.utils import current_org
|
2024-02-26 10:51:40 +00:00
|
|
|
from .models import UserSession, OperateLog
|
2020-05-08 08:48:26 +00:00
|
|
|
|
2022-10-08 08:55:14 +00:00
|
|
|
__all__ = ['CurrentOrgMembersFilter']
|
2020-05-08 08:48:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CurrentOrgMembersFilter(filters.BaseFilterBackend):
|
|
|
|
def get_schema_fields(self, view):
|
|
|
|
return [
|
|
|
|
coreapi.Field(
|
|
|
|
name='user', location='query', required=False, type='string',
|
|
|
|
schema=coreschema.String(
|
|
|
|
title='user',
|
|
|
|
description='user'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
def _get_user_list(self):
|
2020-07-20 02:42:22 +00:00
|
|
|
users = current_org.get_members(exclude=('Auditor',))
|
2020-05-08 08:48:26 +00:00
|
|
|
return users
|
|
|
|
|
|
|
|
def filter_queryset(self, request, queryset, view):
|
|
|
|
user_id = request.GET.get('user')
|
|
|
|
if user_id:
|
|
|
|
queryset = queryset.filter(user=user_id)
|
|
|
|
else:
|
|
|
|
queryset = queryset.filter(user__in=self._get_user_list())
|
|
|
|
return queryset
|
2022-08-18 03:48:58 +00:00
|
|
|
|
2023-11-16 06:41:35 +00:00
|
|
|
|
|
|
|
class UserSessionFilterSet(BaseFilterSet):
|
|
|
|
is_active = drf_filters.BooleanFilter(method='filter_is_active')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def filter_is_active(queryset, name, is_active):
|
2024-01-31 09:17:34 +00:00
|
|
|
keys = user_session_manager.get_active_keys()
|
2023-11-16 06:41:35 +00:00
|
|
|
if is_active:
|
2024-01-31 09:17:34 +00:00
|
|
|
queryset = queryset.filter(key__in=keys)
|
2023-11-16 06:41:35 +00:00
|
|
|
else:
|
2024-01-31 09:17:34 +00:00
|
|
|
queryset = queryset.exclude(key__in=keys)
|
2023-11-16 06:41:35 +00:00
|
|
|
return queryset
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = UserSession
|
|
|
|
fields = ['id', 'ip', 'city', 'type']
|
2024-02-26 10:51:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class OperateLogFilterSet(BaseFilterSet):
|
|
|
|
resource_type = drf_filters.CharFilter(method='filter_resource_type')
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def filter_resource_type(queryset, name, resource_type):
|
|
|
|
current_lang = translation.get_language()
|
|
|
|
with translation.override(current_lang):
|
|
|
|
mapper = {str(m._meta.verbose_name): m._meta.verbose_name_raw for m in apps.get_models()}
|
|
|
|
tp = mapper.get(resource_type)
|
|
|
|
queryset = queryset.filter(resource_type=tp)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = OperateLog
|
|
|
|
fields = [
|
|
|
|
'user', 'action', 'resource', 'remote_addr'
|
|
|
|
]
|