Initial back end support for user based languages

This commit is contained in:
Andrew 2021-09-29 17:46:54 -04:00
parent e976593d83
commit 56deaa0bcb
4 changed files with 33 additions and 4 deletions

View File

@ -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)

View File

@ -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))

View File

@ -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,

View File

@ -54,9 +54,9 @@
<div class="row">
<div class="col-md-6 col-sm-12">
{% if data['new_user'] %}
<form class="forms-sample" method="post" action="/panel/add_user">
<form id="user_form" class="forms-sample" method="post" action="/panel/add_user">
{% else %}
<form class="forms-sample" method="post" action="/panel/edit_user">
<form id="user_form" class="forms-sample" method="post" action="/panel/edit_user">
{% end %}
{% raw xsrf_form_html() %}
<input type="hidden" name="id" value="{{ data['user']['user_id'] }}">
@ -80,6 +80,14 @@
<label for="password1">Repeat Password <small class="text-muted ml-1"> - leave blank to don't change</small> </label>
<input type="password" class="form-control" name="password1" id="password1" value="" placeholder="Repeat Password" >
</div>
<div class="form-group">
<label for="language">User Language:</label>
<select id="language" name="language" form="user_form">
{% for lang in data['languages'] %}
<option value="{{lang}}">{{lang}}</option>
{% end %}
</select>
</div>
</div>
</div>