2 # -*- coding: utf-8 -*-
6 # configuration (codes d'accès à la base MS-SQL)
7 sys
.path
.append('/home/thomas/public_html/')
10 # pour savoir quel objet interroger : Routes
11 from routes
import Mapper
12 from routes
.middleware
import RoutesMiddleware
15 from jinja
.exceptions
import TemplateNotFound
16 from objetsql
import ObjetSQLInconnuError
18 # TODO : ajouter un middleware de cache (beaker, basé sur REQUEST_URI quand la methode est GET)
19 # from beaker.middleware import CacheMiddleware
21 # formats de sortie autorisés, et content-type correspondant
22 # formats = { 'xml': 'application/xml', 'html': 'text/html', 'txt': 'text/plain', 'json': 'application/json', 'rss': 'application/rss+xml' }
23 formats
= { 'xml': 'application/xml', 'html': 'text/html', 'txt': 'text/plain', 'json': 'text/plain', 'rss': 'application/rss+xml' } # pour debug : json en text/plain
26 # les routes RESTful (cf http://routes.groovie.org/manual.html#restful-services)
28 mapper
.resource('demlog','demlog')
29 mapper
.resource('comlog','comlog')
30 mapper
.resource('demdep','demdep')
31 mapper
.resource('comdep','comdep')
32 mapper
.resource('dempub','dempub')
33 mapper
.resource('compub','compub')
34 mapper
.resource('comare','comare')
35 mapper
.resource('comarei','comare:(impl)',controller
='comare') # pour les comarexxx où xxx est un code d'implantation
36 mapper
.resource('comsre','comsre')
37 mapper
.resource('comsrei','comsre:(impl)',controller
='comsre')
38 mapper
.resource('comxre','comxre') # comxre = comare + comsre
39 mapper
.resource('comxrei','comxre:(impl)',controller
='comxre')
40 mapper
.resource('dem','dem')
41 mapper
.resource('com','com')
42 mapper
.resource('utilisateur','utilisateur')
45 from document
import demlog
, comlog
, demdep
, comdep
, dempub
, compub
, comare
, comsre
, comxre
, dem
, com
46 from utilisateur
import utilisateur
48 def dispatcher(environ
, start_response
):
49 """dispatch vers la bonne methode du bon objet, et retour WSGI"""
50 results
= environ
['wsgiorg.routing_args'][1] # résultat du middleware Routes
52 # On cherche l'objet puis la méthode
53 target_class
= globals()[results
['controller']]
54 method
= getattr(target_class
,results
['action'])
56 # Si erreur pendant la recherche, on renvoie un 501
57 start_response("501 Not Implemented", [('Content-type', 'text/html')])
58 return '<html><body><h2>501 objet ou action invalide</h2></body></html>' # <pre>%s: %s\n%s</pre></body></html>' % ( sys.exc_info()[0] , sys.exc_info()[1] , traceback.format_exc())
60 # On lance la méthode et on renvoie le résultat
61 type, output
= method(target_class(environ
))
62 start_response("200 OK", [('Content-type', formats
[type])])
63 return output
.encode('utf-8')
64 # gestion des problèmes possibles pendant l'exécution
65 except ObjetSQLInconnuError
, type:
66 start_response("404 Not Found", [('Content-type', 'text/html')])
67 return '<html><body><h2>404 %s inexistant</h2></body></html>' % type
68 except TemplateNotFound
, template
:
69 start_response("415 Unsupported Media Type", [('Content-type', 'text/html')])
70 return '<html><body><h2>415 format non supporté (%s inexistant)</h2></body></html>' % template
72 start_response("500 INTERNAL ERROR", [('Content-type', 'text/html')])
73 return '<html><body><h2>500 erreur lors du traitement</h2><pre>%s: %s\n%s</pre></body></html>' % ( sys
.exc_info()[0] , sys
.exc_info()[1] , traceback
.format_exc())
76 # application() sera lancée par mod_wsgi : on route et on dispatche
77 application
= RoutesMiddleware( dispatcher
, mapper
)