mirror of https://github.com/jumpserver/jumpserver
fix: The applet list is not translated.
parent
36ae076cb0
commit
26cea550c4
|
@ -9,7 +9,7 @@ from django.conf import settings
|
|||
from django.core.files.storage import default_storage
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.utils.translation import gettext as _, get_language
|
||||
from django.utils.translation import gettext as _
|
||||
from rest_framework import viewsets
|
||||
from rest_framework.decorators import action
|
||||
from rest_framework.request import Request
|
||||
|
@ -20,7 +20,6 @@ from common.api import JMSBulkModelViewSet
|
|||
from common.serializers import FileSerializer
|
||||
from common.utils import is_uuid
|
||||
from common.utils.http import is_true
|
||||
from common.utils.yml import yaml_load_with_i18n
|
||||
from terminal import serializers
|
||||
from terminal.models import AppletPublication, Applet
|
||||
|
||||
|
@ -113,43 +112,6 @@ class AppletViewSet(DownloadUploadMixin, JMSBulkModelViewSet):
|
|||
obj = get_object_or_404(Applet, name=pk)
|
||||
else:
|
||||
obj = get_object_or_404(Applet, pk=pk)
|
||||
return self.trans_object(obj)
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
queryset = self.trans_queryset(queryset)
|
||||
return queryset
|
||||
|
||||
@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
|
||||
|
||||
def trans_queryset(self, queryset):
|
||||
for obj in queryset:
|
||||
self.trans_object(obj)
|
||||
return queryset
|
||||
|
||||
@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 trans_object(self, obj):
|
||||
lang = get_language()
|
||||
manifest = self.read_manifest_with_i18n(obj, lang)
|
||||
obj.display_name = manifest.get('display_name', obj.display_name)
|
||||
obj.comment = manifest.get('comment', obj.comment)
|
||||
obj.readme = self.readme(obj, lang)
|
||||
return obj
|
||||
|
||||
def is_record_found(self, obj, search):
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
from django.utils.translation import gettext_lazy as _
|
||||
import os
|
||||
|
||||
from django.utils.translation import gettext_lazy as _, get_language
|
||||
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 ..models import Applet, AppletPublication, AppletHost
|
||||
|
||||
|
@ -42,3 +45,31 @@ 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
|
||||
|
|
Loading…
Reference in New Issue