diff --git a/parameciofast/libraries/i18n.py b/parameciofast/libraries/i18n.py index bed3815..1d2b0ac 100644 --- a/parameciofast/libraries/i18n.py +++ b/parameciofast/libraries/i18n.py @@ -106,11 +106,36 @@ class I18n: l={} - def __init__(self, module, default_lang=None): + def __init__(self, module, default_lang='en-US'): 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): """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. 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): """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] - return I18n.lang(self.module, symbol, text_default) + return self.clang(self.module, symbol, text_default) #@staticmethod #def set_lang(code_lang): @@ -177,6 +202,16 @@ class I18n: 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 def extract_value(value): """Static method for get values from json lang array diff --git a/parameciofast/modules/fastadmin/__init__.py b/parameciofast/modules/fastadmin/__init__.py index 6e657f8..a41a3aa 100644 --- a/parameciofast/modules/fastadmin/__init__.py +++ b/parameciofast/modules/fastadmin/__init__.py @@ -5,6 +5,7 @@ from settings import config #from parameciofast.fast import app from parameciofast.libraries.session import ParamecioSession from starlette.middleware.sessions import SessionMiddleware +from parameciofast.fast import app 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') +# 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) diff --git a/parameciofast/modules/fastadmin/app.py b/parameciofast/modules/fastadmin/app.py index a4ebe2b..31a2726 100644 --- a/parameciofast/modules/fastadmin/app.py +++ b/parameciofast/modules/fastadmin/app.py @@ -38,13 +38,9 @@ if hasattr(config, 'cookie_name'): @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): - if not request.session.get('login_admin', None): - - return RedirectResponse(app.url_path_for('login_admin')) - 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) diff --git a/parameciofast/modules/fastadmin/ausers.py b/parameciofast/modules/fastadmin/ausers.py index 14c9e6c..a75143c 100644 --- a/parameciofast/modules/fastadmin/ausers.py +++ b/parameciofast/modules/fastadmin/ausers.py @@ -2,6 +2,8 @@ from fastapi.responses import HTMLResponse from parameciofast.modules.fastadmin import admin_app from parameciofast.modules.fastadmin.libraries.config import modules_admin, modules_admin_icons from parameciofast.libraries.mtemplates import PTemplate, env_theme +from parameciofast.libraries.i18n import I18n +import parameciofast.modules.fastadmin.libraries.i18n import os 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: 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('') diff --git a/parameciofast/modules/fastadmin/libraries/i18n.py b/parameciofast/modules/fastadmin/libraries/i18n.py new file mode 100644 index 0000000..0f46af9 --- /dev/null +++ b/parameciofast/modules/fastadmin/libraries/i18n.py @@ -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') + diff --git a/parameciofast/modules/fastadmin/templates/layout.phtml b/parameciofast/modules/fastadmin/templates/layout.phtml index dcee1e1..0cab79d 100644 --- a/parameciofast/modules/fastadmin/templates/layout.phtml +++ b/parameciofast/modules/fastadmin/templates/layout.phtml @@ -1,6 +1,9 @@ <% from parameciofast.modules.fastadmin.libraries.config import modules_admin, modules_admin_icons +from parameciofast.libraries.i18n import I18n + +i18n=I18n('fastadmin') %> @@ -58,9 +61,9 @@ from parameciofast.modules.fastadmin.libraries.config import modules_admin, modu % for module in modules_admin: @@ -68,7 +71,7 @@ from parameciofast.modules.fastadmin.libraries.config import modules_admin, modu
-
+
<%block name="content">
@@ -99,6 +102,20 @@ from parameciofast.modules.fastadmin.libraries.config import modules_admin, modu }, 1000); + $('.menu_item').hover( function (e) { + + if($(this).hasClass('active')) { + + $(this).removeClass('active'); + + } else { + + $(this).addClass('active'); + + } + + }); + <%block name="js">