Commit | Line | Data |
---|---|---|
1795efcd TN |
1 | #!/usr/bin/python |
2 | # -*- coding: utf-8 -*- | |
3 | ||
4 | import sys, traceback | |
5 | ||
a6627284 TN |
6 | # configuration (codes d'accès à la base MS-SQL) |
7 | sys.path.append('/home/thomas/public_html/') | |
8 | import rest_config | |
9 | ||
0a4c31d4 | 10 | # pour savoir quel objet interroger : Routes |
1795efcd | 11 | from routes import Mapper |
a6627284 | 12 | from routes.middleware import RoutesMiddleware |
1795efcd | 13 | |
30e64411 TN |
14 | # Gestion des erreurs |
15 | from jinja.exceptions import TemplateNotFound | |
ef44a84b | 16 | from objetsql import ObjetSQLInconnuError |
a6627284 | 17 | |
30e64411 | 18 | # TODO : ajouter un middleware de cache (beaker, basé sur REQUEST_URI quand la methode est GET) |
0a4c31d4 | 19 | # from beaker.middleware import CacheMiddleware |
1795efcd | 20 | |
a6627284 | 21 | # formats de sortie autorisés, et content-type correspondant |
d588d902 | 22 | # formats = { 'xml': 'application/xml', 'html': 'text/html', 'txt': 'text/plain', 'json': 'application/json', 'rss': 'application/rss+xml' } |
30e64411 TN |
23 | formats = { 'xml': 'application/xml', 'html': 'text/html', 'txt': 'text/plain', 'json': 'text/plain', 'rss': 'application/rss+xml' } # pour debug : json en text/plain |
24 | ||
1795efcd | 25 | |
0a4c31d4 | 26 | # les routes RESTful (cf http://routes.groovie.org/manual.html#restful-services) |
1795efcd | 27 | mapper = Mapper() |
0a4c31d4 TN |
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') | |
0a4c31d4 | 34 | mapper.resource('comare','comare') |
85a89d99 TN |
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') | |
f6b772ed TN |
40 | mapper.resource('dem','dem') |
41 | mapper.resource('com','com') | |
ef44a84b | 42 | mapper.resource('utilisateur','utilisateur') |
1795efcd | 43 | |
30e64411 TN |
44 | # objets disponibles |
45 | from document import demlog, comlog, demdep, comdep, dempub, compub, comare, comsre, comxre, dem, com | |
ef44a84b | 46 | from utilisateur import utilisateur |
0ae81148 | 47 | |
a6627284 | 48 | def dispatcher(environ, start_response): |
81b8aba3 | 49 | """dispatch vers la bonne methode du bon objet, et retour WSGI""" |
30e64411 | 50 | results = environ['wsgiorg.routing_args'][1] # résultat du middleware Routes |
1795efcd | 51 | try: |
30e64411 | 52 | # On cherche l'objet puis la méthode |
1795efcd | 53 | target_class = globals()[results['controller']] |
0ae81148 TN |
54 | method = getattr(target_class,results['action']) |
55 | except: | |
30e64411 | 56 | # Si erreur pendant la recherche, on renvoie un 404 |
0ae81148 TN |
57 | start_response("404 Not Found", [('Content-type', 'text/html')]) |
58 | return '<html><body><h2>404 objet ou action invalide</h2><pre>%s: %s\n%s</pre></body></html>' % ( sys.exc_info()[0] , sys.exc_info()[1] , traceback.format_exc()) | |
59 | try: | |
30e64411 | 60 | # On lance la méthode et on renvoie le résultat |
1795efcd TN |
61 | type, output = method(target_class(environ)) |
62 | start_response("200 OK", [('Content-type', formats[type])]) | |
30e64411 TN |
63 | return output.encode('utf-8') |
64 | # gestion des problèmes possibles pendant l'exécution | |
65 | except TemplateNotFound, template: | |
0ae81148 TN |
66 | start_response("415 Unsupported Media Type", [('Content-type', 'text/html')]) |
67 | return '<html><body><h2>415 format non supporté (%s inexistant)</h2></body></html>' % template | |
1795efcd | 68 | except: |
0ae81148 TN |
69 | start_response("500 INTERNAL ERROR", [('Content-type', 'text/html')]) |
70 | 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()) | |
1795efcd TN |
71 | |
72 | ||
30e64411 TN |
73 | # application() sera lancée par mod_wsgi : on route et on dispatche |
74 | application = RoutesMiddleware( dispatcher, mapper ) | |
1795efcd | 75 | |
a6627284 | 76 |