From 56deaa0bcbee4d55d6c79d86f9e206ca04071b50 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 29 Sep 2021 17:46:54 -0400 Subject: [PATCH] Initial back end support for user based languages --- app/classes/controllers/users_controller.py | 1 + app/classes/models/users.py | 8 +++++++- app/classes/web/panel_handler.py | 16 +++++++++++++++- .../templates/panel/panel_edit_user.html | 12 ++++++++++-- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/classes/controllers/users_controller.py b/app/classes/controllers/users_controller.py index a33f0d4b..8d27911b 100644 --- a/app/classes/controllers/users_controller.py +++ b/app/classes/controllers/users_controller.py @@ -65,6 +65,7 @@ class Users_Controller: elif base_data[key] != user_data[key]: up_data[key] = user_data[key] up_data['last_update'] = helper.get_time_as_string() + up_data['lang'] = user_data['lang'] logger.debug("user: {} +role:{} -role:{}".format(user_data, added_roles, removed_roles)) for role in added_roles: users_helper.get_or_create(user_id=user_id, role_id=role) diff --git a/app/classes/models/users.py b/app/classes/models/users.py index 24ff4291..ae12973d 100644 --- a/app/classes/models/users.py +++ b/app/classes/models/users.py @@ -40,7 +40,8 @@ class Users(Model): password = CharField(default="") enabled = BooleanField(default=True) superuser = BooleanField(default=False) - api_token = CharField(default="", unique=True, index=True) # we may need to revisit this + api_token = CharField(default="", unique=True, index=True), # we may need to revisit this + lang = CharField(default='en_EN') class Meta: table_name = "users" @@ -81,6 +82,10 @@ class helper_users: except DoesNotExist: return None + @staticmethod + def get_user_lang_by_id(user_id): + return Users.get(Users.user_id == user_id).lang + @staticmethod def get_user_by_api_token(token: str): query = Users.select().where(Users.api_token == token) @@ -114,6 +119,7 @@ class helper_users: 'api_token': None, 'roles': [], 'servers': [], + 'lang': 'en_EN' } user = model_to_dict(Users.get(Users.user_id == user_id)) diff --git a/app/classes/web/panel_handler.py b/app/classes/web/panel_handler.py index b9dc1fd2..2bfbbd5a 100644 --- a/app/classes/web/panel_handler.py +++ b/app/classes/web/panel_handler.py @@ -1,3 +1,4 @@ +from app.classes.shared.translation import Translation import json import logging import tornado.web @@ -363,6 +364,11 @@ class PanelHandler(BaseHandler): page_data['permissions_all'] = self.controller.crafty_perms.list_defined_crafty_permissions() page_data['permissions_list'] = set() page_data['quantity_server'] = self.controller.crafty_perms.list_all_crafty_permissions_quantity_limits() + page_data['languages'] = [] + for file in os.listdir(os.path.join(helper.root_dir, 'app', 'translations')): + if file.endswith('.json'): + page_data['languages'].append(file.split('.')[0]) + print(page_data['languages']) template = "panel/panel_edit_user.html" @@ -382,6 +388,10 @@ class PanelHandler(BaseHandler): page_data['permissions_all'] = self.controller.crafty_perms.list_defined_crafty_permissions() page_data['permissions_list'] = self.controller.crafty_perms.get_crafty_permissions_list(user_id) page_data['quantity_server'] = self.controller.crafty_perms.list_crafty_permissions_quantity_limits(user_id) + page_data['languages'] = [] + for file in os.listdir(os.path.join(helper.root_dir, 'app', 'translations')): + if file.endswith('.json'): + page_data['languages'].append(file.split('.')[0]) if user_id is None: self.redirect("/panel/error?error=Invalid User ID") @@ -690,6 +700,7 @@ class PanelHandler(BaseHandler): password1 = bleach.clean(self.get_argument('password1', None)) enabled = int(float(self.get_argument('enabled', '0'))) regen_api = int(float(self.get_argument('regen_api', '0'))) + lang = bleach.clean(self.get_argument('lang'), 'en_EN') if Enum_Permissions_Crafty.User_Config not in exec_user_crafty_permissions: if user_id != exec_user_id: @@ -762,6 +773,7 @@ class PanelHandler(BaseHandler): "enabled": enabled, "regen_api": regen_api, "roles": roles, + "lang": lang } user_crafty_data = { "permissions_mask": permissions_mask, @@ -780,7 +792,8 @@ class PanelHandler(BaseHandler): username = bleach.clean(self.get_argument('username', None)) password0 = bleach.clean(self.get_argument('password0', None)) password1 = bleach.clean(self.get_argument('password1', None)) - enabled = int(float(self.get_argument('enabled', '0'))) + enabled = int(float(self.get_argument('enabled', '0'))), + lang = bleach.clean(self.get_argument('lang', 'en_EN')) if Enum_Permissions_Crafty.User_Config not in exec_user_crafty_permissions: self.redirect("/panel/error?error=Unauthorized access: not a user editor") @@ -832,6 +845,7 @@ class PanelHandler(BaseHandler): user_id = self.controller.users.add_user(username, password=password0, enabled=enabled) user_data = { "roles": roles, + 'lang': lang } user_crafty_data = { "permissions_mask": permissions_mask, diff --git a/app/frontend/templates/panel/panel_edit_user.html b/app/frontend/templates/panel/panel_edit_user.html index 2fd401bd..80d6555b 100644 --- a/app/frontend/templates/panel/panel_edit_user.html +++ b/app/frontend/templates/panel/panel_edit_user.html @@ -54,9 +54,9 @@
{% if data['new_user'] %} -
+ {% else %} - + {% end %} {% raw xsrf_form_html() %} @@ -80,6 +80,14 @@
+
+ + +