def evass_realm_entity(table, row):
 db = current.db
 s3db = current.s3db
 tablename = table._tablename
 realm_entity = None
 if (tablename in ('event_event', 'evr_case', 'cr_shelter', 'hrm_human_resource', 'org_facility', 'org_office')):
  otable = s3db.org_organisation
  organisation_id = row.organisation_id
  if organisation_id:
   org = db((otable.id == organisation_id)).select(otable.realm_entity, limitby=(0, 1)).first()
   realm_entity = org.realm_entity
 elif (tablename == 'event_incident'):
  etable = db.event_event
  try:
   incident_id = row.id
   query = ((table.id == incident_id) & (etable.id == table.event_id))
   event = db(query).select(etable.realm_entity, limitby=(0, 1)).first()
   realm_entity = event.realm_entity
  except:
   return
 elif (tablename == 'pr_group'):
  user = current.auth.user
  if user:
   realm_entity = s3db.pr_get_pe_id('org_organisation', user.organisation_id)
 elif (tablename == 'org_organisation'):
  realm_entity = row.pe_id
 return realm_entity

def evass_realm_entity ( table , row ) : db = current . db s3db = current . s3db tablename = table . _tablename realm_entity = None if ( tablename in ( 'event_event' , 'evr_case' , 'cr_shelter' , 'hrm_human_resource' , 'org_facility' , 'org_office' ) ) : otable = s3db . org_organisation organisation_id = row . organisation_id if organisation_id : org = db ( ( otable . id == organisation_id ) ) . select ( otable . realm_entity , limitby = ( 0 , 1 ) ) . first ( ) realm_entity = org . realm_entity elif ( tablename == 'event_incident' ) : etable = db . event_event try : incident_id = row . id query = ( ( table . id == incident_id ) & ( etable . id == table . event_id ) ) event = db ( query ) . select ( etable . realm_entity , limitby = ( 0 , 1 ) ) . first ( ) realm_entity = event . realm_entity except : return elif ( tablename == 'pr_group' ) : user = current . auth . user if user : realm_entity = s3db . pr_get_pe_id ( 'org_organisation' , user . organisation_id ) elif ( tablename == 'org_organisation' ) : realm_entity = row . pe_id return realm_entity
assign a realm entity to records .