1
2 """GNUmed provider inbox middleware.
3
4 This should eventually end up in a class cPractice.
5 """
6
7 __license__ = "GPL"
8 __version__ = "$Revision: 1.14 $"
9 __author__ = "K.Hilbert <Karsten.Hilbert@gmx.net>"
10
11
12 import sys
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
25
26
27 _SQL_get_inbox_messages = u"SELECT * FROM dem.v_message_inbox WHERE %s"
28
30
31 _cmd_fetch_payload = _SQL_get_inbox_messages % u"pk_inbox_message = %s"
32 _cmds_store_payload = [
33 u"""
34 UPDATE dem.message_inbox SET
35 fk_staff = %(pk_staff)s,
36 fk_inbox_item_type = %(pk_type)s,
37 comment = gm.nullify_empty_string(%(comment)s),
38 data = gm.nullify_empty_string(%(data)s),
39 importance = %(importance)s,
40 fk_patient = %(pk_patient)s,
41 ufk_context = %(pk_context)s,
42 due_date = %(due_date)s,
43 expiry_date = %(expiry_date)s
44 WHERE
45 pk = %(pk_inbox_message)s
46 AND
47 xmin = %(xmin_message_inbox)s
48 RETURNING
49 pk as pk_inbox_message,
50 xmin as xmin_message_inbox
51 """
52 ]
53 _updatable_fields = [
54 u'pk_staff',
55 u'pk_type',
56 u'comment',
57 u'data',
58 u'importance',
59 u'pk_patient',
60 u'ufk_context',
61 u'due_date',
62 u'expiry_date'
63 ]
64
119
121
122 if order_by is None:
123 order_by = u'%s ORDER BY due_date, importance DESC, received_when DESC'
124 else:
125 order_by = u'%%s ORDER BY %s' % order_by
126
127 args = {'pat': pk_patient}
128 where_parts = [
129 u'pk_patient = %(pat)s',
130 u'is_due IS TRUE'
131 ]
132
133 cmd = u"SELECT *, now() - due_date AS interval_due FROM dem.v_message_inbox WHERE %s" % (
134 order_by % u' AND '.join(where_parts)
135 )
136 rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd, 'args': args}], get_col_idx = True)
137
138 return [ cInboxMessage(row = {'data': r, 'idx': idx, 'pk_field': 'pk_inbox_message'}) for r in rows ]
139
140
141 -def get_inbox_messages(pk_staff=None, pk_patient=None, include_without_provider=False, order_by=None):
142
143 if order_by is None:
144 order_by = u'%s ORDER BY importance desc, received_when desc'
145 else:
146 order_by = u'%%s ORDER BY %s' % order_by
147
148 args = {}
149 where_parts = []
150
151 if pk_staff is not None:
152 if include_without_provider:
153 where_parts.append(u'pk_staff IN (%(staff)s, NULL) OR modified_by = (SELECT short_alias FROM dem.staff WHERE pk = %(staff)s)')
154 else:
155 where_parts.append(u'pk_staff = %(staff)s OR modified_by = (SELECT short_alias FROM dem.staff WHERE pk = %(staff)s)')
156 args['staff'] = pk_staff
157
158 if pk_patient is not None:
159 where_parts.append(u'pk_patient = %(pat)s')
160 args['pat'] = pk_patient
161
162 cmd = _SQL_get_inbox_messages % (
163 order_by % u' AND '.join(where_parts)
164 )
165 rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd, 'args': args}], get_col_idx = True)
166
167 return [ cInboxMessage(row = {'data': r, 'idx': idx, 'pk_field': 'pk_inbox_message'}) for r in rows ]
168
170
171 success, pk_type = gmTools.input2int(initial = message_type)
172 if not success:
173 pk_type = create_inbox_item_type(message_type = message_type)
174
175 cmd = u"""
176 INSERT INTO dem.message_inbox (
177 fk_staff,
178 fk_patient,
179 fk_inbox_item_type,
180 comment
181 ) VALUES (
182 %(staff)s,
183 %(pat)s,
184 %(type)s,
185 gm.nullify_empty_string(%(subject)s)
186 )
187 RETURNING pk
188 """
189 args = {
190 u'staff': staff,
191 u'pat': patient,
192 u'type': pk_type,
193 u'subject': subject
194 }
195 rows, idx = gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}], return_data = True, get_col_idx = False)
196
197 return cInboxMessage(aPK_obj = rows[0]['pk'])
198
200 args = {'pk': inbox_message}
201 cmd = u"DELETE FROM dem.message_inbox WHERE pk = %(pk)s"
202 gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}])
203 return True
204
206
207
208 success, pk_cat = gmTools.input2int(initial = category)
209 if not success:
210 args = {u'cat': category}
211 cmd = u"""SELECT COALESCE (
212 (SELECT pk FROM dem.inbox_item_category WHERE _(description) = %(cat)s),
213 (SELECT pk FROM dem.inbox_item_category WHERE description = %(cat)s)
214 ) AS pk"""
215 rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd, 'args': args}])
216 if rows[0]['pk'] is None:
217 cmd = u"INSERT INTO dem.inbox_item_category (description) VALUES (%(cat)s) RETURNING pk"
218 rows, idx = gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}], return_data = True)
219 pk_cat = rows[0]['pk']
220 else:
221 pk_cat = rows[0]['pk']
222
223
224 args = {u'cat': pk_cat, u'type': message_type}
225 cmd = u"""SELECT COALESCE (
226 (SELECT pk FROM dem.inbox_item_type where fk_inbox_item_category = %(cat)s and _(description) = %(type)s),
227 (SELECT pk FROM dem.inbox_item_type where fk_inbox_item_category = %(cat)s and description = %(type)s)
228 ) AS pk"""
229 rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd, 'args': args}])
230 if rows[0]['pk'] is None:
231 cmd = u"""
232 INSERT INTO dem.inbox_item_type (
233 fk_inbox_item_category,
234 description,
235 is_user
236 ) VALUES (
237 %(cat)s,
238 %(type)s,
239 TRUE
240 ) RETURNING pk"""
241 rows, idx = gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}], return_data = True)
242
243 return rows[0]['pk']
244
245
273
274
275
276
277 _SQL_get_dynamic_hints = u"SELECT *, xmin AS xmin_auto_hint FROM ref.auto_hint WHERE %s"
278
280 """Represents dynamic hints to be run against the database."""
281
282 _cmd_fetch_payload = _SQL_get_dynamic_hints % u"pk = %s"
283 _cmds_store_payload = [
284 u"""UPDATE ref.auto_hint SET
285 is_active = %(is_active)s
286 WHERE
287 pk = %(pk)s
288 AND
289 xmin = %(xmin_auto_hint)s
290 RETURNING
291 pk,
292 xmin AS xmin_auto_hint
293 """
294 ]
295 _updatable_fields = [
296 u'is_active'
297 ]
298
320
321
323 if order_by is None:
324 order_by = u'true'
325 else:
326 order_by = u'true ORDER BY %s' % order_by
327
328 cmd = _SQL_get_dynamic_hints % order_by
329 rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd}], get_col_idx = True)
330 return [ cDynamicHint(row = {'data': r, 'idx': idx, 'pk_field': 'pk'}) for r in rows ]
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
363 conn = gmPG2.get_connection()
364 curs = conn.cursor()
365 curs.callproc('clin.get_hints_for_patient', [pk_identity])
366 rows = curs.fetchall()
367 idx = gmPG2.get_col_indices(curs)
368 return [ cDynamicHint(row = {'data': r, 'idx': idx, 'pk_field': 'pk'}) for r in rows ]
369
370
371 if __name__ == '__main__':
372
373 if len(sys.argv) < 2:
374 sys.exit()
375
376 if sys.argv[1] != 'test':
377 sys.exit()
378
379 from Gnumed.pycommon import gmI18N
380
381 gmI18N.activate_locale()
382 gmI18N.install_domain()
383
384
390
394
397
401
405
406
407
408
409
410 test_auto_hints()
411
412
413