scripts/intranet/ClassesIntranet/Intranet.py
gdetrez b59ce5e610 Rsoltion de quelques problmes avec les chemins
darcs-hash:20070124131613-f46e9-4668b5a746f18078e9690c55080f63d20bb5591a.gz
2007-01-24 14:16:13 +01:00

227 lines
9.6 KiB
Python
Executable file
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
# #############################################################
# ..
# .... ............ ........
# . ....... . .... ..
# . ... .. .. .. .. ..... . ..
# .. .. ....@@@. .. . ........ .
# .. . .. ..@.@@..@@. .@@@@@@@ @@@@@@. ....
# .@@@@. .@@@@. .@@@@..@@.@@..@@@..@@@..@@@@.... ....
# @@@@... .@@@.. @@ @@ .@..@@..@@...@@@. .@@@@@. ..
# .@@@.. . @@@. @@.@@..@@.@@..@@@ @@ .@@@@@@.. .....
# ...@@@.... @@@ .@@.......... ........ ..... ..
# . ..@@@@.. . .@@@@. .. ....... . .............
# . .. .... .. .. . ... ....
# . . .... ............. .. ...
# .. .. ... ........ ... ...
# ................................
#
# #############################################################
# Intranet.py
#
# Classe Intranet, clase de base de l'intranet
#
# Copyright (c) 2006 by www.crans.org
# #############################################################
import crans.cp as _crans_cp
import cherrypy, os, sys
make_path = os.path.join
import crans.utils.exceptions
from ClassesIntranet.AuthorisationsManager import setDroits
class Intranet:
# ######################################################## #
# GESTION DES MODULES #
# ######################################################## #
#
#
_loaded_modules = {}
def _make_static_path_for_module(self, module_name ):
return make_path("/", module_name, "static")
def loadModule(self, un_module):
MODULES_DIR = cherrypy.config.get("crans.modules.dir")
if not un_module.startswith("."):
# faire ici l'importation
# importer le fichier main.py
try:
#module_path = MODULES_DIR + un_module + "/main"
module_path = make_path(un_module, "main")
mon_module = __import__(module_path)
module_root = mon_module.main()
# on ajoute la classe a l'arborescence de cherrypy :
setattr( self, un_module, module_root)
# on ajoute le module aux modules connus :
try:
cat = module_root.category()
if not self._loaded_modules.has_key(cat):
self._loaded_modules[cat] = {}
self._loaded_modules[cat][un_module] = module_root
except:
if cherrypy.config.get("server.environment") == "development":
_crans_cp.log("Impossible d'obtenir les parametres du module %s" % un_module)
_crans_cp.log(crans.utils.exceptions.formatExc())
# on ajoute les droits du module :
try:
droits_module = module_root.droits()
setDroits("/%s" % un_module, droits_module)
except:
if cherrypy.config.get("server.environment") == "development":
_crans_cp.log("Impossible d'obtenir les parametres du module %s" % un_module)
_crans_cp.log(crans.utils.exceptions.formatExc())
except:
_crans_cp.log("Impossible de charger le module %s" % un_module)
_crans_cp.log(crans.utils.exceptions.formatExc())
# ajouter le dossier static ou il faut
staticPath = make_path(MODULES_DIR, un_module, "static")
if os.path.isdir(staticPath):
settings= { self._make_static_path_for_module(un_module):
{ 'sessionAuthenticateFilter.on': False,
'sessionFilter.on': False,
'server.output_filters.templatesEngine.on' : False,
'staticFilter.on': True,
'staticFilter.dir': staticPath,
}
}
cherrypy.config.update(settings)
if cherrypy.config.get("server.environment") == "development":
_crans_cp.log("New static : %s" % staticPath)
# fin de l'ajout du dossier static
def __init__(self):
##
#### import automatique des modules
##
MODULES_DIR = cherrypy.config.get("crans.modules.dir", False)
if MODULES_DIR:
if os.path.isdir(MODULES_DIR):
sys.path.append(MODULES_DIR)
Liste_Modules = os.listdir(MODULES_DIR)
for un_module in Liste_Modules:
if un_module != "CVS":
self.loadModule(un_module)
else:
_crans_cp.log("Dossier des modules invalide", 'LOADING', 2)
else:
_crans_cp.log("Pas de dossier de modules", 'LOADING', 2)
# ######################################################## #
# QUELQUES PAGES #
# ######################################################## #
#
#
def index(self):
items = {}
for a_category in self._loaded_modules:
items[a_category] = {}
for a_module_name in self._loaded_modules[a_category]:
module_object = self._loaded_modules[a_category][a_module_name]
if module_object.accessible():
items[a_category][a_module_name] = {}
items[a_category][a_module_name]["name"] = module_object.title()
items[a_category][a_module_name]["icon"] = self._make_static_path_for_module(a_module_name) + "/" + module_object.icon()
items[a_category][a_module_name]["url"] = "/" + a_module_name + "/"
# si la categorie est vide, on la vire
if items[a_category] == {}:
del items[a_category]
return {
'template':'accueil',
'values':{"modules":items},
'stylesheets':['css/accueil.css'],
}
index.exposed= True
def info(self):
return {
'template':'info-diverses',
'values':{},
'stylesheets':['accueil.css'],
}
info.exposed = True
def send_error_repport(self, **kw):
# on récupère tout de suite le traceback
tb = crans.utils.exceptions.formatExc()
# entêtes du mail
exp = "intranet"
dest = cherrypy.config.get("mail.bugreport", "nounous@crans.org")
subject = "Rapport de Bug"
text = """
Bonsoir,
Ceci est un rapport de bug envoye par l'intranet.
%s
""" % "\n".join( [ "%s: %s" % (str(a), str(kw[a])) for a in kw] )
#On ajoute des variables de cherrypy
text += "\n= Cherrypy vars =\n"
try:
text += "url: %s\n" % cherrypy.request.browser_url
except:
pass
try:
text += "headers: \n %s\n" % "\n".join( [" %s: %s" % (str(a), str(cherrypy.request.headers[a])) for a in cherrypy.request.headers] )
except:
pass
try:
text += "query_string: %s\n" % cherrypy.request.query_string
except:
pass
try:
text += "path: %s\n" % cherrypy.request.path
except:
pass
# on ajoute le traceback
text += "\n= Traceback =\n"
text += tb
#on signe, quand même !
text += "\n-- \nRoot.py pour l'intranet\n"
quickSend(exp, dest, subject, text)
return self.index()
send_error_repport.exposed = True
def testErreur(self):
raise Exception, "sdlfkjqmsdklj"
testErreur.exposed = True
def test(self):
return cherrypy.request.remote_addr
test.exposed = True
def _cp_on_http_error(self, status, message):
if (cherrypy.config.configMap["global"]["server.environment"] == "development"):
cherrypy._cputil._cp_on_http_error(status, message)
return
if status==403:
cherrypy.response.body = {
'template':'error403',
'values':{'status':status, 'message':message },
'standalone':False,
}
elif status==404:
cherrypy.response.body = {
'template':'error',
'values':{'status':status, 'message':message },
'standalone':False,
}
elif status==500:
self.send_error_repport(status = status, message = message )
# les filtres ne sont pas appliqués, on le fait à la main...
from plugins.templatesfilter import TemplatesFilter
TemplatesFilter().goWithThisDict({'template':'error', 'values':{'status':status, 'message':message }})
else:
self.send_error_repport(status = status, message = message)
cherrypy._cputil._cp_on_http_error(status, message)