[doc] fix most of ReST compilation errors and warnings
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr## This file is part of CubicWeb.## CubicWeb is free software: you can redistribute it and/or modify it under the# terms of the GNU Lesser General Public License as published by the Free# Software Foundation, either version 2.1 of the License, or (at your option)# any later version.## CubicWeb is distributed in the hope that it will be useful, but WITHOUT# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more# details.## You should have received a copy of the GNU Lesser General Public License along# with CubicWeb. If not, see <http://www.gnu.org/licenses/>."""web session component: by dfault the session is actually the db connectionobject :/"""__docformat__="restructuredtext en"fromcubicwebimport(RepositoryError,Unauthorized,AuthenticationError,BadConnectionId)fromcubicweb.webimportInvalidSession,Redirectfromcubicweb.web.applicationimportAbstractSessionManagerfromcubicweb.dbapiimportProgrammingError,DBAPISessionclassInMemoryRepositorySessionManager(AbstractSessionManager):"""manage session data associated to a session identifier"""def__init__(self,*args,**kwargs):AbstractSessionManager.__init__(self,*args,**kwargs)# XXX require a RepositoryAuthenticationManager which violates# authenticate interface by returning a session instead of a user#assert isinstance(self.authmanager, RepositoryAuthenticationManager)self._sessions={}# dump_data / restore_data to avoid loosing open sessions on registry# reloadingdefdump_data(self):returnself._sessionsdefrestore_data(self,data):self._sessions=datadefcurrent_sessions(self):returnself._sessions.values()defget_session(self,req,sessionid):"""return existing session for the given session identifier"""ifsessionidnotinself._sessions:raiseInvalidSession()session=self._sessions[sessionid]ifsession.cnx:try:user=self.authmanager.validate_session(req,session)exceptInvalidSession:# invalid sessionself.close_session(session)raise# associate the connection to the current requestreq.set_session(session,user)returnsessiondefopen_session(self,req,allow_no_cnx=True):"""open and return a new session for the given request. The session is also bound to the request. raise :exc:`cubicweb.AuthenticationError` if authentication failed (no authentication info found or wrong user/password) """try:cnx,login=self.authmanager.authenticate(req)exceptAuthenticationError:ifallow_no_cnx:session=DBAPISession(None)else:raiseelse:session=DBAPISession(cnx,login)self._sessions[session.sessionid]=session# associate the connection to the current requestreq.set_session(session)returnsessiondefpostlogin(self,req):"""postlogin: the user has been authenticated, redirect to the original page (index by default) with a welcome message """# Update last connection date# XXX: this should be in a post login hook in the repository, but there# we can't differentiate actual login of automatic session# reopening. Is it actually a problem?if'last_login_time'inreq.vreg.schema:self._update_last_login_time(req)args=req.formforforminternal_keyin('__form_id','__domid','__errorurl'):args.pop(forminternal_key,None)path=req.relative_path(False)ifpathin('login','logout')orreq.form.get('vid')=='loggedout':path='view'args['__message']=req._('welcome %s !')%req.user.loginif'vid'inreq.formandreq.form['vid']!='loggedout':args['vid']=req.form['vid']if'rql'inreq.form:args['rql']=req.form['rql']raiseRedirect(req.build_url(path,**args))req.set_message(req._('welcome %s !')%req.user.login)def_update_last_login_time(self,req):# XXX should properly detect missing permission / non writeable source# and avoid "except (RepositoryError, Unauthorized)" belowifreq.user.cw_metainformation()['source']['type']=='ldapuser':returntry:req.execute('SET X last_login_time NOW WHERE X eid %(x)s',{'x':req.user.eid})req.cnx.commit()except(RepositoryError,Unauthorized):req.cnx.rollback()exceptException:req.cnx.rollback()raisedefclose_session(self,session):"""close session on logout or on invalid session detected (expired out, corrupted...) """self.info('closing http session %s'%session.sessionid)delself._sessions[session.sessionid]ifsession.cnx:try:session.cnx.close()except(ProgrammingError,BadConnectionId):# expired on the repository sidepasssession.cnx=None