changement de structure
darcs-hash:20060920201205-f46e9-1eb5e174b56f63387321970baaa769c5baaf9f98.gz
This commit is contained in:
parent
b5b3e3b72e
commit
3dc01c0c2d
6 changed files with 288 additions and 255 deletions
176
intranet/Root.py
176
intranet/Root.py
|
@ -1,148 +1,17 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
|
||||
import cherrypy, sys, os, datetime
|
||||
from Cheetah.Template import Template
|
||||
sys.path.append('/usr/scripts/gestion/')
|
||||
|
||||
# ######################################################## #
|
||||
# Configuration de Cheetah #
|
||||
# ######################################################## #
|
||||
|
||||
def serverSidePath(self, path):
|
||||
if (cherrypy.config.configMap["global"]["server.environment"] == "development"):
|
||||
if os.path.isfile(os.getcwd()+'/templates/'+path+".dev"):
return os.getcwd()+'/templates/'+path+".dev"
# les template se trouve dans le dossier template
|
||||
return os.getcwd()+'/templates/'+path
|
||||
|
||||
# on surcharge cette fonction dans la classe Template
|
||||
Template.serverSidePath = serverSidePath
|
||||
|
||||
|
||||
|
||||
# ######################################################## #
|
||||
# FILTRES MAISON #
|
||||
# ######################################################## #
|
||||
|
||||
from cherrypy.filters.basefilter import BaseFilter
|
||||
import cherrypy._cputil
|
||||
|
||||
##########################
|
||||
# DomFilter
|
||||
##########################
|
||||
#
|
||||
# transforme des objets python
|
||||
# en chainses de caracteres qui peuvent
|
||||
# etre parsees avec JSON/javascript
|
||||
#
|
||||
class DOMFilter(BaseFilter):
|
||||
def beforeFinalize(self):
|
||||
body = cherrypy.response.body
|
||||
if isinstance(body, dict):
|
||||
body = self.printAsDom(body)
|
||||
cherrypy.response.body = body
|
||||
|
||||
def printAsDom(self, chose):
|
||||
if isinstance(chose, dict):
|
||||
stringList = []
|
||||
for a_key in chose.keys():
|
||||
stringList.append('%s:%s' % (self.printAsDom(a_key), self.printAsDom(chose[a_key])))
|
||||
return "{%s}" % ','.join(stringList)
|
||||
|
||||
if isinstance(chose, list):
|
||||
stringList = []
|
||||
for an_item in chose:
|
||||
stringList.append('%s' % (self.printAsDom(an_item)))
|
||||
return "[%s]" % ','.join(stringList)
|
||||
|
||||
if isinstance(chose, str):
|
||||
return '"%s"' % chose
|
||||
|
||||
if isinstance(chose, unicode):
|
||||
return '"%s"' % chose.encode('utf8')
|
||||
|
||||
return str(chose)
|
||||
|
||||
|
||||
|
||||
|
||||
##########################
|
||||
# templatesEngine
|
||||
##########################
|
||||
#
|
||||
# Application des templates,
|
||||
# avec plein de test chians
|
||||
#
|
||||
class templatesEngine(BaseFilter):
|
||||
|
||||
def _getBodyTemplate(self, body):
|
||||
if isinstance(body, dict):
|
||||
if body.has_key('template'):
|
||||
if body['template'].endswith(".py"):
|
||||
return body['template']
|
||||
elif body['template'].endswith(".tmpl"):
|
||||
return body['template']
|
||||
else:
|
||||
return body['template'] + ".tmpl"
|
||||
return False
|
||||
|
||||
def _isStandaloneBody(self, body):
|
||||
if isinstance(body, dict):
|
||||
if body.has_key('standalone'):
|
||||
return body['standalone']
|
||||
if body.has_key('template'):
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
return True
|
||||
|
||||
def _getBodyNameSpace(self, body):
|
||||
if isinstance(body, dict):
|
||||
if body.has_key('values'):
|
||||
return body['values']
|
||||
return {}
|
||||
|
||||
def _useMainTemplate(self, body):
|
||||
t = Template(file='main.tmpl', searchList=[body,{'login':cherrypy.session['uid'], 'environment':cherrypy.config.configMap["global"]["server.environment"]}])
|
||||
return str(t)
|
||||
|
||||
|
||||
|
||||
def beforeFinalize(self):
|
||||
|
||||
body = cherrypy.response.body
|
||||
if isinstance(body, dict):
|
||||
bodyTemplate = self._getBodyTemplate(body)
|
||||
if bodyTemplate:
|
||||
templatevalues = self._getBodyNameSpace(body)
|
||||
t = Template(file=bodyTemplate, searchList=[templatevalues])
|
||||
body['page'] = str(t)
|
||||
|
||||
if not self._isStandaloneBody(body):
|
||||
body = self._useMainTemplate(body)
|
||||
else:
|
||||
body = body["page"]
|
||||
|
||||
cherrypy.response.body = body
|
||||
|
||||
|
||||
##########################
|
||||
# verification des droits
|
||||
##########################
|
||||
#
|
||||
# Application des templates,
|
||||
# avec plein de test chians
|
||||
#
|
||||
class verifDroits(BaseFilter):
|
||||
|
||||
def before_main(self):
|
||||
if not cherrypy.config.get('sessionAuthenticateFilter.on', False):
|
||||
return
|
||||
if not cherrypy.session.get("session_key"):
|
||||
return
|
||||
droits = cherrypy.config.get('crans.droits', 'all')
|
||||
if (droits != "all"):
|
||||
if not droits in cherrypy.session['droits']:
|
||||
raise cherrypy.HTTPError(403, "Vous n'avez pas les droits nécessaires.")
|
||||
from plugins.domfilter import DOMFilter
|
||||
from plugins.templatesfilter import TemplatesFilter
|
||||
from plugins.verifdroitsfilter import VerifDroitsFilter
|
||||
|
||||
# ######################################################## #
|
||||
# SERVER #
|
||||
|
@ -162,7 +31,7 @@ class Intranet:
|
|||
self.impression = impression.root()
|
||||
|
||||
|
||||
_cpFilterList = [templatesEngine(), DOMFilter(), verifDroits()]
|
||||
_cpFilterList = [TemplatesFilter(), DOMFilter(), VerifDroitsFilter()]
|
||||
|
||||
def index(self):
|
||||
return {
|
||||
|
@ -184,7 +53,8 @@ class Intranet:
|
|||
nounous.exposed= True
|
||||
|
||||
def test(self):
|
||||
return {"quoi":cherrypy.request.path, "il_faut":cherrypy.config.configMap["/nounous"]["crans.droits"], "moi":cherrypy.session['droits'] }
|
||||
return {"quoi":cherrypy.request.path, "il_faut":cherrypy.config.configMap["/nounous"]["crans.droits"], "moi":cherrypy.session['droits']
|
||||
}
|
||||
test.exposed = True
|
||||
|
||||
def environment(self):
|
||||
|
@ -192,7 +62,6 @@ class Intranet:
|
|||
environment.exposed = True
|
||||
'''
|
||||
|
||||
|
||||
# ######################################################## #
|
||||
# LOGIN MAISON #
|
||||
# ######################################################## #
|
||||
|
@ -230,21 +99,42 @@ def verifLogin(login = '', password = ''):
|
|||
message = u"L'authentification a echoué."
|
||||
return message
|
||||
|
||||
# ######################################################## #
|
||||
# COMMAND LINE OPTION #
|
||||
# ######################################################## #
|
||||
#
|
||||
#
|
||||
|
||||
from optparse import OptionParser
|
||||
|
||||
parser = OptionParser()
|
||||
parser.add_option("-d", "--dev",
|
||||
action="store_true", dest="dev", default=False,
|
||||
help="launch in dev mode")
|
||||
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
|
||||
# ######################################################## #
|
||||
# CHERRYPY #
|
||||
# ######################################################## #
|
||||
#
|
||||
# mise en place de cherrypy + conf
|
||||
#
|
||||
#cherrypy.config.update(file="/usr/scripts/intranet/dev.cfg")
|
||||
cherrypy.config.update(file="/usr/scripts/intranet/prod.cfg")
|
||||
|
||||
cherrypy.config.update(file="/usr/scripts/intranet/intranet.cfg")
|
||||
if (options.dev):
|
||||
cherrypy.config.update(file="/usr/scripts/intranet/dev.cfg")
|
||||
else:
|
||||
cherrypy.config.update(file="/usr/scripts/intranet/prod.cfg")
|
||||
|
||||
|
||||
settings={'/': {
|
||||
'sessionAuthenticateFilter.checkLoginAndPassword': verifLogin,
|
||||
'sessionAuthenticateFilter.loginScreen': login
|
||||
}
|
||||
}
|
||||
}}
|
||||
cherrypy.config.update(settings)
|
||||
|
||||
if (cherrypy.config.configMap["global"]["server.environment"] == "development"):
|
||||
from ldap_crans_test import crans_ldap
|
||||
print("settings : unsing test ldap : env=" + cherrypy.config.configMap["global"]["server.environment"])
|
||||
|
@ -255,3 +145,5 @@ cherrypy.config.update({'global':{'crans_ldap':crans_ldap()}})
|
|||
cherrypy.root = Intranet()
|
||||
|
||||
cherrypy.server.start()
|
||||
|
||||
|
||||
|
|
|
@ -1,2 +1,5 @@
|
|||
[global]
|
||||
rootDir="/usr/scripts/intranet"
|
||||
|
||||
[/nounous]
|
||||
crans.droits="nounou"
|
||||
|
|
0
intranet/plugins/__init__.py
Normal file
0
intranet/plugins/__init__.py
Normal file
38
intranet/plugins/domfilter.py
Normal file
38
intranet/plugins/domfilter.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
from cherrypy.filters.basefilter import BaseFilter
|
||||
import cherrypy._cputil
|
||||
|
||||
##########################
|
||||
# DomFilter
|
||||
##########################
|
||||
#
|
||||
# transforme des objets python
|
||||
# en chainses de caracteres qui peuvent
|
||||
# etre parsees avec JSON/javascript
|
||||
#
|
||||
class DOMFilter(BaseFilter):
|
||||
def beforeFinalize(self):
|
||||
body = cherrypy.response.body
|
||||
if isinstance(body, dict):
|
||||
body = self.printAsDom(body)
|
||||
cherrypy.response.body = body
|
||||
|
||||
def printAsDom(self, chose):
|
||||
if isinstance(chose, dict):
|
||||
stringList = []
|
||||
for a_key in chose.keys():
|
||||
stringList.append('%s:%s' % (self.printAsDom(a_key), self.printAsDom(chose[a_key])))
|
||||
return "{%s}" % ','.join(stringList)
|
||||
|
||||
if isinstance(chose, list):
|
||||
stringList = []
|
||||
for an_item in chose:
|
||||
stringList.append('%s' % (self.printAsDom(an_item)))
|
||||
return "[%s]" % ','.join(stringList)
|
||||
|
||||
if isinstance(chose, str):
|
||||
return '"%s"' % chose
|
||||
|
||||
if isinstance(chose, unicode):
|
||||
return '"%s"' % chose.encode('utf8')
|
||||
|
||||
return str(chose)
|
78
intranet/plugins/templatesfilter.py
Normal file
78
intranet/plugins/templatesfilter.py
Normal file
|
@ -0,0 +1,78 @@
|
|||
from cherrypy.filters.basefilter import BaseFilter
|
||||
import cherrypy._cputil, os
|
||||
from Cheetah.Template import Template
|
||||
|
||||
# ######################################################## #
|
||||
# Configuration de Cheetah #
|
||||
# ######################################################## #
|
||||
|
||||
def serverSidePath(self, path):
|
||||
if (cherrypy.config.configMap["global"]["server.environment"] == "development"):
|
||||
if os.path.isfile(os.getcwd()+'/templates/'+path+".dev"):
|
||||
return cherrypy.config.configMap["global"]["rootDir"]+'/templates/'+path+".dev"
|
||||
# les template se trouve dans le dossier template
|
||||
return cherrypy.config.configMap["global"]["rootDir"]+'/templates/'+path
|
||||
|
||||
# on surcharge cette fonction dans la classe Template
|
||||
Template.serverSidePath = serverSidePath
|
||||
|
||||
|
||||
##########################
|
||||
# templatesEngine
|
||||
##########################
|
||||
#
|
||||
# Application des templates,
|
||||
# avec plein de test chians
|
||||
#
|
||||
class TemplatesFilter(BaseFilter):
|
||||
|
||||
def _getBodyTemplate(self, body):
|
||||
if isinstance(body, dict):
|
||||
if body.has_key('template'):
|
||||
if body['template'].endswith(".py"):
|
||||
return body['template']
|
||||
elif body['template'].endswith(".tmpl"):
|
||||
return body['template']
|
||||
else:
|
||||
return body['template'] + ".tmpl"
|
||||
return False
|
||||
|
||||
def _isStandaloneBody(self, body):
|
||||
if isinstance(body, dict):
|
||||
if body.has_key('standalone'):
|
||||
return body['standalone']
|
||||
if body.has_key('template'):
|
||||
return False
|
||||
else:
|
||||
return True
|
||||
return True
|
||||
|
||||
def _getBodyNameSpace(self, body):
|
||||
if isinstance(body, dict):
|
||||
if body.has_key('values'):
|
||||
return body['values']
|
||||
return {}
|
||||
|
||||
def _useMainTemplate(self, body):
|
||||
t = Template(file='main.tmpl', searchList=[body,{'login':cherrypy.session['uid'], 'environment':cherrypy.config.configMap["global"]["server.environment"]}])
|
||||
return str(t)
|
||||
|
||||
|
||||
|
||||
def beforeFinalize(self):
|
||||
|
||||
body = cherrypy.response.body
|
||||
if isinstance(body, dict):
|
||||
bodyTemplate = self._getBodyTemplate(body)
|
||||
if bodyTemplate:
|
||||
templatevalues = self._getBodyNameSpace(body)
|
||||
t = Template(file=bodyTemplate, searchList=[templatevalues])
|
||||
body['page'] = str(t)
|
||||
|
||||
if not self._isStandaloneBody(body):
|
||||
body = self._useMainTemplate(body)
|
||||
else:
|
||||
body = body["page"]
|
||||
|
||||
cherrypy.response.body = body
|
||||
|
22
intranet/plugins/verifdroitsfilter.py
Normal file
22
intranet/plugins/verifdroitsfilter.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
from cherrypy.filters.basefilter import BaseFilter
|
||||
import cherrypy._cputil
|
||||
|
||||
##########################
|
||||
# verification des droits
|
||||
##########################
|
||||
#
|
||||
# Application des templates,
|
||||
# avec plein de test chians
|
||||
#
|
||||
class VerifDroitsFilter(BaseFilter):
|
||||
|
||||
def before_main(self):
|
||||
if not cherrypy.config.get('sessionAuthenticateFilter.on', False):
|
||||
return
|
||||
if not cherrypy.session.get("session_key"):
|
||||
return
|
||||
droits = cherrypy.config.get('crans.droits', 'all')
|
||||
if (droits != "all"):
|
||||
if not droits in cherrypy.session['droits']:
|
||||
raise cherrypy.HTTPError(403, "Vous n'avez pas les droits nécessaires.")
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue