Commit | Line | Data |
---|---|---|
1795efcd TN |
1 | #!/usr/bin/python |
2 | # -*- coding: utf-8 -*- | |
3 | ||
4 | import sys, traceback | |
5 | ||
6 | from routes import Mapper | |
7 | from cgi import parse_qs | |
8 | ||
9 | from pymssql import connect | |
10 | from jinja import Environment, FileSystemLoader | |
11 | from jinja.filters import stringfilter | |
12 | ||
13 | import re | |
14 | p = re.compile('(dem|com)-(...)-auf',re.IGNORECASE) | |
15 | ||
16 | def coda2rest(value): | |
17 | m = p.search(value) | |
18 | if m == None: return value | |
19 | return m.group(1).lower() + m.group(2).lower() | |
20 | ||
21 | @stringfilter | |
22 | def do_coda2rest(value): | |
23 | return coda2rest(value) | |
24 | ||
25 | formats = { 'xml': 'application/xml', 'html': 'text/html', 'txt': 'text/plain', 'json': 'application/json', 'rss': 'application/rss+xml' } | |
26 | ||
27 | mapper = Mapper() | |
28 | mapper.connect(':controller/') | |
29 | mapper.connect(':controller/:id', action='get') | |
30 | mapper.connect(':controller/:action/:id') | |
31 | mapper.create_regs(['document','demlog','comlog']) | |
32 | ||
33 | class objetsql(object): | |
34 | def __init__(self, environ): | |
35 | self.bd = connect(host='10.36.0.240',user='log_lec',password='123soap8',database='prodprocurement') | |
36 | self.cursor = self.bd.cursor() | |
37 | self.jinja = Environment(loader=FileSystemLoader('/home/thomas/public_html/')) | |
38 | self.jinja.filters['coda2rest'] = do_coda2rest | |
39 | self.environ = environ | |
40 | if (self.environ['org.auf.filters'].has_key('format')): | |
41 | self.outputformat=self.environ['org.auf.filters']['format'][0] | |
42 | else: | |
43 | self.outputformat='xml' | |
44 | ||
45 | class document(objetsql): | |
46 | def __init__(self, environ, code_document='%', basename_template='document'): | |
47 | super(document, self).__init__(environ) | |
48 | self.code_document = code_document | |
49 | self.basename_template = basename_template | |
50 | ||
51 | def index(self): | |
52 | self.cursor.execute("select top 30 * from auf_v_acces_demcom where code like '%s' order by date_modif desc" % (self.code_document)) | |
53 | documents={} | |
54 | documents['code'] = self.code_document | |
55 | documents_liste=[] | |
56 | while 1: | |
57 | document = dict_fetchone(self.cursor) | |
58 | if document == None: break | |
59 | document['code_rest'] = coda2rest(document['code']) | |
60 | documents_liste.append(document) | |
61 | documents['documents'] = documents_liste | |
62 | template = self.jinja.get_template('%s-index.%s' % (self.basename_template, self.outputformat)) | |
63 | output = template.render(documents) | |
64 | return self.outputformat, output | |
65 | ||
66 | def get(self): | |
67 | id = int(self.environ['org.auf.routes']['id']) | |
68 | self.cursor.execute("select top 1 * from auf_v_acces_demcom where code like '%s' and numero = %d" % (self.code_document, id)) | |
69 | document = dict_fetchone(self.cursor) | |
70 | if document == None: | |
71 | raise "document inexistant" | |
72 | document['code_rest'] = coda2rest(document['code']) | |
73 | details = [] | |
74 | self.cursor.execute("select * from auf_v_acces_dtls_demcom where code like '%s' and numero = %d" % (self.code_document, id)) | |
75 | while 1: | |
76 | detail = dict_fetchone(self.cursor) | |
77 | if detail == None: break | |
78 | details.append(detail) | |
79 | document['details']=details | |
80 | template = self.jinja.get_template('%s.%s' % (self.basename_template, self.outputformat)) | |
81 | output = template.render(document) | |
82 | return self.outputformat, output | |
83 | ||
84 | class demlog(document): | |
85 | def __init__(self, environ): | |
86 | super(demlog, self).__init__(environ, code_document = 'DEM-LOG-AUF') | |
87 | ||
88 | class comlog(document): | |
89 | def __init__(self, environ): | |
90 | super(comlog, self).__init__(environ, code_document = 'COM-LOG-AUF') | |
91 | ||
92 | def application(environ, start_response): | |
93 | """sera lancée par mod_wsgi""" | |
94 | results = mapper.match(environ['PATH_INFO']) | |
95 | filters = parse_qs(environ['QUERY_STRING']) | |
96 | environ['org.auf.routes'] = results | |
97 | environ['org.auf.filters'] = filters | |
98 | try: | |
99 | target_class = globals()[results['controller']] | |
100 | method_name = results['action'] | |
101 | if environ['REQUEST_METHOD'] != 'GET': | |
102 | method_name = environ['REQUEST_METHOD'] + '_' + method_name | |
103 | method = getattr(target_class,method_name) | |
104 | type, output = method(target_class(environ)) | |
105 | start_response("200 OK", [('Content-type', formats[type])]) | |
106 | return output.encode('utf-8') | |
107 | except: | |
108 | start_response("404 NOT FOUND", [('Content-type', 'text/plain')]) | |
109 | return 'erreur lors du traitement\n%s: %s\n%s' % ( sys.exc_info()[0] , sys.exc_info()[1] , traceback.format_exc()) | |
110 | ||
111 | ||
112 | # utilitaires | |
113 | def dict_fetchone(cursor): | |
114 | """Renvoie le resultat d'un fetchone dans un dictionnaire""" | |
115 | result = cursor.fetchone() | |
116 | if result == None: return None | |
117 | result_dict = {} | |
118 | for i in range(len(result)): | |
119 | if isinstance( result[i], str ): | |
120 | result_dict[cursor.description[i][0]] = result[i].decode('iso-8859-1') | |
121 | else: | |
122 | result_dict[cursor.description[i][0]] = result[i] | |
123 | return result_dict | |
124 |