2020-10-21 11:32:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2020-12-11 07:29:02 +00:00
|
|
|
from applications.models import Application
|
2020-10-22 10:13:14 +00:00
|
|
|
from perms.models import ApplicationPermission
|
|
|
|
from perms import serializers
|
2020-12-11 07:29:02 +00:00
|
|
|
from ..base import BasePermissionViewSet
|
2020-10-21 11:32:27 +00:00
|
|
|
|
|
|
|
|
2020-12-11 07:29:02 +00:00
|
|
|
class ApplicationPermissionViewSet(BasePermissionViewSet):
|
2020-10-21 11:32:27 +00:00
|
|
|
"""
|
|
|
|
应用授权列表的增删改查API
|
|
|
|
"""
|
|
|
|
model = ApplicationPermission
|
|
|
|
serializer_class = serializers.ApplicationPermissionSerializer
|
2021-07-21 09:26:51 +00:00
|
|
|
filterset_fields = {
|
|
|
|
'name': ['exact'],
|
|
|
|
'category': ['exact'],
|
|
|
|
'type': ['exact', 'in'],
|
|
|
|
}
|
2021-07-16 09:09:06 +00:00
|
|
|
search_fields = ['name', 'category', 'type']
|
2020-12-11 08:18:03 +00:00
|
|
|
custom_filter_fields = BasePermissionViewSet.custom_filter_fields + [
|
2021-07-30 09:19:07 +00:00
|
|
|
'application_id', 'application', 'app', 'app_name'
|
2020-12-11 08:18:03 +00:00
|
|
|
]
|
2020-10-21 11:32:27 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset().prefetch_related(
|
|
|
|
"applications", "users", "user_groups", "system_users"
|
|
|
|
)
|
|
|
|
return queryset
|
|
|
|
|
2020-12-11 07:29:02 +00:00
|
|
|
def filter_application(self, queryset):
|
2021-07-30 09:19:07 +00:00
|
|
|
app_id = self.request.query_params.get('application_id') or \
|
|
|
|
self.request.query_params.get('app')
|
|
|
|
app_name = self.request.query_params.get('application') or \
|
|
|
|
self.request.query_params.get('app_name')
|
|
|
|
|
|
|
|
if app_id:
|
|
|
|
applications = Application.objects.filter(pk=app_id)
|
|
|
|
elif app_name:
|
|
|
|
applications = Application.objects.filter(name=app_name)
|
2020-12-11 07:29:02 +00:00
|
|
|
else:
|
|
|
|
return queryset
|
|
|
|
if not applications:
|
|
|
|
return queryset.none()
|
|
|
|
queryset = queryset.filter(applications=applications)
|
|
|
|
return queryset
|
|
|
|
|
|
|
|
def filter_queryset(self, queryset):
|
|
|
|
queryset = super().filter_queryset(queryset)
|
|
|
|
queryset = self.filter_application(queryset)
|
|
|
|
return queryset
|
|
|
|
|