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