1
2 """GNUmed auto hints middleware.
3
4 This should eventually end up in a class cPractice.
5 """
6
7 __license__ = "GPL"
8 __author__ = "K.Hilbert <Karsten.Hilbert@gmx.net>"
9
10
11 import sys
12 import logging
13
14
15 if __name__ == '__main__':
16 sys.path.insert(0, '../../')
17 from Gnumed.pycommon import gmPG2
18 from Gnumed.pycommon import gmBusinessDBObject
19 from Gnumed.pycommon import gmTools
20 from Gnumed.pycommon import gmDateTime
21
22 from Gnumed.business import gmStaff
23
24 _log = logging.getLogger('gm.hints')
25
26
27
28
29 _SQL_get_dynamic_hints = "SELECT * FROM ref.v_auto_hints WHERE %s"
30
32 """Represents dynamic hints to be run against the database."""
33
34 _cmd_fetch_payload = _SQL_get_dynamic_hints % "pk_auto_hint = %s"
35 _cmds_store_payload = [
36 """UPDATE ref.auto_hint SET
37 query = gm.nullify_empty_string(%(query)s),
38 recommendation_query = gm.nullify_empty_string(%(recommendation_query)s),
39 title = gm.nullify_empty_string(%(title)s),
40 hint = gm.nullify_empty_string(%(hint)s),
41 url = gm.nullify_empty_string(%(url)s),
42 source = gm.nullify_empty_string(%(source)s),
43 is_active = %(is_active)s,
44 popup_type = %(popup_type)s,
45 highlight_as_priority = %(highlight_as_priority)s
46 WHERE
47 pk = %(pk_auto_hint)s
48 AND
49 xmin = %(xmin_auto_hint)s
50 RETURNING
51 xmin AS xmin_auto_hint
52 """
53 ]
54 _updatable_fields = [
55 'query',
56 'recommendation_query',
57 'title',
58 'hint',
59 'url',
60 'source',
61 'is_active',
62 'popup_type',
63 'highlight_as_priority'
64 ]
65
68
69
111
112
113 - def suppress(self, rationale=None, pk_encounter=None):
119
125
126
128 if order_by is None:
129 order_by = 'TRUE'
130 else:
131 order_by = 'TRUE ORDER BY %s' % order_by
132 cmd = _SQL_get_dynamic_hints % order_by
133 rows, idx = gmPG2.run_ro_queries(link_obj = link_obj, queries = [{'cmd': cmd}], get_col_idx = True)
134 return [ cDynamicHint(row = {'data': r, 'idx': idx, 'pk_field': 'pk_auto_hint'}) for r in rows ]
135
136
138 args = {
139 'query': query,
140 'title': title,
141 'hint': hint,
142 'source': source,
143 'usr': gmStaff.gmCurrentProvider()['db_user']
144 }
145 cmd = """
146 INSERT INTO ref.auto_hint (
147 query,
148 title,
149 hint,
150 source,
151 lang
152 ) VALUES (
153 gm.nullify_empty_string(%(query)s),
154 gm.nullify_empty_string(%(title)s),
155 gm.nullify_empty_string(%(hint)s),
156 gm.nullify_empty_string(%(source)s),
157 i18n.get_curr_lang(%(usr)s)
158 )
159 RETURNING pk
160 """
161 rows, idx = gmPG2.run_rw_queries(link_obj = link_obj, queries = [{'cmd': cmd, 'args': args}], return_data = True, get_col_idx = True)
162 return cDynamicHint(aPK_obj = rows[0]['pk'], link_obj = link_obj)
163
164
166 args = {'pk': pk_hint}
167 cmd = "DELETE FROM ref.auto_hint WHERE pk = %(pk)s"
168 gmPG2.run_rw_queries(link_obj = link_obj, queries = [{'cmd': cmd, 'args': args}])
169 return True
170
171
173 conn = gmPG2.get_connection()
174 curs = conn.cursor()
175 curs.callproc('clin.get_hints_for_patient', [pk_identity])
176 rows = curs.fetchall()
177 idx = gmPG2.get_col_indices(curs)
178 curs.close()
179 conn.rollback()
180
181 applying_rows = []
182 for row in rows:
183 if row['rationale4suppression'] is None:
184 applying_rows.append(row)
185 continue
186 if row['rationale4suppression'].startswith('magic_tag::'):
187 _log.debug('hint with magic tag: %s', row['rationale4suppression'])
188 if 'suppression_needs_invalidation' in row['rationale4suppression']:
189 _log.debug('database asks for invalidation of suppression of hint [%s]', row)
190 if pk_encounter is not None:
191 invalidate_hint_suppression(pk_hint = row['pk_auto_hint'], pk_encounter = pk_encounter)
192 if 'does_not_apply' in row['rationale4suppression']:
193 continue
194
195
196
197 applying_rows.append(row)
198
199 return [ cDynamicHint(row = {'data': r, 'idx': idx, 'pk_field': 'pk_auto_hint'}) for r in applying_rows ]
200
201
203 args = {
204 'hint': pk_hint,
205 'rationale': rationale,
206 'enc': pk_encounter
207 }
208 cmd = """
209 DELETE FROM clin.suppressed_hint
210 WHERE
211 fk_hint = %(hint)s
212 AND
213 fk_encounter IN (
214 SELECT pk FROM clin.encounter WHERE fk_patient = (
215 SELECT fk_patient FROM clin.encounter WHERE pk = %(enc)s
216 )
217 )
218 """
219 queries = [{'cmd': cmd, 'args': args}]
220 cmd = """
221 INSERT INTO clin.suppressed_hint (
222 fk_encounter,
223 fk_hint,
224 rationale,
225 md5_sum
226 ) VALUES (
227 %(enc)s,
228 %(hint)s,
229 %(rationale)s,
230 (SELECT r_vah.md5_sum FROM ref.v_auto_hints r_vah WHERE r_vah.pk_auto_hint = %(hint)s)
231 )
232 """
233 queries.append({'cmd': cmd, 'args': args})
234 gmPG2.run_rw_queries(queries = queries)
235 return True
236
237
238
239
240 _SQL_get_suppressed_hints = "SELECT * FROM clin.v_suppressed_hints WHERE %s"
241
280
281
283 args = {'pat': pk_identity}
284 if pk_identity is None:
285 where = 'true'
286 else:
287 where = "pk_identity = %(pat)s"
288 if order_by is None:
289 order_by = ''
290 else:
291 order_by = ' ORDER BY %s' % order_by
292 cmd = (_SQL_get_suppressed_hints % where) + order_by
293 rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd, 'args': args}], get_col_idx = True)
294 return [ cSuppressedHint(row = {'data': r, 'idx': idx, 'pk_field': 'pk_suppressed_hint'}) for r in rows ]
295
296
298 args = {'pk': pk_suppressed_hint}
299 cmd = "DELETE FROM clin.suppressed_hint WHERE pk = %(pk)s"
300 gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}])
301 return True
302
303
305 _log.debug('invalidating suppression of hint #%s', pk_hint)
306 args = {
307 'pk_hint': pk_hint,
308 'enc': pk_encounter,
309 'fake_md5': '***INVALIDATED***'
310 }
311 cmd = """
312 UPDATE clin.suppressed_hint SET
313 fk_encounter = %(enc)s,
314 md5_sum = %(fake_md5)s
315 WHERE
316 pk = (
317 SELECT pk_suppressed_hint
318 FROM clin.v_suppressed_hints
319 WHERE
320 pk_hint = %(pk_hint)s
321 AND
322 pk_identity = (
323 SELECT fk_patient FROM clin.encounter WHERE pk = %(enc)s
324 )
325 )
326 """
327 gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}])
328 return True
329
330
331 if __name__ == '__main__':
332
333 if len(sys.argv) < 2:
334 sys.exit()
335
336 if sys.argv[1] != 'test':
337 sys.exit()
338
339 from Gnumed.pycommon import gmI18N
340
341 gmI18N.activate_locale()
342 gmI18N.install_domain()
343
344
350
351 test_auto_hints()
352