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 | |
65f02320 | 16 | from objet import ObjetInconnu |
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 | |
0897170c TN |
21 | # content-type correspondant a certains formats |
22 | contenttype = { 'xml': 'application/xml', | |
23 | 'rss': 'application/rss+xml', | |
24 | 'html': 'text/html', | |
25 | 'txt': 'text/plain', | |
3e2ae479 | 26 | 'json': 'text/plain', # 'application/json', |
0897170c TN |
27 | 'csv': 'text/csv', |
28 | 'debug': 'text/plain' } | |
1795efcd | 29 | |
0a4c31d4 | 30 | # les routes RESTful (cf http://routes.groovie.org/manual.html#restful-services) |
1795efcd | 31 | mapper = Mapper() |
0a4c31d4 TN |
32 | mapper.resource('demlog','demlog') |
33 | mapper.resource('comlog','comlog') | |
34 | mapper.resource('demdep','demdep') | |
35 | mapper.resource('comdep','comdep') | |
36 | mapper.resource('dempub','dempub') | |
37 | mapper.resource('compub','compub') | |
0a4c31d4 | 38 | mapper.resource('comare','comare') |
85a89d99 TN |
39 | mapper.resource('comarei','comare:(impl)',controller='comare') # pour les comarexxx où xxx est un code d'implantation |
40 | mapper.resource('comsre','comsre') | |
41 | mapper.resource('comsrei','comsre:(impl)',controller='comsre') | |
42 | mapper.resource('comxre','comxre') # comxre = comare + comsre | |
43 | mapper.resource('comxrei','comxre:(impl)',controller='comxre') | |
f6b772ed TN |
44 | mapper.resource('dem','dem') |
45 | mapper.resource('com','com') | |
ef44a84b | 46 | mapper.resource('utilisateur','utilisateur') |
d374db4d | 47 | mapper.resource('fournisseur','fournisseur') |
63f79d9d | 48 | mapper.resource('budget','budget') |
1795efcd | 49 | |
30e64411 TN |
50 | # objets disponibles |
51 | from document import demlog, comlog, demdep, comdep, dempub, compub, comare, comsre, comxre, dem, com | |
ef44a84b | 52 | from utilisateur import utilisateur |
d374db4d | 53 | from fournisseur import fournisseur |
63f79d9d | 54 | from budget import budget |
0ae81148 | 55 | |
a6627284 | 56 | def dispatcher(environ, start_response): |
81b8aba3 | 57 | """dispatch vers la bonne methode du bon objet, et retour WSGI""" |
30e64411 | 58 | results = environ['wsgiorg.routing_args'][1] # résultat du middleware Routes |
1795efcd | 59 | try: |
30e64411 | 60 | # On cherche l'objet puis la méthode |
1795efcd | 61 | target_class = globals()[results['controller']] |
0ae81148 TN |
62 | method = getattr(target_class,results['action']) |
63 | except: | |
6264cb94 | 64 | # Si erreur pendant la recherche, on renvoie un 501 |
4bd61de2 | 65 | start_response("501 Not Implemented", [('Content-type', 'text/html; charset=utf-8')]) |
6264cb94 | 66 | 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()) |
0ae81148 | 67 | try: |
30e64411 | 68 | # On lance la méthode et on renvoie le résultat |
1795efcd | 69 | type, output = method(target_class(environ)) |
0897170c | 70 | start_response("200 OK", [('Content-type', contenttype.get(type,'text/plain') + "; charset=utf-8")]) |
30e64411 TN |
71 | return output.encode('utf-8') |
72 | # gestion des problèmes possibles pendant l'exécution | |
65f02320 | 73 | except ObjetInconnu, type: |
4bd61de2 | 74 | start_response("404 Not Found", [('Content-type', 'text/html; charset=utf-8')]) |
6264cb94 | 75 | return '<html><body><h2>404 %s inexistant</h2></body></html>' % type |
30e64411 | 76 | except TemplateNotFound, template: |
4bd61de2 | 77 | start_response("415 Unsupported Media Type", [('Content-type', 'text/html; charset=utf-8')]) |
0ae81148 | 78 | return '<html><body><h2>415 format non supporté (%s inexistant)</h2></body></html>' % template |
1795efcd | 79 | except: |
4bd61de2 | 80 | start_response("500 INTERNAL ERROR", [('Content-type', 'text/html; charset=utf-8')]) |
0ae81148 | 81 | 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 |
82 | |
83 | ||
30e64411 TN |
84 | # application() sera lancée par mod_wsgi : on route et on dispatche |
85 | application = RoutesMiddleware( dispatcher, mapper ) | |
1795efcd | 86 | |
a6627284 | 87 |