From 1ddc228449be894a584a469c2123e7a25e7b85c6 Mon Sep 17 00:00:00 2001 From: Bai Date: Thu, 29 Oct 2020 17:47:38 +0800 Subject: [PATCH] =?UTF-8?q?perf(application):=20RemoteApp=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E7=B1=BB=E5=AD=97=E6=AE=B5asset=EF=BC=8C=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E4=B8=BACharPrimaryKeyRelatedField=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=85=B6=E4=BB=96=E5=AD=97=E6=AE=B5=E7=9A=84required?= =?UTF-8?q?=3DFlase?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/applications/serializers/remote_app.py | 50 ++++++++++++++------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/apps/applications/serializers/remote_app.py b/apps/applications/serializers/remote_app.py index 81a18caec..277ff6da2 100644 --- a/apps/applications/serializers/remote_app.py +++ b/apps/applications/serializers/remote_app.py @@ -3,6 +3,7 @@ import copy from django.utils.translation import ugettext_lazy as _ +from django.core.exceptions import ObjectDoesNotExist from rest_framework import serializers from common.serializers import AdaptedBulkListSerializer @@ -14,42 +15,61 @@ from .. import const from ..models import RemoteApp, Category, Application +class CharPrimaryKeyRelatedField(serializers.PrimaryKeyRelatedField): + + def to_internal_value(self, data): + if self.pk_field is not None: + data = self.pk_field.to_internal_value(data) + try: + instance = self.get_queryset().get(pk=data) + return str(instance.id) + except ObjectDoesNotExist: + self.fail('does_not_exist', pk_value=data) + except (TypeError, ValueError): + self.fail('incorrect_type', data_type=type(data).__name__) + + def to_representation(self, value): + if self.pk_field is not None: + return self.pk_field.to_representation(value) + return value + + class RemoteAppAttrsSerializer(serializers.Serializer): - asset = serializers.CharField(max_length=36, label=_('Assets')) + asset = CharPrimaryKeyRelatedField(queryset=Asset.objects, label=_("Assets")) path = serializers.CharField(max_length=128, label=_('Remote App path')) class ChromeAttrsSerializer(RemoteAppAttrsSerializer): REMOTE_APP_PATH = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' path = serializers.CharField(max_length=128, label=_('Remote App path'), default=REMOTE_APP_PATH) - chrome_target = serializers.CharField(max_length=128, label=_('Target URL')) - chrome_username = serializers.CharField(max_length=128, label=_('Username')) - chrome_password = serializers.CharField(max_length=128, write_only=True, label=_('Password')) + chrome_target = serializers.CharField(max_length=128, allow_blank=True, required=False, label=_('Target URL')) + chrome_username = serializers.CharField(max_length=128, allow_blank=True, required=False, label=_('Username')) + chrome_password = serializers.CharField(max_length=128, allow_blank=True, required=False, write_only=True, label=_('Password')) class MySQLWorkbenchAttrsSerializer(RemoteAppAttrsSerializer): REMOTE_APP_PATH = 'C:\Program Files\MySQL\MySQL Workbench 8.0 CE\MySQLWorkbench.exe' path = serializers.CharField(max_length=128, label=_('Remote App path'), default=REMOTE_APP_PATH) - mysql_workbench_ip = serializers.CharField(max_length=128, label=_('IP')) + mysql_workbench_ip = serializers.CharField(max_length=128, allow_blank=True, required=False, label=_('IP')) mysql_workbench_port = serializers.IntegerField(label=_('Port')) - mysql_workbench_name = serializers.CharField(max_length=128, label=_('Database')) - mysql_workbench_username = serializers.CharField(max_length=128, label=_('Username')) - mysql_workbench_password = serializers.CharField(max_length=128, write_only=True, label=_('Password')) + mysql_workbench_name = serializers.CharField(max_length=128, allow_blank=True, required=False, label=_('Database')) + mysql_workbench_username = serializers.CharField(max_length=128, allow_blank=True, required=False, label=_('Username')) + mysql_workbench_password = serializers.CharField(max_length=128, allow_blank=True, required=False, write_only=True, label=_('Password')) class VMwareClientAttrsSerializer(RemoteAppAttrsSerializer): REMOTE_APP_PATH = 'C:\Program Files (x86)\VMware\Infrastructure\Virtual Infrastructure Client\Launcher\VpxClient.exe' path = serializers.CharField(max_length=128, label=_('Remote App path'), default=REMOTE_APP_PATH) - vmware_target = serializers.CharField(max_length=128, label=_('Target URL')) - vmware_username = serializers.CharField(max_length=128, label=_('Username')) - vmware_password = serializers.CharField(max_length=128, write_only=True, label=_('Password')) + vmware_target = serializers.CharField(max_length=128, allow_blank=True, required=False, label=_('Target URL')) + vmware_username = serializers.CharField(max_length=128, allow_blank=True, required=False, label=_('Username')) + vmware_password = serializers.CharField(max_length=128, allow_blank=True, required=False, write_only=True, label=_('Password')) class CustomRemoteAppAttrsSeralizers(RemoteAppAttrsSerializer): - custom_cmdline = serializers.CharField(max_length=128, label=_('Operating parameter')) - custom_target = serializers.CharField(max_length=128, label=_('Target url')) - custom_username = serializers.CharField(max_length=128, label=_('Username')) - custom_password = serializers.CharField(max_length=128, write_only=True, label=_('Password')) + custom_cmdline = serializers.CharField(max_length=128, allow_blank=True, required=False, label=_('Operating parameter')) + custom_target = serializers.CharField(max_length=128, allow_blank=True, required=False, label=_('Target url')) + custom_username = serializers.CharField(max_length=128, allow_blank=True, required=False, label=_('Username')) + custom_password = serializers.CharField(max_length=128, allow_blank=True, required=False, write_only=True, label=_('Password')) class RemoteAppConnectionInfoSerializer(serializers.ModelSerializer):