mirror of https://github.com/jumpserver/jumpserver
fix: Virtual apps manifest i18n
parent
8e007004c2
commit
144f4b4466
|
@ -1,3 +1,8 @@
|
|||
import os
|
||||
|
||||
from django.utils.translation import get_language
|
||||
|
||||
from common.utils.yml import yaml_load_with_i18n
|
||||
from terminal.utils.loki_client import get_loki_client
|
||||
|
||||
__all__ = ['LokiMixin', ]
|
||||
|
@ -16,3 +21,33 @@ class LokiMixin:
|
|||
stream_selector = '{component=~"%s"}' % components
|
||||
query = f'{stream_selector} |="{search}"'
|
||||
return query
|
||||
|
||||
|
||||
class ManifestI18nMixin:
|
||||
@staticmethod
|
||||
def read_manifest_with_i18n(obj, lang='zh'):
|
||||
path = os.path.join(obj.path, 'manifest.yml')
|
||||
if os.path.exists(path):
|
||||
with open(path, encoding='utf8') as f:
|
||||
manifest = yaml_load_with_i18n(f, lang)
|
||||
else:
|
||||
manifest = {}
|
||||
return manifest
|
||||
|
||||
@staticmethod
|
||||
def readme(obj, lang=''):
|
||||
lang = lang[:2]
|
||||
readme_file = os.path.join(obj.path, f'README_{lang.upper()}.md')
|
||||
if os.path.isfile(readme_file):
|
||||
with open(readme_file, 'r') as f:
|
||||
return f.read()
|
||||
return ''
|
||||
|
||||
def to_representation(self, instance):
|
||||
data = super().to_representation(instance)
|
||||
lang = get_language()
|
||||
manifest = self.read_manifest_with_i18n(instance, lang)
|
||||
data['display_name'] = manifest.get('display_name', getattr(instance, 'display_name', ''))
|
||||
data['comment'] = manifest.get('comment', getattr(instance, 'comment', ''))
|
||||
data['readme'] = self.readme(instance, lang)
|
||||
return data
|
||||
|
|
|
@ -1,12 +1,10 @@
|
|||
import os
|
||||
|
||||
from django.utils.translation import gettext_lazy as _, get_language
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from rest_framework import serializers
|
||||
|
||||
from common.const.choices import Status
|
||||
from common.serializers.fields import ObjectRelatedField, LabeledChoiceField
|
||||
from common.utils.yml import yaml_load_with_i18n
|
||||
from terminal.const import PublishStatus
|
||||
from ..mixin import ManifestI18nMixin
|
||||
from ..models import Applet, AppletPublication, AppletHost
|
||||
|
||||
__all__ = [
|
||||
|
@ -27,7 +25,7 @@ class AppletPublicationSerializer(serializers.ModelSerializer):
|
|||
fields = fields_mini + ['status', 'comment'] + read_only_fields
|
||||
|
||||
|
||||
class AppletSerializer(serializers.ModelSerializer):
|
||||
class AppletSerializer(ManifestI18nMixin, serializers.ModelSerializer):
|
||||
icon = serializers.ReadOnlyField(label=_("Icon"))
|
||||
type = LabeledChoiceField(choices=Applet.Type.choices, label=_("Type"))
|
||||
edition = LabeledChoiceField(
|
||||
|
@ -45,31 +43,3 @@ class AppletSerializer(serializers.ModelSerializer):
|
|||
'version', 'author', 'type', 'edition',
|
||||
'can_concurrent', 'protocols', 'tags', 'comment',
|
||||
] + read_only_fields
|
||||
|
||||
@staticmethod
|
||||
def read_manifest_with_i18n(obj, lang='zh'):
|
||||
path = os.path.join(obj.path, 'manifest.yml')
|
||||
if os.path.exists(path):
|
||||
with open(path, encoding='utf8') as f:
|
||||
manifest = yaml_load_with_i18n(f, lang)
|
||||
else:
|
||||
manifest = {}
|
||||
return manifest
|
||||
|
||||
@staticmethod
|
||||
def readme(obj, lang=''):
|
||||
lang = lang[:2]
|
||||
readme_file = os.path.join(obj.path, f'README_{lang.upper()}.md')
|
||||
if os.path.isfile(readme_file):
|
||||
with open(readme_file, 'r') as f:
|
||||
return f.read()
|
||||
return ''
|
||||
|
||||
def to_representation(self, instance):
|
||||
data = super().to_representation(instance)
|
||||
lang = get_language()
|
||||
manifest = self.read_manifest_with_i18n(instance, lang)
|
||||
data['display_name'] = manifest.get('display_name', instance.display_name)
|
||||
data['comment'] = manifest.get('comment', instance.comment)
|
||||
data['readme'] = self.readme(instance, lang)
|
||||
return data
|
||||
|
|
|
@ -4,6 +4,7 @@ from rest_framework import serializers
|
|||
from common.const.choices import Status
|
||||
from common.serializers.fields import ObjectRelatedField, LabeledChoiceField
|
||||
from terminal.const import PublishStatus
|
||||
from ..mixin import ManifestI18nMixin
|
||||
from ..models import VirtualApp, VirtualAppPublication, AppProvider
|
||||
|
||||
__all__ = [
|
||||
|
@ -11,7 +12,7 @@ __all__ = [
|
|||
]
|
||||
|
||||
|
||||
class VirtualAppSerializer(serializers.ModelSerializer):
|
||||
class VirtualAppSerializer(ManifestI18nMixin, serializers.ModelSerializer):
|
||||
icon = serializers.ReadOnlyField(label=_("Icon"))
|
||||
image_protocol = serializers.CharField(max_length=16, default='vnc')
|
||||
image_port = serializers.IntegerField(default=5900)
|
||||
|
|
Loading…
Reference in New Issue