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

Source Code for Module Gnumed.business.gmExternalCare

  1  # -*- coding: utf-8 -*- 
  2  """GNUmed external care classes. 
  3  """ 
  4  #============================================================ 
  5  __author__ = "K.Hilbert <Karsten.Hilbert@gmx.net>" 
  6  __license__ = "GPL v2 or later" 
  7   
  8  # standard libs 
  9  import sys 
 10  import logging 
 11   
 12   
 13  if __name__ == '__main__': 
 14          sys.path.insert(0, '../../') 
 15   
 16  from Gnumed.pycommon import gmBusinessDBObject 
 17  from Gnumed.pycommon import gmPG2 
 18  from Gnumed.pycommon import gmI18N 
 19   
 20  from Gnumed.business import gmOrganization 
 21   
 22  if __name__ == '__main__': 
 23          gmI18N.activate_locale() 
 24          gmI18N.install_domain() 
 25   
 26  from Gnumed.pycommon import gmTools 
 27   
 28  _log = logging.getLogger('gm.ext_care') 
 29   
 30  #============================================================ 
 31  _SQL_get_external_care_items = """SELECT * FROM clin.v_external_care WHERE %s""" 
 32   
33 -class cExternalCareItem(gmBusinessDBObject.cBusinessDBObject):
34 """Represents an external care item. 35 36 Note: Upon saving .issue being (non-empty AND not None) will 37 override .fk_health_issue (IOW, if your code wants to set 38 .fk_health_issue to something other than NULL it needs to 39 unset .issue explicitly (to u'' or None)). 40 """ 41 _cmd_fetch_payload = _SQL_get_external_care_items % "pk_external_care = %s" 42 _cmds_store_payload = [ 43 """UPDATE clin.external_care SET 44 comment = gm.nullify_empty_string(%(comment)s), 45 fk_encounter = %(pk_encounter)s, 46 issue = gm.nullify_empty_string(%(issue)s), 47 provider = gm.nullify_empty_string(%(provider)s), 48 fk_org_unit = %(pk_org_unit)s, 49 inactive = %(inactive)s, 50 fk_health_issue = ( 51 CASE 52 WHEN gm.is_null_or_blank_string(%(issue)s) IS TRUE THEN %(pk_health_issue)s 53 ELSE NULL 54 END 55 )::integer 56 WHERE 57 pk = %(pk_external_care)s 58 AND 59 xmin = %(xmin_external_care)s 60 RETURNING 61 xmin AS xmin_external_care 62 """, 63 _SQL_get_external_care_items % "pk_external_care = %(pk_external_care)s" 64 ] 65 _updatable_fields = [ 66 'pk_encounter', 67 'pk_health_issue', 68 'pk_org_unit', 69 'issue', 70 'provider', 71 'comment', 72 'inactive' 73 ] 74 #--------------------------------------------------------
75 - def format(self, with_health_issue=True, with_address=False, with_comms=False):
76 lines = [] 77 lines.append(_('External care%s #%s') % ( 78 gmTools.bool2subst ( 79 self._payload[self._idx['inactive']], 80 ' (%s)' % _('inactive'), 81 '', 82 ' [ERROR: .inactive is NULL]' 83 ), 84 self._payload[self._idx['pk_external_care']] 85 )) 86 if with_health_issue: 87 if self._payload[self._idx['pk_health_issue']] is None: 88 lines.append(' ' + _('Issue: %s') % self._payload[self._idx['issue']]) 89 else: 90 lines.append(' ' + _('Health issue: %s') % self._payload[self._idx['issue']]) 91 lines.append(' (' + _('also treated here') + ')') 92 if self._payload[self._idx['provider']] is not None: 93 lines.append(' ' + _('Provider: %s') % self._payload[self._idx['provider']]) 94 lines.append(' ' + _('Location: %s@%s') % (self._payload[self._idx['unit']], self._payload[self._idx['organization']])) 95 unit = self.org_unit 96 if with_address: 97 adr = unit.address 98 if adr is not None: 99 lines.extend(adr.format()) 100 if with_comms: 101 for comm in unit.comm_channels: 102 lines.append(' %s: %s%s' % ( 103 comm['l10n_comm_type'], 104 comm['url'], 105 gmTools.bool2subst(comm['is_confidential'], _(' (confidential)'), '', '') 106 )) 107 if self._payload[self._idx['comment']] is not None: 108 lines.append('') 109 lines.append(' ' + self._payload[self._idx['comment']]) 110 111 return lines
112 #--------------------------------------------------------
113 - def _get_org_unit(self):
114 return gmOrganization.cOrgUnit(self._payload[self._idx['pk_org_unit']])
115 116 org_unit = property(_get_org_unit, lambda x:x)
117 118 #------------------------------------------------------------
119 -def get_external_care_items(order_by=None, pk_identity=None, pk_health_issue=None, exclude_inactive=False):
120 121 args = { 122 'pk_pat': pk_identity, 123 'pk_issue': pk_health_issue 124 } 125 where_parts = [] 126 if pk_identity is not None: 127 where_parts.append('pk_identity = %(pk_pat)s') 128 if pk_health_issue is not None: 129 where_parts.append('pk_health_issue = %(pk_issue)s') 130 if exclude_inactive is True: 131 where_parts.append('inactive IS FALSE') 132 133 if len(where_parts) == 0: 134 where = 'TRUE' 135 else: 136 where = ' AND '.join(where_parts) 137 138 if order_by is not None: 139 where = '%s ORDER BY %s' % ( 140 where, 141 order_by 142 ) 143 144 cmd = _SQL_get_external_care_items % where 145 rows, idx = gmPG2.run_ro_queries(queries = [{'cmd': cmd, 'args': args}], get_col_idx = True) 146 return [ cExternalCareItem(row = {'data': r, 'idx': idx, 'pk_field': 'pk_external_care'}) for r in rows ]
147 148 #------------------------------------------------------------
149 -def create_external_care_item(pk_health_issue=None, issue=None, pk_org_unit=None, pk_encounter=None):
150 args = { 151 'pk_health_issue': pk_health_issue, 152 'issue': issue, 153 'pk_org_unit': pk_org_unit, 154 'enc': pk_encounter 155 } 156 cmd = """ 157 INSERT INTO clin.external_care ( 158 issue, 159 fk_health_issue, 160 fk_encounter, 161 fk_org_unit 162 ) VALUES ( 163 gm.nullify_empty_string(%(issue)s), 164 (CASE 165 WHEN gm.is_null_or_blank_string(%(issue)s) IS TRUE THEN %(pk_health_issue)s 166 ELSE NULL 167 END)::integer, 168 %(enc)s, 169 %(pk_org_unit)s 170 ) 171 RETURNING pk""" 172 rows, idx = gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}], return_data = True, get_col_idx = False) 173 174 return cExternalCareItem(aPK_obj = rows[0]['pk'])
175 176 #------------------------------------------------------------
177 -def delete_external_care_item(pk_external_care=None):
178 args = {'pk': pk_external_care} 179 cmd = "DELETE FROM clin.external_care WHERE pk = %(pk)s" 180 gmPG2.run_rw_queries(queries = [{'cmd': cmd, 'args': args}]) 181 return True
182 183 #============================================================ 184 # main 185 #------------------------------------------------------------ 186 if __name__ == "__main__": 187 188 if len(sys.argv) == 1: 189 sys.exit() 190 191 if sys.argv[1] != 'test': 192 sys.exit() 193 194 #from Gnumed.pycommon import gmLog2 195 #-----------------------------------------
196 - def test_get_care_items():
197 for item in get_external_care_items(pk_identity = 12, order_by = 'provider'): 198 print(item.format())
199 200 #----------------------------------------- 201 test_get_care_items() 202