mirror of https://github.com/jumpserver/jumpserver
perf: update action choice field default
parent
bc9782bd55
commit
4db15d9af7
|
@ -85,11 +85,11 @@ class BulkSerializerMixin(object):
|
|||
# since super by default strips out read-only fields
|
||||
# hence id will no longer be present in validated_data
|
||||
if all(
|
||||
[
|
||||
isinstance(self.root, BulkListSerializer),
|
||||
id_attr,
|
||||
request_method in ("PUT", "PATCH"),
|
||||
]
|
||||
[
|
||||
isinstance(self.root, BulkListSerializer),
|
||||
id_attr,
|
||||
request_method in ("PUT", "PATCH"),
|
||||
]
|
||||
):
|
||||
id_field = self.fields.get("id") or self.fields.get("pk")
|
||||
if data.get("id"):
|
||||
|
@ -322,9 +322,9 @@ class DefaultValueFieldsMixin:
|
|||
if model_field is None:
|
||||
continue
|
||||
if (
|
||||
not hasattr(model_field, "field")
|
||||
or not hasattr(model_field.field, "default")
|
||||
or model_field.field.default == NOT_PROVIDED
|
||||
not hasattr(model_field, "field")
|
||||
or not hasattr(model_field.field, "default")
|
||||
or model_field.field.default == NOT_PROVIDED
|
||||
):
|
||||
continue
|
||||
if name == "id":
|
||||
|
@ -335,7 +335,6 @@ class DefaultValueFieldsMixin:
|
|||
default = default()
|
||||
if default == "":
|
||||
continue
|
||||
# print(f"Set default value: {name}: {default}")
|
||||
serializer_field.default = default
|
||||
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
from django.db.models import Q, Count
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from rest_framework import serializers
|
||||
from rest_framework.fields import empty
|
||||
|
||||
from accounts.const import Source
|
||||
from accounts.models import AccountTemplate, Account
|
||||
|
@ -26,11 +27,21 @@ class ActionChoicesField(BitChoicesField):
|
|||
def to_file_internal_value(self, data):
|
||||
return data
|
||||
|
||||
@property
|
||||
def default(self):
|
||||
if self._default is empty:
|
||||
return self._default
|
||||
return self.to_representation(self._default)
|
||||
|
||||
@default.setter
|
||||
def default(self, value):
|
||||
self._default = value
|
||||
|
||||
|
||||
class PermAccountsSerializer(serializers.ListField):
|
||||
def get_render_help_text(self):
|
||||
return _('Accounts, format ["@virtual", "root", "%template_id"], '
|
||||
'virtual choices: @ALL, @SPEC, @USER, @ANON, @INPUT')
|
||||
'virtual choices: @ALL, @SPEC, @USER, @ANON, @INPUT')
|
||||
|
||||
|
||||
class PermProtocolsSerializer(serializers.ListField):
|
||||
|
@ -71,7 +82,7 @@ class AssetPermissionSerializer(ResourceLabelsMixin, BulkOrgResourceModelSeriali
|
|||
fields = fields_mini + fields_m2m + fields_generic
|
||||
read_only_fields = ["created_by", "date_created", "from_ticket"]
|
||||
extra_kwargs = {
|
||||
"actions": {"label": _("Action")},
|
||||
"actions": {"label": _("Action"), },
|
||||
"is_expired": {"label": _("Is expired")},
|
||||
"is_valid": {"label": _("Is valid")},
|
||||
}
|
||||
|
@ -84,7 +95,7 @@ class AssetPermissionSerializer(ResourceLabelsMixin, BulkOrgResourceModelSeriali
|
|||
actions = self.fields.get("actions")
|
||||
if not actions:
|
||||
return
|
||||
actions.default = list(actions.choices.keys())
|
||||
actions.default = ActionChoices.all()
|
||||
|
||||
@staticmethod
|
||||
def get_all_assets(nodes, assets):
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import uuid
|
||||
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
|
||||
class MenuPermission(models.Model):
|
||||
|
|
|
@ -31,9 +31,9 @@ class ApplyAssetSerializer(BaseApplyAssetSerializer, TicketApplySerializer):
|
|||
class Meta(TicketApplySerializer.Meta):
|
||||
model = ApplyAssetTicket
|
||||
writeable_fields = [
|
||||
'id', 'title', 'type', 'apply_nodes', 'apply_assets', 'apply_accounts',
|
||||
'apply_actions', 'apply_date_start', 'apply_date_expired',
|
||||
'comment', 'org_id'
|
||||
'id', 'title', 'type', 'apply_nodes', 'apply_assets',
|
||||
'apply_accounts', 'apply_actions', 'apply_date_start',
|
||||
'apply_date_expired', 'comment', 'org_id'
|
||||
]
|
||||
read_only_fields = TicketApplySerializer.Meta.read_only_fields + ['apply_permission_name', ]
|
||||
fields = TicketApplySerializer.Meta.fields_small + writeable_fields + read_only_fields
|
||||
|
@ -45,12 +45,22 @@ class ApplyAssetSerializer(BaseApplyAssetSerializer, TicketApplySerializer):
|
|||
}
|
||||
extra_kwargs.update(ticket_extra_kwargs)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
# self.set_actions_field()
|
||||
|
||||
def validate_apply_nodes(self, nodes):
|
||||
return self.filter_many_to_many_field(Node, nodes)
|
||||
|
||||
def validate_apply_assets(self, assets):
|
||||
return self.filter_many_to_many_field(Asset, assets)
|
||||
|
||||
def set_actions_field(self):
|
||||
actions = self.fields.get("apply_actions")
|
||||
if not actions:
|
||||
return
|
||||
actions.default = ['connect']
|
||||
|
||||
def validate(self, attrs):
|
||||
attrs['type'] = 'apply_asset'
|
||||
attrs = super().validate(attrs)
|
||||
|
|
Loading…
Reference in New Issue