Fixes in session admin
This commit is contained in:
parent
9a6403a5f4
commit
4852ad04aa
6 changed files with 100 additions and 14 deletions
|
|
@ -106,11 +106,36 @@ class I18n:
|
||||||
|
|
||||||
l={}
|
l={}
|
||||||
|
|
||||||
def __init__(self, module, default_lang=None):
|
def __init__(self, module, default_lang='en-US'):
|
||||||
|
|
||||||
self.module=module
|
self.module=module
|
||||||
self.default_lang=I18n.default_lang
|
|
||||||
|
|
||||||
|
self.default_lang=default_lang
|
||||||
|
|
||||||
|
def clang(self, module, symbol, text_default):
|
||||||
|
"""Static method for get a string from selected language
|
||||||
|
|
||||||
|
Static method used to get the string of the selected language. If there is no string in the selected language, it returns text_default.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
module (str): The module to which the translation string belongs
|
||||||
|
symbol (str): Simple symbol that is useful for identify the string
|
||||||
|
text_default (str): The text used by default when there are not translation in the selected language
|
||||||
|
"""
|
||||||
|
|
||||||
|
#if not lang:
|
||||||
|
# lang=I18n.get_default_lang()
|
||||||
|
|
||||||
|
lang=self.default_lang
|
||||||
|
|
||||||
|
I18n.l[lang]=I18n.l.get(lang, {})
|
||||||
|
|
||||||
|
I18n.l[lang][module]=I18n.l[lang].get(module, {})
|
||||||
|
|
||||||
|
I18n.l[lang][module][symbol]=I18n.l[lang][module].get(symbol, text_default)
|
||||||
|
|
||||||
|
return I18n.l[lang][module][symbol]
|
||||||
|
|
||||||
def slang(self, symbol, text_default, lang=None):
|
def slang(self, symbol, text_default, lang=None):
|
||||||
"""Method for get a string from selected language but object oriented
|
"""Method for get a string from selected language but object oriented
|
||||||
|
|
||||||
|
|
@ -120,7 +145,7 @@ class I18n:
|
||||||
symbol (str): The symbol used for identify the text string.
|
symbol (str): The symbol used for identify the text string.
|
||||||
text_default (str): The text default used. You have use how base for translations.
|
text_default (str): The text default used. You have use how base for translations.
|
||||||
"""
|
"""
|
||||||
return I18n.lang(self.module, symbol, text_default, lang)
|
return self.clang(self.module, symbol, text_default, lang)
|
||||||
|
|
||||||
def tlang(self, text_default, lang=None):
|
def tlang(self, text_default, lang=None):
|
||||||
"""Method for get a string from selected language but object oriented and using module and symbol by default
|
"""Method for get a string from selected language but object oriented and using module and symbol by default
|
||||||
|
|
@ -134,7 +159,7 @@ class I18n:
|
||||||
|
|
||||||
symbol=text_default[:60]
|
symbol=text_default[:60]
|
||||||
|
|
||||||
return I18n.lang(self.module, symbol, text_default)
|
return self.clang(self.module, symbol, text_default)
|
||||||
|
|
||||||
#@staticmethod
|
#@staticmethod
|
||||||
#def set_lang(code_lang):
|
#def set_lang(code_lang):
|
||||||
|
|
@ -177,6 +202,16 @@ class I18n:
|
||||||
|
|
||||||
return I18n.l[lang][module][symbol]
|
return I18n.l[lang][module][symbol]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def add_lang(lang, module, symbol, text):
|
||||||
|
|
||||||
|
I18n.l[lang]=I18n.l.get(lang, {})
|
||||||
|
|
||||||
|
I18n.l[lang][module]=I18n.l[lang].get(module, {})
|
||||||
|
|
||||||
|
I18n.l[lang][module][symbol]=text
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def extract_value(value):
|
def extract_value(value):
|
||||||
"""Static method for get values from json lang array
|
"""Static method for get values from json lang array
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ from settings import config
|
||||||
#from parameciofast.fast import app
|
#from parameciofast.fast import app
|
||||||
from parameciofast.libraries.session import ParamecioSession
|
from parameciofast.libraries.session import ParamecioSession
|
||||||
from starlette.middleware.sessions import SessionMiddleware
|
from starlette.middleware.sessions import SessionMiddleware
|
||||||
|
from parameciofast.fast import app
|
||||||
|
|
||||||
cookie_name='paramecio_session'
|
cookie_name='paramecio_session'
|
||||||
|
|
||||||
|
|
@ -16,4 +17,20 @@ url_app=config.apps['admin'][2]
|
||||||
|
|
||||||
admin_app=FastAPI(docs_url="/docs", openapi_url="/docs/openapi.json", title='Paramecio Admin', version='0.9')
|
admin_app=FastAPI(docs_url="/docs", openapi_url="/docs/openapi.json", title='Paramecio Admin', version='0.9')
|
||||||
|
|
||||||
|
# FastAPI set the middlewares in reversed order.
|
||||||
|
|
||||||
|
@admin_app.middleware("http")
|
||||||
|
async def check_session(request: Request, call_next):
|
||||||
|
|
||||||
|
#print(request.scope['route'].name)
|
||||||
|
|
||||||
|
response = await call_next(request)
|
||||||
|
|
||||||
|
if not request.session.get('login_admin', None) and request.scope['endpoint'].__name__!='login_admin' and request.scope['endpoint'].__name__!='signup_admin' and request.scope['endpoint'].__name__!='check_login_admin' and request.scope['endpoint'].__name__!='signup_insert_admin' and request.scope['endpoint'].__name__!='logout_admin':
|
||||||
|
|
||||||
|
return RedirectResponse(request.url_for('login_admin'))
|
||||||
|
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
admin_app.add_middleware(SessionMiddleware, secret_key=config.secret_key, max_age=None, session_cookie=cookie_name, path=url_app)
|
admin_app.add_middleware(SessionMiddleware, secret_key=config.secret_key, max_age=None, session_cookie=cookie_name, path=url_app)
|
||||||
|
|
|
||||||
|
|
@ -38,13 +38,9 @@ if hasattr(config, 'cookie_name'):
|
||||||
@admin_app.get('/', response_class=HTMLResponse)
|
@admin_app.get('/', response_class=HTMLResponse)
|
||||||
def home_admin(request: Request, paramecio_session: Annotated[str | None, Cookie(description='Cookie for validate into the admin site. The cookie name can change in you settings/config.py')] = None, remote_address: Annotated[str | None, Header()] = None):
|
def home_admin(request: Request, paramecio_session: Annotated[str | None, Cookie(description='Cookie for validate into the admin site. The cookie name can change in you settings/config.py')] = None, remote_address: Annotated[str | None, Header()] = None):
|
||||||
|
|
||||||
if not request.session.get('login_admin', None):
|
|
||||||
|
|
||||||
return RedirectResponse(app.url_path_for('login_admin'))
|
|
||||||
|
|
||||||
i18n=I18n('admin', I18n.session_lang(request.session))
|
i18n=I18n('admin', I18n.session_lang(request.session))
|
||||||
|
|
||||||
return t.load_template('layout.phtml', title=i18n.tlang('Admin'), tlang=i18n.tlang, url_for=app.url_path_for)
|
return t.load_template('layout.phtml', title=i18n.tlang('Admin'), tlang=i18n.tlang, url_for=app.url_path_for, module_selected='home_admin')
|
||||||
|
|
||||||
|
|
||||||
@admin_app.get('/login', response_class=HTMLResponse)
|
@admin_app.get('/login', response_class=HTMLResponse)
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ from fastapi.responses import HTMLResponse
|
||||||
from parameciofast.modules.fastadmin import admin_app
|
from parameciofast.modules.fastadmin import admin_app
|
||||||
from parameciofast.modules.fastadmin.libraries.config import modules_admin, modules_admin_icons
|
from parameciofast.modules.fastadmin.libraries.config import modules_admin, modules_admin_icons
|
||||||
from parameciofast.libraries.mtemplates import PTemplate, env_theme
|
from parameciofast.libraries.mtemplates import PTemplate, env_theme
|
||||||
|
from parameciofast.libraries.i18n import I18n
|
||||||
|
import parameciofast.modules.fastadmin.libraries.i18n
|
||||||
import os
|
import os
|
||||||
|
|
||||||
env=env_theme(__file__)
|
env=env_theme(__file__)
|
||||||
|
|
@ -15,7 +17,7 @@ tpl_path=os.path.dirname(__file__).replace('/admin', '')+'/templates/admin'
|
||||||
if t.env.directories[1]!=tpl_path:
|
if t.env.directories[1]!=tpl_path:
|
||||||
t.env.directories.insert(1, tpl_path)
|
t.env.directories.insert(1, tpl_path)
|
||||||
|
|
||||||
modules_admin.append(['Admin users', 'fastadmin_ausers', 'people-circle'])
|
modules_admin.append(['fastadmin_ausers', 'people-circle'])
|
||||||
|
|
||||||
modules_admin_icons.append('<symbol id="people-circle" viewBox="0 0 16 16"><path d="M11 6a3 3 0 1 1-6 0 3 3 0 0 1 6 0z"/><path fill-rule="evenodd" d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8zm8-7a7 7 0 0 0-5.468 11.37C3.242 11.226 4.805 10 8 10s4.757 1.225 5.468 2.37A7 7 0 0 0 8 1z"/></symbol>')
|
modules_admin_icons.append('<symbol id="people-circle" viewBox="0 0 16 16"><path d="M11 6a3 3 0 1 1-6 0 3 3 0 0 1 6 0z"/><path fill-rule="evenodd" d="M0 8a8 8 0 1 1 16 0A8 8 0 0 1 0 8zm8-7a7 7 0 0 0-5.468 11.37C3.242 11.226 4.805 10 8 10s4.757 1.225 5.468 2.37A7 7 0 0 0 8 1z"/></symbol>')
|
||||||
|
|
||||||
|
|
|
||||||
19
parameciofast/modules/fastadmin/libraries/i18n.py
Normal file
19
parameciofast/modules/fastadmin/libraries/i18n.py
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
from parameciofast.libraries.i18n import I18n
|
||||||
|
|
||||||
|
"""
|
||||||
|
I18n.l['en-US']=I18n.l.get('en-US', {})
|
||||||
|
|
||||||
|
I18n.l['en-US']['fastadmin']=I18n.l['en-US'].get('fastadmin', {})
|
||||||
|
|
||||||
|
I18n.l['en-US']['fastadmin']['fastadmin_ausers']='Admin users'
|
||||||
|
|
||||||
|
I18n.l['es-ES']=I18n.l.get('es-ES', {})
|
||||||
|
|
||||||
|
I18n.l['es-ES']['fastadmin']=I18n.l['en-US'].get('fastadmin', {})
|
||||||
|
|
||||||
|
I18n.l['es-ES']['fastadmin']['fastadmin_ausers']='Usuarios admin'
|
||||||
|
"""
|
||||||
|
|
||||||
|
I18n.add_lang('en-US', 'fastadmin', 'fastadmin_ausers', 'Admin users')
|
||||||
|
I18n.add_lang('es-ES', 'fastadmin', 'fastadmin_ausers', 'Usuarios admin')
|
||||||
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
<%
|
<%
|
||||||
|
|
||||||
from parameciofast.modules.fastadmin.libraries.config import modules_admin, modules_admin_icons
|
from parameciofast.modules.fastadmin.libraries.config import modules_admin, modules_admin_icons
|
||||||
|
from parameciofast.libraries.i18n import I18n
|
||||||
|
|
||||||
|
i18n=I18n('fastadmin')
|
||||||
|
|
||||||
%>
|
%>
|
||||||
<!doctype html>
|
<!doctype html>
|
||||||
|
|
@ -58,9 +61,9 @@ from parameciofast.modules.fastadmin.libraries.config import modules_admin, modu
|
||||||
% for module in modules_admin:
|
% for module in modules_admin:
|
||||||
<ul class="nav nav-pills flex-column mb-auto">
|
<ul class="nav nav-pills flex-column mb-auto">
|
||||||
<li>
|
<li>
|
||||||
<a href="${url_for(module[1])}" class="nav-link text-white">
|
<a href="${url_for(module[0])}" class="nav-link text-white menu_item ${'active' if module[0]==module_selected else ''}" id="menu_${module[0]}">
|
||||||
<svg class="bi me-2" width="16" height="16"><use xlink:href="#${module[2]}"></use></svg>
|
<svg class="bi me-2" width="16" height="16"><use xlink:href="#${module[1]}"></use></svg>
|
||||||
${module[0]}
|
${i18n.clang('fastadmin', module[0], module[0])}
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
@ -68,7 +71,7 @@ from parameciofast.modules.fastadmin.libraries.config import modules_admin, modu
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
<div class="col-sm m-2" style="">
|
<div class="col-sm m-2 pt-3" style="">
|
||||||
<%block name="content">
|
<%block name="content">
|
||||||
<div class="card">
|
<div class="card">
|
||||||
<div class="card-header bg-primary bg-gradient">
|
<div class="card-header bg-primary bg-gradient">
|
||||||
|
|
@ -99,6 +102,20 @@ from parameciofast.modules.fastadmin.libraries.config import modules_admin, modu
|
||||||
|
|
||||||
}, 1000);
|
}, 1000);
|
||||||
|
|
||||||
|
$('.menu_item').hover( function (e) {
|
||||||
|
|
||||||
|
if($(this).hasClass('active')) {
|
||||||
|
|
||||||
|
$(this).removeClass('active');
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
|
$(this).addClass('active');
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
<%block name="js">
|
<%block name="js">
|
||||||
</%block>
|
</%block>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue