1
2 """GNUmed provider inbox 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
13
14 if __name__ == '__main__':
15 sys.path.insert(0, '../../')
16 from Gnumed.pycommon import gmPG2
17 from Gnumed.pycommon import gmBusinessDBObject
18 from Gnumed.pycommon import gmTools
19 from Gnumed.pycommon import gmDateTime
20
21 from Gnumed.business import gmStaff
22
23
24
25
26 _SQL_get_inbox_messages = u"SELECT * FROM dem.v_message_inbox WHERE %s"
27
29
30 _cmd_fetch_payload = _SQL_get_inbox_messages % u"pk_inbox_message = %s"
31 _cmds_store_payload = [
32 u"""
33 UPDATE dem.message_inbox SET
34 fk_staff = %(pk_staff)s,
35 fk_inbox_item_type = %(pk_type)s,
36 comment = gm.nullify_empty_string(%(comment)s),
37 data = gm.nullify_empty_string(%(data)s),
38 importance = %(importance)s,
39 fk_patient = %(pk_patient)s,
40 ufk_context = NULLIF(%(pk_context)s::integer[], ARRAY[NULL::integer]),
41 due_date = %(due_date)s,
42 expiry_date = %(expiry_date)s
43 WHERE
44 pk = %(pk_inbox_message)s
45 AND
46 xmin = %(xmin_message_inbox)s
47 RETURNING
48 pk as pk_inbox_message,
49 xmin as xmin_message_inbox
50 """
51 ]
52 _updatable_fields = [
53 u'pk_staff',
54 u'pk_type',
55 u'comment',
56 u'data',
57 u'importance',
58 u'pk_patient',
59 u'pk_context',
60 u'due_date',
61 u'expiry_date'
62 ]
63
118
120
121 if order_by is None:
122 order_by = u'%s ORDER BY due_date, importance DESC, received_when DESC'
123 else:
124 order_by = u'%%s ORDER BY %s' % order_by
125
126 args = {'pat': pk_patient}
127 where_parts = [
128 u'pk_patient = %(pat)s',
129 u'due_date IS NOT NULL'
130 ]
131
132 cmd = u"SELECT * FROM dem.v_message_inbox WHERE %s" % (
133 order_by % u' AND '.join(where_parts)
134 )
135 rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd, 'args': args}], get_col_idx = True)
136
137 return [ cInboxMessage(row = {'data': r, 'idx': idx, 'pk_field': 'pk_inbox_message'}) for r in rows ]
138
139
141
142 if order_by is None:
143 order_by = u'%s ORDER BY due_date, importance DESC, received_when DESC'
144 else:
145 order_by = u'%%s ORDER BY %s' % order_by
146
147 args = {'pat': pk_patient}
148 where_parts = [
149 u'pk_patient = %(pat)s',
150 u'is_due IS TRUE'
151 ]
152
153 cmd = u"SELECT * FROM dem.v_message_inbox WHERE %s" % (
154 order_by % u' AND '.join(where_parts)
155 )
156 rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd, 'args': args}], get_col_idx = True)
157
158 return [ cInboxMessage(row = {'data': r, 'idx': idx, 'pk_field': 'pk_inbox_message'}) for r in rows ]
159
160
161 -def get_inbox_messages(pk_staff=None, pk_patient=None, include_without_provider=False, order_by=None):
162
163 if order_by is None:
164 order_by = u'%s ORDER BY importance desc, received_when desc'
165 else:
166 order_by = u'%%s ORDER BY %s' % order_by
167
168 args = {}
169 where_parts = []
170
171 if pk_staff is not None:
172 if include_without_provider:
173 where_parts.append(u'pk_staff IN (%(staff)s, NULL) OR modified_by = (SELECT short_alias FROM dem.staff WHERE pk = %(staff)s)')
174 else:
175 where_parts.append(u'pk_staff = %(staff)s OR modified_by = (SELECT short_alias FROM dem.staff WHERE pk = %(staff)s)')
176 args['staff'] = pk_staff
177
178 if pk_patient is not None:
179 where_parts.append(u'pk_patient = %(pat)s')
180 args['pat'] = pk_patient
181
182 cmd = _SQL_get_inbox_messages % (
183 order_by % u' AND '.join(where_parts)
184 )
185 rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd, 'args': args}], get_col_idx = True)
186
187 return [ cInboxMessage(row = {'data': r, 'idx': idx, 'pk_field': 'pk_inbox_message'}) for r in rows ]
188
190
191 success, pk_type = gmTools.input2int(initial = message_type)
192 if not success:
193 pk_type = create_inbox_item_type(message_type = message_type)
194
195 cmd = u"""
196 INSERT INTO dem.message_inbox (
197 fk_staff,
198 fk_patient,
199 fk_inbox_item_type,
200 comment
201 ) VALUES (
202 %(staff)s,
203 %(pat)s,
204 %(type)s,
205 gm.nullify_empty_string(%(subject)s)
206 )
207 RETURNING pk
208 """
209 args = {
210 u'staff': staff,
211 u'pat': patient,
212 u'type': pk_type,
213 u'subject': subject
214 }
215 rows, idx = gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}], return_data = True, get_col_idx = False)
216
217 return cInboxMessage(aPK_obj = rows[0]['pk'])
218
220 args = {'pk': inbox_message}
221 cmd = u"DELETE FROM dem.message_inbox WHERE pk = %(pk)s"
222 gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}])
223 return True
224
226
227
228 success, pk_cat = gmTools.input2int(initial = category)
229 if not success:
230 args = {u'cat': category}
231 cmd = u"""SELECT COALESCE (
232 (SELECT pk FROM dem.inbox_item_category WHERE _(description) = %(cat)s),
233 (SELECT pk FROM dem.inbox_item_category WHERE description = %(cat)s)
234 ) AS pk"""
235 rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd, 'args': args}])
236 if rows[0]['pk'] is None:
237 cmd = u"INSERT INTO dem.inbox_item_category (description) VALUES (%(cat)s) RETURNING pk"
238 rows, idx = gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}], return_data = True)
239 pk_cat = rows[0]['pk']
240 else:
241 pk_cat = rows[0]['pk']
242
243
244 args = {u'cat': pk_cat, u'type': message_type}
245 cmd = u"""SELECT COALESCE (
246 (SELECT pk FROM dem.inbox_item_type where fk_inbox_item_category = %(cat)s and _(description) = %(type)s),
247 (SELECT pk FROM dem.inbox_item_type where fk_inbox_item_category = %(cat)s and description = %(type)s)
248 ) AS pk"""
249 rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd, 'args': args}])
250 if rows[0]['pk'] is None:
251 cmd = u"""
252 INSERT INTO dem.inbox_item_type (
253 fk_inbox_item_category,
254 description,
255 is_user
256 ) VALUES (
257 %(cat)s,
258 %(type)s,
259 TRUE
260 ) RETURNING pk"""
261 rows, idx = gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}], return_data = True)
262
263 return rows[0]['pk']
264
265
293
294
295
296
297 _SQL_get_dynamic_hints = u"SELECT *, xmin AS xmin_auto_hint FROM ref.auto_hint WHERE %s"
298
300 """Represents dynamic hints to be run against the database."""
301
302 _cmd_fetch_payload = _SQL_get_dynamic_hints % u"pk = %s"
303 _cmds_store_payload = [
304 u"""UPDATE ref.auto_hint SET
305 is_active = %(is_active)s
306 WHERE
307 pk = %(pk)s
308 AND
309 xmin = %(xmin_auto_hint)s
310 RETURNING
311 pk,
312 xmin AS xmin_auto_hint
313 """
314 ]
315 _updatable_fields = [
316 u'is_active'
317 ]
318
340
341
343 if order_by is None:
344 order_by = u'true'
345 else:
346 order_by = u'true ORDER BY %s' % order_by
347
348 cmd = _SQL_get_dynamic_hints % order_by
349 rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd}], get_col_idx = True)
350 return [ cDynamicHint(row = {'data': r, 'idx': idx, 'pk_field': 'pk'}) for r in rows ]
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
383 conn = gmPG2.get_connection()
384 curs = conn.cursor()
385 curs.callproc('clin.get_hints_for_patient', [pk_identity])
386 rows = curs.fetchall()
387 idx = gmPG2.get_col_indices(curs)
388 return [ cDynamicHint(row = {'data': r, 'idx': idx, 'pk_field': 'pk'}) for r in rows ]
389
390
391 if __name__ == '__main__':
392
393 if len(sys.argv) < 2:
394 sys.exit()
395
396 if sys.argv[1] != 'test':
397 sys.exit()
398
399 from Gnumed.pycommon import gmI18N
400
401 gmI18N.activate_locale()
402 gmI18N.install_domain()
403
404
410
414
417
421
425
426
427
428
429
430 test_auto_hints()
431
432
433