2023-08-15 05:45:44 +00:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
2023-12-05 02:58:19 +00:00
|
|
|
from assets.const import Protocol
|
2023-08-15 05:45:44 +00:00
|
|
|
from common.serializers.fields import EncryptedField
|
|
|
|
|
|
|
|
__all__ = [
|
2023-08-15 08:58:41 +00:00
|
|
|
'AnnouncementSettingSerializer', 'OpsSettingSerializer',
|
2023-12-05 02:58:19 +00:00
|
|
|
'VaultSettingSerializer', 'TicketSettingSerializer',
|
2023-12-20 07:53:48 +00:00
|
|
|
'ChatAISettingSerializer', 'VirtualAppSerializer',
|
2023-08-15 05:45:44 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class AnnouncementSerializer(serializers.Serializer):
|
|
|
|
ID = serializers.CharField(required=False, allow_blank=True, allow_null=True)
|
|
|
|
SUBJECT = serializers.CharField(required=True, max_length=1024, label=_("Subject"))
|
|
|
|
CONTENT = serializers.CharField(label=_("Content"))
|
|
|
|
LINK = serializers.URLField(
|
|
|
|
required=False, allow_null=True, allow_blank=True,
|
2024-01-15 11:41:50 +00:00
|
|
|
label=_("More URL"), default='',
|
2023-08-15 05:45:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def to_representation(self, instance):
|
|
|
|
defaults = {'ID': '', 'SUBJECT': '', 'CONTENT': '', 'LINK': '', 'ENABLED': False}
|
|
|
|
data = {**defaults, **instance}
|
|
|
|
return super().to_representation(data)
|
|
|
|
|
|
|
|
def to_internal_value(self, data):
|
|
|
|
data['ID'] = str(uuid.uuid4())
|
|
|
|
return super().to_internal_value(data)
|
|
|
|
|
|
|
|
|
|
|
|
class AnnouncementSettingSerializer(serializers.Serializer):
|
2023-08-15 08:58:41 +00:00
|
|
|
PREFIX_TITLE = _('Announcement')
|
|
|
|
|
2024-02-26 07:47:05 +00:00
|
|
|
ANNOUNCEMENT_ENABLED = serializers.BooleanField(label=_('Announcement'), default=True)
|
2023-08-15 05:45:44 +00:00
|
|
|
ANNOUNCEMENT = AnnouncementSerializer(label=_("Announcement"))
|
|
|
|
|
|
|
|
|
|
|
|
class VaultSettingSerializer(serializers.Serializer):
|
2023-08-17 07:34:50 +00:00
|
|
|
PREFIX_TITLE = _('HCP Vault')
|
2023-08-15 08:58:41 +00:00
|
|
|
|
2023-08-17 04:12:58 +00:00
|
|
|
VAULT_ENABLED = serializers.BooleanField(
|
2024-02-26 07:47:05 +00:00
|
|
|
required=False, label=_('Vault'), read_only=True
|
2023-08-15 05:45:44 +00:00
|
|
|
)
|
|
|
|
VAULT_HCP_HOST = serializers.CharField(
|
|
|
|
max_length=256, allow_blank=True, required=False, label=_('Host')
|
|
|
|
)
|
|
|
|
VAULT_HCP_TOKEN = EncryptedField(
|
|
|
|
max_length=256, allow_blank=True, required=False, label=_('Token'), default=''
|
|
|
|
)
|
|
|
|
VAULT_HCP_MOUNT_POINT = serializers.CharField(
|
|
|
|
max_length=256, allow_blank=True, required=False, label=_('Mount Point')
|
|
|
|
)
|
|
|
|
|
2024-01-02 11:07:58 +00:00
|
|
|
HISTORY_ACCOUNT_CLEAN_LIMIT = serializers.IntegerField(
|
|
|
|
default=999, max_value=999, min_value=1,
|
2024-01-03 03:25:45 +00:00
|
|
|
required=False, label=_('Historical accounts retained count'),
|
2024-01-02 11:07:58 +00:00
|
|
|
help_text=_(
|
|
|
|
'If the specific value is less than 999, '
|
|
|
|
'the system will automatically perform a task every night: '
|
|
|
|
'check and delete historical accounts that exceed the predetermined number. '
|
|
|
|
'If the value reaches or exceeds 999, no historical account deletion will be performed.'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2023-08-15 05:45:44 +00:00
|
|
|
|
2023-12-05 02:58:19 +00:00
|
|
|
class ChatAISettingSerializer(serializers.Serializer):
|
|
|
|
PREFIX_TITLE = _('Chat AI')
|
|
|
|
GPT_MODEL_CHOICES = []
|
|
|
|
|
|
|
|
CHAT_AI_ENABLED = serializers.BooleanField(
|
2024-02-26 07:47:05 +00:00
|
|
|
required=False, label=_('Chat AI')
|
2023-12-05 02:58:19 +00:00
|
|
|
)
|
|
|
|
GPT_BASE_URL = serializers.CharField(
|
2024-03-29 10:53:24 +00:00
|
|
|
allow_blank=True, required=False, label=_('Base Url')
|
2023-12-05 02:58:19 +00:00
|
|
|
)
|
|
|
|
GPT_API_KEY = EncryptedField(
|
2024-03-29 10:53:24 +00:00
|
|
|
allow_blank=True, required=False, label=_('API Key'),
|
2023-12-05 02:58:19 +00:00
|
|
|
)
|
|
|
|
GPT_PROXY = serializers.CharField(
|
2024-03-29 10:53:24 +00:00
|
|
|
allow_blank=True, required=False, label=_('Proxy')
|
2023-12-05 02:58:19 +00:00
|
|
|
)
|
|
|
|
GPT_MODEL = serializers.ChoiceField(
|
|
|
|
default='', choices=GPT_MODEL_CHOICES, label=_("GPT Model"), required=False,
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.set_GPT_MODEL_choices()
|
|
|
|
|
|
|
|
def set_GPT_MODEL_choices(self):
|
|
|
|
field_gpt_model = self.fields.get("GPT_MODEL")
|
|
|
|
if not field_gpt_model:
|
|
|
|
return
|
|
|
|
gpt_api_model = Protocol.gpt_protocols()[Protocol.chatgpt]['setting']['api_mode']
|
|
|
|
choices = gpt_api_model['choices']
|
2023-12-25 11:01:08 +00:00
|
|
|
field_gpt_model.choices = choices
|
2023-12-05 02:58:19 +00:00
|
|
|
field_gpt_model.default = gpt_api_model['default']
|
|
|
|
cls = self.__class__
|
|
|
|
if cls.GPT_MODEL_CHOICES:
|
|
|
|
return
|
|
|
|
cls.GPT_MODEL_CHOICES.extend(choices)
|
|
|
|
|
|
|
|
|
2023-08-15 05:45:44 +00:00
|
|
|
class TicketSettingSerializer(serializers.Serializer):
|
2023-08-15 08:58:41 +00:00
|
|
|
PREFIX_TITLE = _('Ticket')
|
|
|
|
|
2023-08-15 05:45:44 +00:00
|
|
|
TICKETS_ENABLED = serializers.BooleanField(required=False, default=True, label=_("Enable tickets"))
|
2024-02-19 03:18:01 +00:00
|
|
|
TICKETS_DIRECT_APPROVE = serializers.BooleanField(required=False, default=False, label=_("No login approval"))
|
2023-08-15 05:45:44 +00:00
|
|
|
TICKET_AUTHORIZE_DEFAULT_TIME = serializers.IntegerField(
|
|
|
|
min_value=1, max_value=999999, required=False,
|
2024-02-26 07:47:05 +00:00
|
|
|
label=_("Default period")
|
2023-08-15 05:45:44 +00:00
|
|
|
)
|
|
|
|
TICKET_AUTHORIZE_DEFAULT_TIME_UNIT = serializers.ChoiceField(
|
|
|
|
choices=[('day', _("day")), ('hour', _("hour"))],
|
2024-02-26 07:47:05 +00:00
|
|
|
label=_("Default unit"), required=False,
|
2023-08-15 05:45:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class OpsSettingSerializer(serializers.Serializer):
|
2023-08-15 08:58:41 +00:00
|
|
|
PREFIX_TITLE = _('Feature')
|
|
|
|
|
2023-08-15 05:45:44 +00:00
|
|
|
SECURITY_COMMAND_EXECUTION = serializers.BooleanField(
|
2024-02-26 07:47:05 +00:00
|
|
|
required=False, label=_('Job center'),
|
2023-08-15 05:45:44 +00:00
|
|
|
help_text=_('Allow user run batch command or not using ansible')
|
|
|
|
)
|
|
|
|
SECURITY_COMMAND_BLACKLIST = serializers.ListField(
|
|
|
|
child=serializers.CharField(max_length=1024, ),
|
2024-02-26 07:47:05 +00:00
|
|
|
label=_('Command blacklist'),
|
2023-08-15 05:45:44 +00:00
|
|
|
help_text=_("Commands that are not allowed execute.")
|
|
|
|
)
|
2023-12-20 07:53:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VirtualAppSerializer(serializers.Serializer):
|
|
|
|
PREFIX_TITLE = _('Virtual app')
|
|
|
|
|
|
|
|
VIRTUAL_APP_ENABLED = serializers.BooleanField(
|
2024-02-26 07:47:05 +00:00
|
|
|
required=False, label=_('Virtual app'),
|
2023-12-20 07:53:48 +00:00
|
|
|
)
|