Package Gnumed :: Package business :: Module gmProviderInbox
[frames] | no frames]

Source Code for Module Gnumed.business.gmProviderInbox

  1  # -*- coding: latin-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   
 21  from Gnumed.business import gmStaff 
 22   
 23  #============================================================ 
 24  # description 
 25  #------------------------------------------------------------ 
 26  _SQL_get_inbox_messages = u"SELECT * FROM dem.v_message_inbox WHERE %s" 
 27   
28 -class cInboxMessage(gmBusinessDBObject.cBusinessDBObject):
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 = %(pk_context)s 41 WHERE 42 pk = %(pk_inbox_message)s 43 AND 44 xmin = %(xmin_message_inbox)s 45 RETURNING 46 pk as pk_inbox_message, 47 xmin as xmin_message_inbox 48 """ 49 ] 50 _updatable_fields = [ 51 u'pk_staff', 52 u'pk_type', 53 u'comment', 54 u'data', 55 u'importance', 56 u'pk_patient', 57 u'ufk_context' 58 ]
59 #------------------------------------------------------------
60 -def get_inbox_messages(pk_staff=None, pk_patient=None, include_without_provider=False, order_by=None):
61 62 if order_by is None: 63 order_by = u'%s ORDER BY importance desc, received_when desc' 64 else: 65 order_by = u'%%s ORDER BY %s' % order_by 66 67 args = {} 68 where_parts = [] 69 70 if pk_staff is not None: 71 if include_without_provider: 72 where_parts.append(u'pk_staff in (%(staff)s, NULL)') 73 else: 74 where_parts.append(u'pk_staff = %(staff)s') 75 args['staff'] = pk_staff 76 77 if pk_patient is not None: 78 where_parts.append(u'pk_patient = %(pat)s') 79 args['pat'] = pk_patient 80 81 cmd = _SQL_get_inbox_messages % ( 82 order_by % u' AND '.join(where_parts) 83 ) 84 rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd, 'args': args}], get_col_idx = True) 85 86 return [ cInboxMessage(row = {'data': r, 'idx': idx, 'pk_field': 'pk_inbox_message'}) for r in rows ]
87 #------------------------------------------------------------
88 -def create_inbox_message(message_type=None, subject=None, patient=None, staff=None):
89 90 success, pk_type = gmTools.input2int(initial = message_type) 91 if not success: 92 pk_type = create_inbox_item_type(message_type = message_type) 93 94 cmd = u""" 95 INSERT INTO dem.message_inbox ( 96 fk_staff, 97 fk_patient, 98 fk_inbox_item_type, 99 comment 100 ) VALUES ( 101 %(staff)s, 102 %(pat)s, 103 %(type)s, 104 gm.nullify_empty_string(%(subject)s) 105 ) 106 RETURNING pk 107 """ 108 args = { 109 u'staff': staff, 110 u'pat': patient, 111 u'type': pk_type, 112 u'subject': subject 113 } 114 rows, idx = gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}], return_data = True, get_col_idx = False) 115 116 return cInboxMessage(aPK_obj = rows[0]['pk'])
117 #------------------------------------------------------------
118 -def delete_inbox_message(inbox_message=None):
119 args = {'pk': inbox_message} 120 cmd = u"DELETE FROM dem.message_inbox WHERE pk = %(pk)s" 121 gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}]) 122 return True
123 #------------------------------------------------------------
124 -def create_inbox_item_type(message_type=None, category=u'clinical'):
125 126 # determine category PK 127 success, pk_cat = gmTools.input2int(initial = category) 128 if not success: 129 args = {u'cat': category} 130 cmd = u"""SELECT COALESCE ( 131 (SELECT pk FROM dem.inbox_item_category WHERE _(description) = %(cat)s), 132 (SELECT pk FROM dem.inbox_item_category WHERE description = %(cat)s) 133 ) AS pk""" 134 rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd, 'args': args}]) 135 if rows[0]['pk'] is None: 136 cmd = u"INSERT INTO dem.inbox_item_category (description) VALUES (%(cat)s) RETURNING pk" 137 rows, idx = gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}], return_data = True) 138 pk_cat = rows[0]['pk'] 139 else: 140 pk_cat = rows[0]['pk'] 141 142 # find type PK or create type 143 args = {u'cat': pk_cat, u'type': message_type} 144 cmd = u"""SELECT COALESCE ( 145 (SELECT pk FROM dem.inbox_item_type where fk_inbox_item_category = %(cat)s and _(description) = %(type)s), 146 (SELECT pk FROM dem.inbox_item_type where fk_inbox_item_category = %(cat)s and description = %(type)s) 147 ) AS pk""" 148 rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd, 'args': args}]) 149 if rows[0]['pk'] is None: 150 cmd = u""" 151 INSERT INTO dem.inbox_item_type ( 152 fk_inbox_item_category, 153 description, 154 is_user 155 ) VALUES ( 156 %(cat)s, 157 %(type)s, 158 TRUE 159 ) RETURNING pk""" 160 rows, idx = gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}], return_data = True) 161 162 return rows[0]['pk']
163 #============================================================ 164 #============================================================
165 -class cProviderInbox:
166 - def __init__(self, provider_id=None):
167 if provider_id is None: 168 self.__provider_id = gmStaff.gmCurrentProvider()['pk_staff'] 169 else: 170 self.__provider_id = provider_id
171 #--------------------------------------------------------
172 - def delete_message(self, pk=None):
173 return delete_inbox_message(inbox_message = pk)
174 #--------------------------------------------------------
175 - def add_message(message_type=None, subject=None, patient=None):
176 return create_inbox_message ( 177 message_type = message_type, 178 subject = subject, 179 patient = patient, 180 staff = self.__provider_id 181 )
182 #-------------------------------------------------------- 183 # properties 184 #--------------------------------------------------------
185 - def _get_messages(self):
186 return get_inbox_messages(pk_staff = self.__provider_id)
187
188 - def _set_messages(self, messages):
189 return
190 191 messages = property(_get_messages, _set_messages)
192 #============================================================ 193 if __name__ == '__main__': 194 195 if len(sys.argv) < 2: 196 sys.exit() 197 198 if sys.argv[1] != 'test': 199 sys.exit() 200 201 from Gnumed.pycommon import gmI18N 202 203 gmI18N.activate_locale() 204 gmI18N.install_domain() 205 206 #---------------------------------------
207 - def test_inbox():
208 gmStaff.gmCurrentProvider(provider = gmStaff.cStaff()) 209 inbox = cProviderInbox() 210 for msg in inbox.messages: 211 print msg
212 #---------------------------------------
213 - def test_msg():
214 msg = cInboxMessage(aPK_obj = 1) 215 print msg
216 #---------------------------------------
217 - def test_create_type():
218 print create_inbox_item_type(message_type = 'test')
219 #--------------------------------------- 220 #test_inbox() 221 #test_msg() 222 test_create_type() 223 224 #============================================================ 225