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)')
154 else:
155 where_parts.append(u'pk_staff = %(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 if __name__ == '__main__':
275
276 if len(sys.argv) < 2:
277 sys.exit()
278
279 if sys.argv[1] != 'test':
280 sys.exit()
281
282 from Gnumed.pycommon import gmI18N
283
284 gmI18N.activate_locale()
285 gmI18N.install_domain()
286
287
293
297
300
304
305
306
307
308 test_due()
309
310
311