Add static management and improving multi account support

* mandaye/auth/authform.py: create a method to login external users
 * mandaye/config.py: add static root directory
 * mandaye/filters/vincennes.py: improve multi accounts
 * mandaye/server.py: add support for static folder
 * mandaye/templates/biblio/multicompte.html: improve multi accounts
 interface
This commit is contained in:
Jérôme Schneider 2011-10-05 15:51:24 +02:00
parent b1a3a93cb5
commit 71cdd79c0e
6 changed files with 62 additions and 46 deletions

7
TODO
View File

@ -1,8 +1,3 @@
* Improve README.rst
* Move the default filters into the mapping
* Add a debug mode
* Mapping should match the url
* Manage a folder with the configurations
* Finish authentification management
* Merge __init__ and init in the dispatchers
* Migrate to Webob

View File

@ -154,6 +154,28 @@ this keys: form_url, form_attrs, username_field and password_field")
return get_response(env, HTTPRequest(), values.get('failed_url'))
def _login_ext_user(self, ext_user, env, condition):
""" Log in an external user
"""
if not ext_user.login or not ext_user.password:
return _500(env['PATH_INFO'],
'Invalid values for AuthFormDispatcher.login')
# TODO: generized this condition
extra_values = {}
if ext_user.birthdate and self.form_values.has_key('birthdate_field'):
extra_values = { self.form_values['birthdate_field']: ext_user.birthdate }
response = self.replay(env, ext_user.login,
ext_user.password, extra_values)
if condition and eval(condition):
ext_user.last_connection = datetime.now()
mandaye.sql_session.commit()
env['beaker.session'][self.site_name] = ext_user.login
env['beaker.session'].save()
return response
else:
# TODO; find a better solution
return _302(values.get('associate_url'))
def login(self, env, values, condition, request, response):
""" Automatic login on a site with a form
"""
@ -173,22 +195,7 @@ this keys: form_url, form_attrs, username_field and password_field")
first()
if not ext_user:
return _302(values.get('associate_url'))
if not ext_user.login or not ext_user.password:
return _500(env['PATH_INFO'],
'Invalid values for AuthFormDispatcher.login')
# TODO: generized this condition
extra_values = {}
if ext_user.birthdate and self.form_values.has_key('birthdate_field'):
extra_values = { self.form_values['birthdate_field']: ext_user.birthdate }
response = self.replay(env, ext_user.login,
ext_user.password, extra_values)
if condition and eval(condition):
ext_user.last_connection = datetime.now()
mandaye.sql_session.commit()
return response
else:
# TODO; find a better solution
return _302(values.get('associate_url'))
return self._login_ext_user(ext_user, env, condition)
def change_user(self, env, values, request, response):
""" Multi accounts feature
@ -210,22 +217,8 @@ this keys: form_url, form_attrs, username_field and password_field")
filter(ExtUser.login==username).\
first()
if not ext_user:
return _500(env['PATH_INFO'], 'User %s not found' % username)
if not ext_user.login or not ext_user.password:
return _500(env['PATH_INFO'],
'Authform.change_user ext_user no login or password')
extra_values = {}
if ext_user.birthdate and self.form_values.has_key('birthdate_field'):
extra_values = { self.form_values['birthdate_field']: ext_user.birthdate }
response = self.replay(env, ext_user.login,
ext_user.password, extra_values)
if response.code == 302:
ext_user.last_connection = datetime.now()
mandaye.sql_session.commit()
return response
else:
# TODO; find a better solution
return _302(values.get('associate_url'))
return self._login_ext_user(ext_user, env, 'response.code==302')
def disassociate(self, env, values, request, response):
""" Multi accounts feature

View File

@ -16,6 +16,8 @@ certfile = ""
# Template directory
template_directory = "mandaye/templates"
# Static folder
static_root = 'mandaye/static'
# Database configuration
# Follow the rfc 1738 http://rfc.net/rfc1738.html

View File

@ -44,14 +44,16 @@ class Biblio:
def resp_multicompte_html(self, env, values, request, response):
""" Modify response html to support multi accounts
"""
site_name = values.get('site_name')
login = env['beaker.session'].get('login')
current_account = env['beaker.session'].get(site_name)
if response.msg and '<h2><div>Mon compte</div></h2>' in response.msg\
and login:
ext_users = mandaye.sql_session.query(ExtUser).\
join(LocalUser).\
join(Site).\
filter(LocalUser.login==login).\
filter(Site.name==values.get('site_name')).\
filter(Site.name==site_name).\
order_by(ExtUser.last_connection.desc()).\
all()
accounts = []
@ -59,9 +61,9 @@ class Biblio:
accounts.append(ext_user.login)
soup = BeautifulSoup(response.msg)
div = soup.find('div', {'id': 'opacaccount'})
ul = div.find('ul')
multi = serve_template(values.get('template'), accounts=accounts, **values)
ul.append(multi)
multi = serve_template(values.get('template'),
accounts=accounts, current_account=current_account, **values)
div.parent.append(multi)
response.msg = str(soup)
return response

View File

@ -1,7 +1,7 @@
# gevent patching
from gevent import monkey
monkey.patch_all(aggressive=False)
monkey.patch_all()
import Cookie
import config
@ -16,6 +16,7 @@ import poster.streaminghttp
from beaker.middleware import SessionMiddleware
from cgi import escape
from gevent.pywsgi import WSGIServer, WSGIHandler
from static import Cling
from mandaye.config import debug
from mandaye.dispatcher import Dispatcher
@ -82,7 +83,21 @@ class MandayeApp(object):
"""
self.env = env
self.env['mandaye.scheme'] = env['wsgi.url_scheme']
self.dispatcher = self._get_dispatcher(env['HTTP_HOST'], env['PATH_INFO'])
self.dispatcher = None
local_host = env['HTTP_HOST']
path_info = env['PATH_INFO']
if config.hosts.has_key(local_host):
for mapper in config.hosts[local_host]:
if re.match(mapper.get('path'), path_info):
if mapper.get('force_ssl'):
self.env['mandaye.scheme'] = 'https'
if mapper.has_key('target'):
self.dispatcher = Dispatcher(self.env, mapper.get('target'),
mapper.get('mapping'))
elif mapper.has_key('static'):
env['PATH_INFO'] = re.sub('^' + mapper.get('path'), '',
env['PATH_INFO'])
return Cling(mapper.get('static')).__call__(env, start_response)
if self.dispatcher:
return self.on_request(start_response)
else:

View File

@ -1,8 +1,17 @@
<div style='margin-top:10px;'>
<h2 style='margin-bottom:5px;'>Gestion des comptes</h2>
<ul>
% for account in accounts:
<li><a href="/mandaye/change_user?username=${account}">${account}</a>
<div class="logoutlink"><a href="/mandaye/disassociate?username=${account}">Désassocier</a></div></li>
% if account != current_account:
<li style='margin-bottom:8px;'>
<b>${account}</b><br />
<a href="/mandaye/change_user?username=${account}">Se connecter</a> |
<a href="/mandaye/disassociate?username=${account}">Désassocier</a>
</li>
%endif
% endfor
</ul>
<p style='margin-top:10px; font-weight:bold;'>
<a href="${associate_url}">Associer un nouveau compte</a>
</p>
</div>