Added modules support for admin
This commit is contained in:
parent
d9b62719d7
commit
b845f78e3f
9 changed files with 142 additions and 21 deletions
|
|
@ -1,4 +1,4 @@
|
|||
from flask import Blueprint, redirect, session, url_for, request, g, make_response
|
||||
from flask import Blueprint, redirect, session, url_for, request, g, make_response, abort
|
||||
from settings import config
|
||||
from paramecio2.libraries.mtemplates import PTemplate, env_theme
|
||||
from paramecio2.libraries.i18n import I18n
|
||||
|
|
@ -6,6 +6,10 @@ from paramecio2.libraries.formsutils import show_form, generate_csrf, set_extra_
|
|||
from paramecio2.libraries.db.webmodel import WebModel
|
||||
from paramecio2.modules.admin.models.admin import UserAdmin
|
||||
from paramecio2.libraries.keyutils import create_key_encrypt
|
||||
from time import time
|
||||
import os, sys
|
||||
from importlib import import_module
|
||||
from paramecio2.libraries.config_admin import config_admin
|
||||
|
||||
env=env_theme(__file__)
|
||||
|
||||
|
|
@ -29,8 +33,23 @@ def admin_prepare():
|
|||
g.connection=WebModel.connection()
|
||||
|
||||
if request.endpoint!='admin_app.login' and request.endpoint!='admin_app.signup':
|
||||
|
||||
if 'login_admin' not in session:
|
||||
return redirect(url_for('admin_app.login'))
|
||||
|
||||
if 'remember_login_admin' in request.cookies:
|
||||
|
||||
with g.connection.query('select count(id) as count_id from useradmin where token_login=%s', [request.cookies['remember_login_admin']]) as cursor:
|
||||
|
||||
arr_count=cursor.fetchone()
|
||||
|
||||
if arr_count['count_id']==0:
|
||||
|
||||
return redirect(url_for('admin_app.login'))
|
||||
|
||||
|
||||
else:
|
||||
|
||||
return redirect(url_for('admin_app.login'))
|
||||
|
||||
@admin_app.after_request
|
||||
def admin_finished(response):
|
||||
|
|
@ -41,23 +60,75 @@ def admin_finished(response):
|
|||
|
||||
return response
|
||||
|
||||
# Load
|
||||
# Load modules from admin
|
||||
|
||||
for app in config.apps:
|
||||
module_app=config.apps[app][0]
|
||||
module_path=os.path.dirname(sys.modules[module_app].__file__)
|
||||
|
||||
if os.path.isfile(module_path+'/settings/config_admin.py'):
|
||||
#print(module_path+'/settings/config_admin.py')
|
||||
pos_last_point=module_app.rfind('.')
|
||||
|
||||
config_path=module_app[:pos_last_point]+'.settings.config_admin'
|
||||
|
||||
a=import_module(config_path)
|
||||
|
||||
arr_modules_admin={}
|
||||
|
||||
for app_load in config_admin.values():
|
||||
|
||||
#print(app)
|
||||
|
||||
if len(app_load)==3:
|
||||
|
||||
arr_modules_admin[app_load[2]+'/']=import_module(app_load[1])
|
||||
#print(app_load[1])
|
||||
else:
|
||||
|
||||
arr_modules_admin[app_load[2]+'/'+app_load[3]]=import_module(app_load[1])
|
||||
#print(app_load[1])
|
||||
|
||||
@admin_app.route('/admin')
|
||||
@admin_app.route('/admin/<module>')
|
||||
def admin(module=''):
|
||||
@admin_app.route('/admin/<module>/<submodule>')
|
||||
def admin(module='', submodule=''):
|
||||
|
||||
"""
|
||||
if 'login_admin' not in session:
|
||||
return redirect(url_for('admin_app.login'))
|
||||
"""
|
||||
|
||||
return t.load_template('home.phtml', title=I18n.lang('admin', 'paramecio_admin', 'Paramecio admin'))
|
||||
if module=='':
|
||||
|
||||
return t.load_template('home.phtml', title=I18n.lang('admin', 'paramecio_admin', 'Paramecio admin'))
|
||||
|
||||
else:
|
||||
|
||||
path_module=module+'/'+submodule
|
||||
|
||||
if path_module in arr_modules_admin:
|
||||
|
||||
content=arr_modules_admin[path_module].admin()
|
||||
|
||||
return t.load_template('content.phtml', title=I18n.lang('admin', 'paramecio_admin', 'Paramecio admin'), contents=content)
|
||||
|
||||
else:
|
||||
abort(404)
|
||||
|
||||
|
||||
@admin_app.route('/admin/logout')
|
||||
def logout():
|
||||
|
||||
return redirect(url_for('admin_app.login'))
|
||||
resp=make_response(redirect(url_for('admin_app.login')))
|
||||
|
||||
if 'login_admin' in session:
|
||||
del session['login_admin']
|
||||
|
||||
if 'remember_login_admin' in request.cookies:
|
||||
resp.set_cookie('remember_login_admin', value='', max_age=0, expires=0, path=config.application_root)
|
||||
|
||||
return resp
|
||||
|
||||
@admin_app.route('/admin/login', methods=['GET', 'POST'])
|
||||
def login():
|
||||
|
|
@ -92,8 +163,25 @@ def login():
|
|||
if user_admin.fields['password'].verify(password, arr_user['password']):
|
||||
|
||||
session['login_admin']=True
|
||||
|
||||
resp = make_response({'error': 0})
|
||||
|
||||
if 'remember_login' in request.form:
|
||||
|
||||
remember_key=create_key_encrypt()
|
||||
|
||||
user_admin.safe_query()
|
||||
|
||||
user_admin.check_user=False
|
||||
|
||||
user_admin.set_conditions('WHERE id=%s', [arr_user['id']]).update({'token_login': remember_key})
|
||||
|
||||
timestamp=int(time())+315360000
|
||||
|
||||
resp.set_cookie('remember_login_admin', value=remember_key, max_age=315360000, expires=timestamp, path=config.application_root)
|
||||
|
||||
return resp
|
||||
|
||||
return {'error': 0}
|
||||
|
||||
else:
|
||||
|
||||
|
|
@ -133,8 +221,6 @@ def signup():
|
|||
|
||||
if user_admin.insert(forms, False):
|
||||
|
||||
|
||||
|
||||
error= {'error': 0}
|
||||
|
||||
return error
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue