From 145c7952c956abc6f080b51e936194ab4dabf966 Mon Sep 17 00:00:00 2001 From: ibuler Date: Thu, 13 Jan 2022 14:18:32 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=20ops=20=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=EF=BC=8C=E6=94=AF=E6=8C=81=20i18n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 内置任务名称支持了 i18n,数据库存英文,返回是翻译一下 - 任务执行中,添加 language 上下文 --- apps/assets/tasks/account_connectivity.py | 4 +- apps/assets/tasks/asset_connectivity.py | 11 +- .../tasks/gather_asset_hardware_info.py | 10 +- apps/assets/tasks/gather_asset_users.py | 4 +- apps/assets/tasks/push_system_user.py | 8 +- apps/assets/tasks/system_user_connectivity.py | 8 +- apps/common/utils/jumpserver.py | 4 +- apps/locale/zh/LC_MESSAGES/django.mo | 4 +- apps/locale/zh/LC_MESSAGES/django.po | 115 +++++++++--------- apps/ops/apps.py | 1 + apps/ops/models/adhoc.py | 13 +- apps/ops/serializers/adhoc.py | 2 +- apps/ops/signals_handler.py | 23 ++++ apps/ops/tasks.py | 8 +- 14 files changed, 127 insertions(+), 88 deletions(-) create mode 100644 apps/ops/signals_handler.py diff --git a/apps/assets/tasks/account_connectivity.py b/apps/assets/tasks/account_connectivity.py index 4327c0ffe..197979055 100644 --- a/apps/assets/tasks/account_connectivity.py +++ b/apps/assets/tasks/account_connectivity.py @@ -1,7 +1,7 @@ # ~*~ coding: utf-8 ~*~ from celery import shared_task -from django.utils.translation import ugettext as _ +from django.utils.translation import ugettext as _, gettext_noop from common.utils import get_logger from orgs.utils import org_aware_func @@ -104,6 +104,6 @@ def test_accounts_connectivity_manual(accounts): :param accounts: 对象 """ for account in accounts: - task_name = _("Test account connectivity: {}").format(account) + task_name = gettext_noop("Test account connectivity: ") + str(account) test_account_connectivity_util(account, task_name) print(".\n") diff --git a/apps/assets/tasks/asset_connectivity.py b/apps/assets/tasks/asset_connectivity.py index 1ae98fd1d..0038c38d3 100644 --- a/apps/assets/tasks/asset_connectivity.py +++ b/apps/assets/tasks/asset_connectivity.py @@ -2,7 +2,7 @@ from itertools import groupby from collections import defaultdict from celery import shared_task -from django.utils.translation import ugettext as _ +from django.utils.translation import gettext_noop from common.utils import get_logger from orgs.utils import org_aware_func @@ -46,7 +46,7 @@ def test_asset_connectivity_util(assets, task_name=None): from ops.utils import update_or_create_ansible_task if task_name is None: - task_name = _("Test assets connectivity") + task_name = gettext_noop("Test assets connectivity. ") hosts = clean_ansible_task_hosts(assets) if not hosts: @@ -88,7 +88,7 @@ def test_asset_connectivity_util(assets, task_name=None): @shared_task(queue="ansible") def test_asset_connectivity_manual(asset): - task_name = _("Test assets connectivity: {}").format(asset) + task_name = gettext_noop("Test assets connectivity: ") + str(asset) summary = test_asset_connectivity_util([asset], task_name=task_name) if summary.get('dark'): @@ -99,7 +99,7 @@ def test_asset_connectivity_manual(asset): @shared_task(queue="ansible") def test_assets_connectivity_manual(assets): - task_name = _("Test assets connectivity: {}").format([asset.hostname for asset in assets]) + task_name = gettext_noop("Test assets connectivity: ") + str([asset.hostname for asset in assets]) summary = test_asset_connectivity_util(assets, task_name=task_name) if summary.get('dark'): @@ -110,8 +110,7 @@ def test_assets_connectivity_manual(assets): @shared_task(queue="ansible") def test_node_assets_connectivity_manual(node): - task_name = _("Test if the assets under the node are connectable: {}".format(node.name)) + task_name = gettext_noop("Test if the assets under the node are connectable: ") + node.name assets = node.get_all_assets() result = test_asset_connectivity_util(assets, task_name=task_name) return result - diff --git a/apps/assets/tasks/gather_asset_hardware_info.py b/apps/assets/tasks/gather_asset_hardware_info.py index daad3d694..ae8107cb2 100644 --- a/apps/assets/tasks/gather_asset_hardware_info.py +++ b/apps/assets/tasks/gather_asset_hardware_info.py @@ -4,7 +4,7 @@ import json import re from celery import shared_task -from django.utils.translation import ugettext as _ +from django.utils.translation import ugettext as _, gettext_noop from common.utils import ( capacity_convert, sum_capacity, get_logger @@ -94,7 +94,7 @@ def update_assets_hardware_info_util(assets, task_name=None): """ from ops.utils import update_or_create_ansible_task if task_name is None: - task_name = _("Update some assets hardware info") + task_name = gettext_noop("Update some assets hardware info. ") tasks = const.UPDATE_ASSETS_HARDWARE_TASKS hosts = clean_ansible_task_hosts(assets) if not hosts: @@ -111,13 +111,13 @@ def update_assets_hardware_info_util(assets, task_name=None): @shared_task(queue="ansible") def update_asset_hardware_info_manual(asset): - task_name = _("Update asset hardware info: {}").format(asset.hostname) + task_name = gettext_noop("Update asset hardware info: ") + str(asset.hostname) update_assets_hardware_info_util([asset], task_name=task_name) @shared_task(queue="ansible") def update_assets_hardware_info_manual(assets): - task_name = _("Update assets hardware info: {}").format([asset.hostname for asset in assets]) + task_name = gettext_noop("Update assets hardware info: ") + str([asset.hostname for asset in assets]) update_assets_hardware_info_util(assets, task_name=task_name) @@ -134,7 +134,7 @@ def update_assets_hardware_info_period(): @shared_task(queue="ansible") def update_node_assets_hardware_info_manual(node): - task_name = _("Update node asset hardware information: {}").format(node.name) + task_name = gettext_noop("Update node asset hardware information: ") + str(node.name) assets = node.get_all_assets() result = update_assets_hardware_info_util(assets, task_name=task_name) return result diff --git a/apps/assets/tasks/gather_asset_users.py b/apps/assets/tasks/gather_asset_users.py index 6ff2993d7..abc69997a 100644 --- a/apps/assets/tasks/gather_asset_users.py +++ b/apps/assets/tasks/gather_asset_users.py @@ -4,7 +4,7 @@ import re from collections import defaultdict from celery import shared_task -from django.utils.translation import ugettext as _ +from django.utils.translation import gettext_noop from django.utils import timezone from orgs.utils import tmp_to_org, org_aware_func @@ -108,7 +108,7 @@ def add_asset_users(assets, results): def gather_asset_users(assets, task_name=None): from ops.utils import update_or_create_ansible_task if task_name is None: - task_name = _("Gather assets users") + task_name = gettext_noop("Gather assets users") assets = clean_ansible_task_hosts(assets) if not assets: return diff --git a/apps/assets/tasks/push_system_user.py b/apps/assets/tasks/push_system_user.py index 2be740eca..75e3dc019 100644 --- a/apps/assets/tasks/push_system_user.py +++ b/apps/assets/tasks/push_system_user.py @@ -3,7 +3,7 @@ from itertools import groupby from celery import shared_task from common.db.utils import get_object_if_need, get_objects -from django.utils.translation import ugettext as _ +from django.utils.translation import ugettext as _, gettext_noop from django.db.models import Empty, Q from common.utils import encrypt_password, get_logger @@ -279,7 +279,7 @@ def push_system_user_to_assets_manual(system_user, username=None): """ system_user = get_object_if_need(SystemUser, system_user) assets = system_user.get_related_assets() - task_name = _("Push system users to assets: {}").format(system_user.name) + task_name = gettext_noop("Push system users to assets: ") + system_user.name return push_system_user_util(system_user, assets, task_name=task_name, username=username) @@ -291,7 +291,7 @@ def push_system_user_a_asset_manual(system_user, asset, username=None): """ # if username is None: # username = system_user.username - task_name = _("Push system users to asset: {}({}) => {}").format( + task_name = gettext_noop("Push system users to asset: ") + "{}({}) => {}".format( system_user.name, username, asset ) return push_system_user_util(system_user, [asset], task_name=task_name, username=username) @@ -312,7 +312,7 @@ def push_system_user_to_assets(system_user_id, asset_ids, username=None): """ system_user = SystemUser.objects.get(id=system_user_id) assets = get_objects(Asset, asset_ids) - task_name = _("Push system users to assets: {}").format(system_user.name) + task_name = gettext_noop("Push system users to assets: ") + system_user.name return push_system_user_util(system_user, assets, task_name, username=username) diff --git a/apps/assets/tasks/system_user_connectivity.py b/apps/assets/tasks/system_user_connectivity.py index d7252fa72..893081163 100644 --- a/apps/assets/tasks/system_user_connectivity.py +++ b/apps/assets/tasks/system_user_connectivity.py @@ -3,7 +3,7 @@ from itertools import groupby from collections import defaultdict from celery import shared_task -from django.utils.translation import ugettext as _ +from django.utils.translation import ugettext as _, gettext_noop from assets.models import Asset from common.utils import get_logger @@ -115,7 +115,7 @@ def test_system_user_connectivity_util(system_user, assets, task_name): @shared_task(queue="ansible") @org_aware_func("system_user") def test_system_user_connectivity_manual(system_user, asset_ids=None): - task_name = _("Test system user connectivity: {}").format(system_user) + task_name = gettext_noop("Test system user connectivity: ") + str(system_user) if asset_ids: assets = Asset.objects.filter(id__in=asset_ids) else: @@ -126,7 +126,7 @@ def test_system_user_connectivity_manual(system_user, asset_ids=None): @shared_task(queue="ansible") @org_aware_func("system_user") def test_system_user_connectivity_a_asset(system_user, asset): - task_name = _("Test system user connectivity: {} => {}").format( + task_name = gettext_noop("Test system user connectivity: ") + "{} => {}".format( system_user, asset ) test_system_user_connectivity_util(system_user, [asset], task_name) @@ -145,7 +145,7 @@ def test_system_user_connectivity_period(): return queryset_map = SystemUser.objects.all_group_by_org() for org, system_user in queryset_map.items(): - task_name = _("Test system user connectivity period: {}").format(system_user) + task_name = gettext_noop("Test system user connectivity period: ") + str(system_user) with tmp_to_org(org): assets = system_user.get_related_assets() test_system_user_connectivity_util(system_user, assets, task_name) diff --git a/apps/common/utils/jumpserver.py b/apps/common/utils/jumpserver.py index 402cee610..c9396e1d4 100644 --- a/apps/common/utils/jumpserver.py +++ b/apps/common/utils/jumpserver.py @@ -1,6 +1,6 @@ from django.core.cache import cache -from django.shortcuts import reverse -from django.shortcuts import redirect +from django.shortcuts import reverse, redirect +from django.utils.translation import gettext_noop from .random import random_string diff --git a/apps/locale/zh/LC_MESSAGES/django.mo b/apps/locale/zh/LC_MESSAGES/django.mo index 1633ceafe..e14978a33 100644 --- a/apps/locale/zh/LC_MESSAGES/django.mo +++ b/apps/locale/zh/LC_MESSAGES/django.mo @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:942e981be66e5d0c32efb59583a377503ee3dc285e2794da40c312694c4a9dc2 -size 96378 +oid sha256:504b910235e3d0103d9bf2654bb542b7c4cba7ff1836c24fae4587b065c30fa0 +size 96229 diff --git a/apps/locale/zh/LC_MESSAGES/django.po b/apps/locale/zh/LC_MESSAGES/django.po index 47e01f362..ee3cac7d5 100644 --- a/apps/locale/zh/LC_MESSAGES/django.po +++ b/apps/locale/zh/LC_MESSAGES/django.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: JumpServer 0.3.3\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2022-01-12 20:51+0800\n" +"POT-Creation-Date: 2022-01-13 14:05+0800\n" "PO-Revision-Date: 2021-05-20 10:54+0800\n" "Last-Translator: ibuler \n" "Language-Team: JumpServer team\n" @@ -166,7 +166,7 @@ msgstr "格式为逗号分隔的字符串, * 表示匹配所有. " #: audits/models.py:105 authentication/forms.py:15 authentication/forms.py:17 #: authentication/templates/authentication/_msg_different_city.html:9 #: authentication/templates/authentication/_msg_oauth_bind.html:9 -#: ops/models/adhoc.py:148 users/forms/profile.py:31 users/models/user.py:547 +#: ops/models/adhoc.py:159 users/forms/profile.py:31 users/models/user.py:547 #: users/templates/users/_msg_user_created.html:12 #: users/templates/users/_select_user_modal.html:14 #: xpack/plugins/change_auth_plan/models/asset.py:34 @@ -363,7 +363,7 @@ msgid "Cluster" msgstr "集群" #: applications/serializers/attrs/application_category/db.py:11 -#: ops/models/adhoc.py:146 settings/serializers/auth/radius.py:14 +#: ops/models/adhoc.py:157 settings/serializers/auth/radius.py:14 #: xpack/plugins/cloud/serializers/account_attrs.py:68 msgid "Host" msgstr "主机" @@ -621,7 +621,7 @@ msgstr "开始日期" #: assets/models/backup.py:108 #: authentication/templates/authentication/_msg_oauth_bind.html:11 -#: notifications/notifications.py:187 ops/models/adhoc.py:246 +#: notifications/notifications.py:187 ops/models/adhoc.py:257 #: xpack/plugins/change_auth_plan/models/base.py:112 #: xpack/plugins/change_auth_plan/models/base.py:201 #: xpack/plugins/gathered_user/models.py:79 @@ -646,7 +646,7 @@ msgid "Reason" msgstr "原因" #: assets/models/backup.py:121 audits/serializers.py:76 -#: audits/serializers.py:91 ops/models/adhoc.py:248 +#: audits/serializers.py:91 ops/models/adhoc.py:259 #: terminal/serializers/session.py:35 #: xpack/plugins/change_auth_plan/models/base.py:199 msgid "Is success" @@ -1136,20 +1136,20 @@ msgid "The asset {} system platform {} does not support run Ansible tasks" msgstr "资产 {} 系统平台 {} 不支持运行 Ansible 任务" #: assets/tasks/account_connectivity.py:107 -msgid "Test account connectivity: {}" -msgstr "测试账号可连接性: {}" +msgid "Test account connectivity: " +msgstr "测试账号可连接性: " #: assets/tasks/asset_connectivity.py:49 -msgid "Test assets connectivity" -msgstr "测试资产可连接性" +msgid "Test assets connectivity. " +msgstr "测试资产可连接性. " #: assets/tasks/asset_connectivity.py:91 assets/tasks/asset_connectivity.py:102 -msgid "Test assets connectivity: {}" -msgstr "测试资产可连接性: {}" +msgid "Test assets connectivity: " +msgstr "测试资产可连接性: " #: assets/tasks/asset_connectivity.py:113 -msgid "Test if the assets under the node are connectable: {}" -msgstr "测试节点下资产是否可连接: {}" +msgid "Test if the assets under the node are connectable: " +msgstr "测试节点下资产是否可连接: " #: assets/tasks/const.py:49 msgid "Unreachable" @@ -1164,20 +1164,20 @@ msgid "Get asset info failed: {}" msgstr "获取资产信息失败:{}" #: assets/tasks/gather_asset_hardware_info.py:97 -msgid "Update some assets hardware info" -msgstr "更新资产硬件信息" +msgid "Update some assets hardware info. " +msgstr "更新资产硬件信息. " #: assets/tasks/gather_asset_hardware_info.py:114 -msgid "Update asset hardware info: {}" -msgstr "更新资产硬件信息: {}" +msgid "Update asset hardware info: " +msgstr "更新资产硬件信息: " #: assets/tasks/gather_asset_hardware_info.py:120 -msgid "Update assets hardware info: {}" -msgstr "更新资产硬件信息: {}" +msgid "Update assets hardware info: " +msgstr "更新资产硬件信息: " #: assets/tasks/gather_asset_hardware_info.py:137 -msgid "Update node asset hardware information: {}" -msgstr "更新节点资产硬件信息: {}" +msgid "Update node asset hardware information: " +msgstr "更新节点资产硬件信息: " #: assets/tasks/gather_asset_users.py:111 msgid "Gather assets users" @@ -1202,12 +1202,12 @@ msgid "Hosts count: {}" msgstr "主机数量: {}" #: assets/tasks/push_system_user.py:282 assets/tasks/push_system_user.py:315 -msgid "Push system users to assets: {}" -msgstr "推送系统用户到入资产: {}" +msgid "Push system users to assets: " +msgstr "推送系统用户到入资产: " #: assets/tasks/push_system_user.py:294 -msgid "Push system users to asset: {}({}) => {}" -msgstr "推送系统用户到入资产: {}({}) => {}" +msgid "Push system users to asset: " +msgstr "推送系统用户到入资产: " #: assets/tasks/system_user_connectivity.py:56 msgid "Dynamic system user not support test" @@ -1218,16 +1218,13 @@ msgid "Start test system user connectivity for platform: [{}]" msgstr "开始测试系统用户在该系统平台的可连接性: [{}]" #: assets/tasks/system_user_connectivity.py:118 -msgid "Test system user connectivity: {}" -msgstr "测试系统用户可连接性: {}" - #: assets/tasks/system_user_connectivity.py:129 -msgid "Test system user connectivity: {} => {}" -msgstr "测试系统用户可连接性: {} => {}" +msgid "Test system user connectivity: " +msgstr "测试系统用户可连接性: " #: assets/tasks/system_user_connectivity.py:148 -msgid "Test system user connectivity period: {}" -msgstr "定期测试系统用户可连接性: {}" +msgid "Test system user connectivity period: " +msgstr "定期测试系统用户可连接性: " #: assets/tasks/utils.py:17 msgid "Asset has been disabled, skipped: {}" @@ -1427,14 +1424,14 @@ msgid "Auth Token" msgstr "认证令牌" #: audits/signals_handler.py:68 authentication/notifications.py:73 -#: authentication/views/dingtalk.py:160 authentication/views/feishu.py:148 #: authentication/views/login.py:164 authentication/views/wecom.py:158 #: notifications/backends/__init__.py:11 users/models/user.py:607 msgid "WeCom" msgstr "企业微信" -#: audits/signals_handler.py:69 authentication/views/login.py:170 -#: notifications/backends/__init__.py:12 users/models/user.py:608 +#: audits/signals_handler.py:69 authentication/views/dingtalk.py:160 +#: authentication/views/login.py:170 notifications/backends/__init__.py:12 +#: users/models/user.py:608 msgid "DingTalk" msgstr "钉钉" @@ -2006,7 +2003,7 @@ msgstr "代码错误" #: authentication/templates/authentication/_msg_reset_password.html:3 #: authentication/templates/authentication/_msg_rest_password_success.html:2 #: authentication/templates/authentication/_msg_rest_public_key_success.html:2 -#: jumpserver/conf.py:293 +#: jumpserver/conf.py:293 ops/tasks.py:145 ops/tasks.py:148 #: perms/templates/perms/_msg_item_permissions_expire.html:3 #: perms/templates/perms/_msg_permed_items_expire.html:3 #: users/templates/users/_msg_account_expire_reminder.html:4 @@ -2206,6 +2203,11 @@ msgstr "飞书查询用户失败" msgid "The FeiShu is already bound to another user" msgstr "该飞书已经绑定其他用户" +#: authentication/views/feishu.py:148 authentication/views/login.py:176 +#: notifications/backends/__init__.py:14 users/models/user.py:609 +msgid "FeiShu" +msgstr "飞书" + #: authentication/views/feishu.py:149 msgid "Binding FeiShu successfully" msgstr "绑定 飞书 成功" @@ -2234,11 +2236,6 @@ msgstr "正在跳转到 {} 认证" msgid "Please enable cookies and try again." msgstr "设置你的浏览器支持cookie" -#: authentication/views/login.py:176 notifications/backends/__init__.py:14 -#: users/models/user.py:609 -msgid "FeiShu" -msgstr "飞书" - #: authentication/views/login.py:265 msgid "" "Wait for {} confirm, You also can copy link to her/him
\n" @@ -2558,56 +2555,56 @@ msgstr "单位: 时" msgid "Callback" msgstr "回调" -#: ops/models/adhoc.py:143 +#: ops/models/adhoc.py:154 msgid "Tasks" msgstr "任务" -#: ops/models/adhoc.py:144 +#: ops/models/adhoc.py:155 msgid "Pattern" msgstr "模式" -#: ops/models/adhoc.py:145 +#: ops/models/adhoc.py:156 msgid "Options" msgstr "选项" -#: ops/models/adhoc.py:147 +#: ops/models/adhoc.py:158 msgid "Run as admin" msgstr "再次执行" -#: ops/models/adhoc.py:150 +#: ops/models/adhoc.py:161 msgid "Become" msgstr "Become" -#: ops/models/adhoc.py:151 +#: ops/models/adhoc.py:162 msgid "Create by" msgstr "创建者" -#: ops/models/adhoc.py:240 +#: ops/models/adhoc.py:251 msgid "Task display" msgstr "任务名称" -#: ops/models/adhoc.py:242 +#: ops/models/adhoc.py:253 msgid "Host amount" msgstr "主机数量" -#: ops/models/adhoc.py:244 +#: ops/models/adhoc.py:255 msgid "Start time" msgstr "开始时间" -#: ops/models/adhoc.py:245 +#: ops/models/adhoc.py:256 msgid "End time" msgstr "完成时间" -#: ops/models/adhoc.py:247 ops/models/command.py:28 +#: ops/models/adhoc.py:258 ops/models/command.py:28 #: terminal/serializers/session.py:39 msgid "Is finished" msgstr "是否完成" -#: ops/models/adhoc.py:249 +#: ops/models/adhoc.py:260 msgid "Adhoc raw result" msgstr "结果" -#: ops/models/adhoc.py:250 +#: ops/models/adhoc.py:261 msgid "Adhoc result summary" msgstr "汇总" @@ -2655,11 +2652,11 @@ msgstr "内存使用率超过 {max_threshold}%: => {value}" msgid "CPU load more than {max_threshold}: => {value}" msgstr "CPU 使用率超过 {max_threshold}: => {value}" -#: ops/tasks.py:71 +#: ops/tasks.py:72 msgid "Clean task history period" msgstr "定期清除任务历史" -#: ops/tasks.py:84 +#: ops/tasks.py:85 msgid "Clean celery log period" msgstr "定期清除Celery日志" @@ -6311,6 +6308,12 @@ msgstr "旗舰版" msgid "Community edition" msgstr "社区版" +#~ msgid "Push system users to asset: {}({}) => {}" +#~ msgstr "推送系统用户到入资产: {}({}) => {}" + +#~ msgid "Test system user connectivity: {} => {}" +#~ msgstr "测试系统用户可连接性: {} => {}" + #~ msgid "Account backup plan execution" #~ msgstr "改密计划执行" diff --git a/apps/ops/apps.py b/apps/ops/apps.py index 5133c6655..43496ebf2 100644 --- a/apps/ops/apps.py +++ b/apps/ops/apps.py @@ -13,5 +13,6 @@ class OpsConfig(AppConfig): from orgs.utils import set_current_org set_current_org(Organization.root()) from .celery import signal_handler + from . import signals_handler from . import notifications super().ready() diff --git a/apps/ops/models/adhoc.py b/apps/ops/models/adhoc.py index 4c4c549f9..3e95799b5 100644 --- a/apps/ops/models/adhoc.py +++ b/apps/ops/models/adhoc.py @@ -9,7 +9,7 @@ from celery import current_task from django.db import models from django.conf import settings from django.utils import timezone -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import ugettext_lazy as _, gettext from common.utils import get_logger, lazyproperty from common.fields.model import ( @@ -58,6 +58,17 @@ class Task(PeriodTaskModelMixin, OrgModelMixin): else: return False + @lazyproperty + def display_name(self): + sps = ['. ', ': '] + spb = {str(sp in self.name): sp for sp in sps} + sp = spb.get('True') + if not sp: + return self.name + + tpl, data = self.name.split(sp, 1) + return gettext(tpl + sp) + data + @property def timedelta(self): if self.latest_execution: diff --git a/apps/ops/serializers/adhoc.py b/apps/ops/serializers/adhoc.py index badbed2b4..80619c34a 100644 --- a/apps/ops/serializers/adhoc.py +++ b/apps/ops/serializers/adhoc.py @@ -56,7 +56,7 @@ class TaskSerializer(BulkOrgResourceModelSerializer): class Meta: model = Task - fields_mini = ['id', 'name'] + fields_mini = ['id', 'name', 'display_name'] fields_small = fields_mini + [ 'interval', 'crontab', 'is_periodic', 'is_deleted', diff --git a/apps/ops/signals_handler.py b/apps/ops/signals_handler.py new file mode 100644 index 000000000..a4d6fb4eb --- /dev/null +++ b/apps/ops/signals_handler.py @@ -0,0 +1,23 @@ +from django.utils import translation +from django.core.cache import cache +from celery.signals import task_prerun, before_task_publish + + +TASK_LANG_CACHE_KEY = 'TASK_LANG_{}' +TASK_LANG_CACHE_TTL = 1800 + + +@before_task_publish.connect() +def before_task_publish(headers=None, **kwargs): + task_id = headers.get('id') + current_lang = translation.get_language() + key = TASK_LANG_CACHE_KEY.format(task_id) + cache.set(key, current_lang, 1800) + + +@task_prerun.connect() +def on_celery_task_pre_run(task_id='', **kwargs): + key = TASK_LANG_CACHE_KEY.format(task_id) + task_lang = cache.get(key) + if task_lang: + translation.activate(task_lang) diff --git a/apps/ops/tasks.py b/apps/ops/tasks.py index fb0c2e71c..e68b4c55c 100644 --- a/apps/ops/tasks.py +++ b/apps/ops/tasks.py @@ -5,9 +5,10 @@ import time from django.conf import settings from celery import shared_task, subtask + from celery.exceptions import SoftTimeLimitExceeded from django.utils import timezone -from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import ugettext_lazy as _, gettext from common.utils import get_logger, get_object_or_none, get_log_keep_day from orgs.utils import tmp_to_root_org, tmp_to_org @@ -141,10 +142,10 @@ def hello(name, callback=None): import time count = User.objects.count() - print("Hello {}".format(name)) + print(gettext("Hello") + ': ' + name) print("Count: ", count) time.sleep(1) - return count + return gettext("Hello") @shared_task @@ -177,3 +178,4 @@ def add_m(x): s.append(add.s(i)) res = chain(*tuple(s))() return res +