Added simple plugin for bottle for check login
This commit is contained in:
parent
598b49ad15
commit
99ef772206
5 changed files with 111 additions and 6 deletions
101
paramecio/citoplasma/plugins.py
Normal file
101
paramecio/citoplasma/plugins.py
Normal file
|
|
@ -0,0 +1,101 @@
|
||||||
|
from paramecio.modules.admin.models.admin import UserAdmin
|
||||||
|
from bottle import request
|
||||||
|
from paramecio.citoplasma.sessions import get_session
|
||||||
|
from paramecio.citoplasma.urls import redirect, make_url
|
||||||
|
import inspect
|
||||||
|
|
||||||
|
class LoginPlugin(object):
|
||||||
|
|
||||||
|
name = 'login'
|
||||||
|
api = 2
|
||||||
|
|
||||||
|
def __init__(self, keyword='login'):
|
||||||
|
|
||||||
|
self.keyword=keyword
|
||||||
|
|
||||||
|
|
||||||
|
def setup(self, app):
|
||||||
|
''' Make sure that other installed plugins don't affect the same keyword argument.'''
|
||||||
|
for other in app.plugins:
|
||||||
|
if not isinstance(other, LoginPlugin): continue
|
||||||
|
if other.keyword == self.keyword:
|
||||||
|
raise PluginError("Found another login plugin with "\
|
||||||
|
"conflicting settings (non-unique keyword).")
|
||||||
|
|
||||||
|
def apply(self, callback, context):
|
||||||
|
|
||||||
|
# Test if the original callback accepts a 'db' keyword.
|
||||||
|
# Ignore it if it does not need a login handle.
|
||||||
|
|
||||||
|
conf = context.config.get('login') or {}
|
||||||
|
|
||||||
|
keyword = conf.get('keyword', self.keyword)
|
||||||
|
|
||||||
|
args = inspect.getfullargspec(context.callback)[0]
|
||||||
|
|
||||||
|
if keyword not in args:
|
||||||
|
return callback
|
||||||
|
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
|
||||||
|
s=get_session()
|
||||||
|
|
||||||
|
if 'login' in s:
|
||||||
|
"""
|
||||||
|
if not s.get('usertpv_confirmed_email', 0):
|
||||||
|
redirect('check_email')
|
||||||
|
else:
|
||||||
|
"""
|
||||||
|
#s.save()
|
||||||
|
|
||||||
|
#conn.close()
|
||||||
|
|
||||||
|
rv = callback(*args, **kwargs)
|
||||||
|
|
||||||
|
return rv
|
||||||
|
|
||||||
|
else:
|
||||||
|
#Check if remember_login cookie
|
||||||
|
#, secret=config.key_encrypt
|
||||||
|
"""
|
||||||
|
remember_cookie=request.get_cookie("remember_me")
|
||||||
|
|
||||||
|
if remember_cookie:
|
||||||
|
|
||||||
|
if remember_cookie!='':
|
||||||
|
|
||||||
|
conn=WebModel.connection()
|
||||||
|
|
||||||
|
user=UserAdmin(conn)
|
||||||
|
|
||||||
|
arr_user=user.set_conditions('WHERE token_login=%s', [remember_cookie]).select_a_row_where()
|
||||||
|
|
||||||
|
error=True
|
||||||
|
|
||||||
|
if arr_user:
|
||||||
|
|
||||||
|
tz=arr_user['timezone']
|
||||||
|
|
||||||
|
try:
|
||||||
|
timezone(tz)
|
||||||
|
|
||||||
|
except:
|
||||||
|
tz='Europe/Madrid'
|
||||||
|
|
||||||
|
|
||||||
|
s['usertpv_timezone']=tz
|
||||||
|
|
||||||
|
error=False
|
||||||
|
|
||||||
|
s.save()
|
||||||
|
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
if not error:
|
||||||
|
redirect(config.base_url[:len(config.base_url)-1]+request.path)
|
||||||
|
|
||||||
|
"""
|
||||||
|
redirect(make_url('login'))
|
||||||
|
|
||||||
|
# Replace the route callback with the wrapped one.
|
||||||
|
return wrapper
|
||||||
|
|
@ -34,6 +34,7 @@ class ParentField(IntegerField):
|
||||||
if model_id==value:
|
if model_id==value:
|
||||||
self.error=True
|
self.error=True
|
||||||
self.txt_error='A field cannot be its own father'
|
self.txt_error='A field cannot be its own father'
|
||||||
|
self.required=True
|
||||||
value=0
|
value=0
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -291,7 +291,7 @@ def select_a_row(model, id, fields_selected=[], raw_query=0):
|
||||||
|
|
||||||
def select_count(model, conditions=['', []], field_to_count='id', raw_query=True):
|
def select_count(model, conditions=['', []], field_to_count='id', raw_query=True):
|
||||||
|
|
||||||
|
print(model.dummy)
|
||||||
#First table selecction
|
#First table selecction
|
||||||
|
|
||||||
tables_to_select=['`'+model.name+'`']
|
tables_to_select=['`'+model.name+'`']
|
||||||
|
|
|
||||||
|
|
@ -180,7 +180,7 @@ class PrimaryKeyField(PhangoField):
|
||||||
super(PrimaryKeyField, self).__init__(name, size, required)
|
super(PrimaryKeyField, self).__init__(name, size, required)
|
||||||
self.protected=True
|
self.protected=True
|
||||||
self.name_form=HiddenForm
|
self.name_form=HiddenForm
|
||||||
self.required=True
|
self.required=False
|
||||||
self.error_default="The value is zero"
|
self.error_default="The value is zero"
|
||||||
|
|
||||||
def check(self, value):
|
def check(self, value):
|
||||||
|
|
@ -403,7 +403,8 @@ class WebModel:
|
||||||
|
|
||||||
self.fields[field_model.name].model=self
|
self.fields[field_model.name].model=self
|
||||||
|
|
||||||
self.fields[field_model.name].required=required
|
if required:
|
||||||
|
self.fields[field_model.name].required=required
|
||||||
|
|
||||||
self.fields[field_model.name].post_register()
|
self.fields[field_model.name].post_register()
|
||||||
|
|
||||||
|
|
@ -462,7 +463,7 @@ class WebModel:
|
||||||
|
|
||||||
self.query_error=''
|
self.query_error=''
|
||||||
|
|
||||||
self.fields[self.name_field_id].required=False
|
#self.fields[self.name_field_id].required=False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
|
|
@ -507,7 +508,7 @@ class WebModel:
|
||||||
|
|
||||||
# Connect to db
|
# Connect to db
|
||||||
|
|
||||||
self.fields[self.name_field_id].required=False
|
#self.fields[self.name_field_id].required=False
|
||||||
|
|
||||||
if self.name_field_id in dict_values:
|
if self.name_field_id in dict_values:
|
||||||
del dict_values[self.name_field_id]
|
del dict_values[self.name_field_id]
|
||||||
|
|
|
||||||
|
|
@ -72,7 +72,9 @@ def prepare_app():
|
||||||
|
|
||||||
app.install(ErrorReportingPlugin())
|
app.install(ErrorReportingPlugin())
|
||||||
|
|
||||||
|
if hasattr(config, 'plugins_app'):
|
||||||
|
for p in config.plugins_app:
|
||||||
|
app.install(p())
|
||||||
|
|
||||||
# Clean last slash
|
# Clean last slash
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue