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