Added modules support for admin
This commit is contained in:
parent
d9b62719d7
commit
b845f78e3f
9 changed files with 142 additions and 21 deletions
|
|
@ -40,9 +40,6 @@ def start_app():
|
|||
|
||||
#Add media files support. Only for development.
|
||||
|
||||
#print(inspect.getmembers(sys.modules['paramecio2.modules.admin.app']))
|
||||
#print(os.path.dirname(sys.modules['paramecio2.modules.admin.app'].__file__))
|
||||
|
||||
@app.route('/mediafrom/<module>/<path:media_file>')
|
||||
def send_media_file(module, media_file):
|
||||
|
||||
|
|
|
|||
4
paramecio2/libraries/config_admin.py
Normal file
4
paramecio2/libraries/config_admin.py
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
|
||||
# Variable base for admin modules
|
||||
|
||||
config_admin={}
|
||||
5
paramecio2/modules/admin/admin/ausers.py
Normal file
5
paramecio2/modules/admin/admin/ausers.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
from settings import config
|
||||
|
||||
def admin():
|
||||
|
||||
return "admin users"
|
||||
|
|
@ -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():
|
||||
|
|
@ -93,7 +164,24 @@ def login():
|
|||
|
||||
session['login_admin']=True
|
||||
|
||||
return {'error': 0}
|
||||
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
|
||||
|
||||
|
||||
else:
|
||||
|
||||
|
|
@ -133,8 +221,6 @@ def signup():
|
|||
|
||||
if user_admin.insert(forms, False):
|
||||
|
||||
|
||||
|
||||
error= {'error': 0}
|
||||
|
||||
return error
|
||||
|
|
|
|||
0
paramecio2/modules/admin/settings/__init__.py
Normal file
0
paramecio2/modules/admin/settings/__init__.py
Normal file
6
paramecio2/modules/admin/settings/config_admin.py
Normal file
6
paramecio2/modules/admin/settings/config_admin.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
from paramecio2.libraries.config_admin import config_admin
|
||||
from paramecio2.libraries.i18n import I18n
|
||||
|
||||
#modules_admin=[[I18n.lang('admin', 'users_admin', 'User\'s Admin'), 'paramecio.modules.admin.admin.ausers', 'ausers']]
|
||||
|
||||
config_admin['ausers']=[I18n.lang('admin', 'users', 'Users'), 'paramecio2.modules.admin.admin.ausers', 'ausers']
|
||||
4
paramecio2/modules/admin/templates/content.phtml
Normal file
4
paramecio2/modules/admin/templates/content.phtml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
<%inherit file="dashboard.phtml"/>
|
||||
<%block name="content">
|
||||
${contents|n}
|
||||
</%block>
|
||||
|
|
@ -1,15 +1,8 @@
|
|||
<%def name="check_menu(module)">\
|
||||
% if module[:1]=='/':
|
||||
${make_url(module[1:])}\
|
||||
% else:
|
||||
${make_url('admin/'+module)}\
|
||||
% endif
|
||||
</%def>
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||||
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0;">
|
||||
<title>${title}</title>
|
||||
<link href="${make_media_url('css/admin.css', 'admin')}" rel="stylesheet" />
|
||||
<link href="${make_media_url('css/font-awesome.min.css', 'admin')}" rel="stylesheet" />
|
||||
|
|
@ -42,7 +35,26 @@ ${make_url('admin/'+module)}\
|
|||
<nav id="menu" class="nav-collapse">
|
||||
<ul>
|
||||
<li class="menu_title"><%block name="applications"><i class="fa fa-gear" aria-hidden="true"></i>${lang('admin', 'applications', 'Applications')}</li></%block>
|
||||
<%block name="menu_list">
|
||||
<%
|
||||
|
||||
from paramecio2.libraries.config_admin import config_admin
|
||||
|
||||
%>
|
||||
% for admin in config_admin.values():
|
||||
|
||||
% if len(admin)==3:
|
||||
|
||||
<li><a href="${url_for('.admin', module=admin[2])}"><i class="fa fa-circle-o" aria-hidden="true"></i>${admin[0]}</a></li>
|
||||
|
||||
% else:
|
||||
|
||||
<li><a href="${url_for('.admin', module=admin[2], submodule=admin[3])}"><i class="fa fa-circle-o" aria-hidden="true"></i>${admin[0]}</a></li>
|
||||
|
||||
% endif
|
||||
|
||||
% endfor
|
||||
</%block>
|
||||
</ul>
|
||||
</nav>
|
||||
<div class="contents">
|
||||
|
|
|
|||
|
|
@ -1,2 +1,9 @@
|
|||
<%inherit file="dashboard.phtml"/>
|
||||
<%block name="content">Welcome to Paramecio Admin</%block>
|
||||
<%block name="content">
|
||||
<div class="title">
|
||||
Welcome to Paramecio Admin
|
||||
</div>
|
||||
<div class="cont">
|
||||
From here you can admin your site
|
||||
</div>
|
||||
</%block>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue