fix: 抽象permissions-serializer的actions字段值设置

pull/7502/head
Michael Bai 2022-01-13 18:32:12 +08:00 committed by 老广
parent 21a34ddc03
commit ba695c4600
3 changed files with 26 additions and 16 deletions

View File

@ -6,14 +6,14 @@ from django.utils.translation import ugettext_lazy as _
from orgs.mixins.serializers import BulkOrgResourceModelSerializer from orgs.mixins.serializers import BulkOrgResourceModelSerializer
from perms.models import ApplicationPermission from perms.models import ApplicationPermission
from ..base import ActionsField from ..base import ActionsField, BasePermissionSerializer
__all__ = [ __all__ = [
'ApplicationPermissionSerializer' 'ApplicationPermissionSerializer'
] ]
class ApplicationPermissionSerializer(BulkOrgResourceModelSerializer): class ApplicationPermissionSerializer(BasePermissionSerializer):
actions = ActionsField(required=False, allow_null=True, label=_("Actions")) actions = ActionsField(required=False, allow_null=True, label=_("Actions"))
category_display = serializers.ReadOnlyField(source='get_category_display', label=_('Category display')) category_display = serializers.ReadOnlyField(source='get_category_display', label=_('Category display'))
type_display = serializers.ReadOnlyField(source='get_type_display', label=_('Type display')) type_display = serializers.ReadOnlyField(source='get_type_display', label=_('Type display'))
@ -46,15 +46,7 @@ class ApplicationPermissionSerializer(BulkOrgResourceModelSerializer):
'applications_amount': {'label': _('Applications amount')}, 'applications_amount': {'label': _('Applications amount')},
} }
def __init__(self, *args, **kwargs): def _filter_actions_choices(self, choices):
super().__init__(*args, **kwargs)
self.set_actions_choices()
def set_actions_choices(self):
actions = self.fields.get('actions')
if not actions:
return
choices = actions._choices
if request := self.context.get('request'): if request := self.context.get('request'):
category = request.query_params.get('category') category = request.query_params.get('category')
else: else:
@ -62,8 +54,7 @@ class ApplicationPermissionSerializer(BulkOrgResourceModelSerializer):
exclude_choices = ApplicationPermission.get_exclude_actions_choices(category=category) exclude_choices = ApplicationPermission.get_exclude_actions_choices(category=category)
for choice in exclude_choices: for choice in exclude_choices:
choices.pop(choice, None) choices.pop(choice, None)
actions._choices = choices return choices
actions.default = list(choices.keys())
@classmethod @classmethod
def setup_eager_loading(cls, queryset): def setup_eager_loading(cls, queryset):

View File

@ -9,12 +9,12 @@ from orgs.mixins.serializers import BulkOrgResourceModelSerializer
from perms.models import AssetPermission, Action from perms.models import AssetPermission, Action
from assets.models import Asset, Node, SystemUser from assets.models import Asset, Node, SystemUser
from users.models import User, UserGroup from users.models import User, UserGroup
from ..base import ActionsField from ..base import ActionsField, BasePermissionSerializer
__all__ = ['AssetPermissionSerializer'] __all__ = ['AssetPermissionSerializer']
class AssetPermissionSerializer(BulkOrgResourceModelSerializer): class AssetPermissionSerializer(BasePermissionSerializer):
actions = ActionsField(required=False, allow_null=True, label=_("Actions")) actions = ActionsField(required=False, allow_null=True, label=_("Actions"))
is_valid = serializers.BooleanField(read_only=True, label=_("Is valid")) is_valid = serializers.BooleanField(read_only=True, label=_("Is valid"))
is_expired = serializers.BooleanField(read_only=True, label=_('Is expired')) is_expired = serializers.BooleanField(read_only=True, label=_('Is expired'))

View File

@ -1,7 +1,8 @@
from rest_framework import serializers from rest_framework import serializers
from perms.models import Action from perms.models import Action
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
__all__ = ['ActionsDisplayField', 'ActionsField'] __all__ = ['ActionsDisplayField', 'ActionsField', 'BasePermissionSerializer']
class ActionsField(serializers.MultipleChoiceField): class ActionsField(serializers.MultipleChoiceField):
@ -24,3 +25,21 @@ class ActionsDisplayField(ActionsField):
choices = dict(Action.CHOICES) choices = dict(Action.CHOICES)
return [choices.get(i) for i in values] return [choices.get(i) for i in values]
class BasePermissionSerializer(BulkOrgResourceModelSerializer):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.set_actions_field()
def set_actions_field(self):
actions = self.fields.get('actions')
if not actions:
return
choices = actions._choices
choices = self._filter_actions_choices(choices)
actions._choices = choices
actions.default = list(choices.keys())
def _filter_actions_choices(self, choices):
return choices