def _get_user_location(user, domain): ''' Takes a couch_user and returns that users parentage and the location code ''' try: user_location_id = user.get_domain_membership(domain).location_id loc = SQLLocation.by_location_id(user_location_id) location_type_code = loc.location_type.code # Assuming no web users below block level state_code = 'All' district_code = 'All' block_code = 'All' if location_type_code == 'state': state_code = loc.site_code elif location_type_code == 'district': state_code = loc.parent.site_code district_code = loc.site_code elif location_type_code == 'block': state_code = loc.parent.parent.site_code district_code = loc.parent.site_code block_code = loc.site_code user_site_code = loc.site_code except Exception: location_type_code = 'national' user_site_code = '' state_code = 'All' district_code = 'All' block_code = 'All' return location_type_code, user_site_code, state_code, district_code, block_code
def get_location(location_id): location = SQLLocation.by_location_id(location_id) if not location: raise CommandError("Could not find location %s" % location_id) return location
def get_location(self, location_id): location = SQLLocation.by_location_id(location_id) if not location: raise CommandError("Could not find location %s" % location_id) return location
def get_owner_location(domain, owner_id): owner = get_wrapped_owner(owner_id) if not owner: return None if isinstance(owner, SQLLocation): return owner location_id = owner.get_location_id(domain) return SQLLocation.by_location_id(location_id) if location_id else None
def recipient(self): if self.recipient_type == self.RECIPIENT_TYPE_CASE: try: case = CaseAccessors(self.domain).get_case(self.recipient_id) except CaseNotFound: return None if case.domain != self.domain: return None return case elif self.recipient_type == self.RECIPIENT_TYPE_MOBILE_WORKER: user = CouchUser.get_by_user_id(self.recipient_id, domain=self.domain) if not isinstance(user, CommCareUser): return None return user elif self.recipient_type == self.RECIPIENT_TYPE_WEB_USER: user = CouchUser.get_by_user_id(self.recipient_id, domain=self.domain) if not isinstance(user, WebUser): return None return user elif self.recipient_type == self.RECIPIENT_TYPE_CASE_GROUP: try: group = CommCareCaseGroup.get(self.recipient_id) except ResourceNotFound: return None if group.domain != self.domain: return None return group elif self.recipient_type == self.RECIPIENT_TYPE_USER_GROUP: try: group = Group.get(self.recipient_id) except ResourceNotFound: return None if group.domain != self.domain: return None return group elif self.recipient_type == self.RECIPIENT_TYPE_LOCATION: location = SQLLocation.by_location_id(self.recipient_id) if location is None: return None if location.domain != self.domain: return None return location else: raise UnknownRecipientType(self.recipient_type)
def get_location(self, sms): location_id = self.get_location_id(sms) if not location_id: return None if location_id in self.location_id_to_location: return self.location_id_to_location[location_id] location = SQLLocation.by_location_id(location_id) self.location_id_to_location[location_id] = location return location
def get_case_location(case): """ If the owner of the case is a location, return it. Otherwise return the owner's primary location. If the case owner does not have a primary location, return None. """ case_owner = get_wrapped_owner(get_owner_id(case)) if isinstance(case_owner, SQLLocation): return case_owner location_id = case_owner.get_location_id(case.domain) return SQLLocation.by_location_id(location_id) if location_id else None
def sql_value(self): # all locations in self.value will have same ancestor, so just pick first one to query from corehq.apps.locations.models import SQLLocation location = SQLLocation.by_location_id(self.location_id) if location: ancestor = location.get_ancestor_of_type( self.ancestor_expression['location_type']) else: return None if ancestor: return {self.ancestor_expression['field']: ancestor.location_id} else: return None
def _get_user_location(user, domain): ''' Takes a couch_user and returns that users level in the heirarchy and the location name ''' # TODO: This needs to handle the top level where someone can see everything try: user_location_id = user.get_domain_membership(domain).location_id loc = SQLLocation.by_location_id(user_location_id) location_type_name = loc.location_type.name location_name = loc.name except Exception: location_type_name = 'state' location_name = '' return location_type_name, location_name
def _get_user_permissions(user, domain): ''' Takes a couch_user and returns that users level in the heirarchy and the location name ''' # TODO: This needs to handle the top level where someone can see everything try: user_location_id = user.get_domain_membership(domain).location_id loc = SQLLocation.by_location_id(user_location_id) location_type_name = loc.location_type.name location_name = loc.name except Exception as e: location_type_name = 'state' location_name = '' return location_type_name, location_name
def recipient(self): if self.recipient_type == 'CommCareCase': return CaseAccessors(self.domain).get_case(self.recipient_id) elif self.recipient_type == 'CommCareUser': return CommCareUser.get(self.recipient_id) elif self.recipient_type == 'WebUser': return WebUser.get(self.recipient_id) elif self.recipient_type == 'CommCareCaseGroup': return CommCareCaseGroup.get(self.recipient_id) elif self.recipient_type == 'Group': return Group.get(self.recipient_id) elif self.recipient_type == 'Location': return SQLLocation.by_location_id(self.recipient_id) else: raise UnknownRecipientType(self.recipient_type)
def recipient(self): if self.recipient_type == self.RECIPIENT_TYPE_CASE: return CaseAccessors(self.domain).get_case(self.recipient_id) elif self.recipient_type == self.RECIPIENT_TYPE_MOBILE_WORKER: return CommCareUser.get(self.recipient_id) elif self.recipient_type == self.RECIPIENT_TYPE_WEB_USER: return WebUser.get(self.recipient_id) elif self.recipient_type == self.RECIPIENT_TYPE_CASE_GROUP: return CommCareCaseGroup.get(self.recipient_id) elif self.recipient_type == self.RECIPIENT_TYPE_USER_GROUP: return Group.get(self.recipient_id) elif self.recipient_type == self.RECIPIENT_TYPE_LOCATION: return SQLLocation.by_location_id(self.recipient_id) else: raise UnknownRecipientType(self.recipient_type)
def parse_group_location_params(self, param_ids): locationids_param = [] groupids_param = [] if param_ids: for id in param_ids: if id.startswith("g__"): groupids_param.append(id[3:]) elif id.startswith("l__"): loc = SQLLocation.by_location_id(id[3:]) if loc.get_descendants(): locationids_param.extend(loc.get_descendants().location_ids()) locationids_param.append(id[3:]) return locationids_param, groupids_param
def parse_group_location_params(self, param_ids): locationids_param = [] groupids_param = [] if param_ids: for id in param_ids: if id.startswith("g__"): groupids_param.append(id[3:]) elif id.startswith("l__"): loc = SQLLocation.by_location_id(id[3:]) if loc.get_descendants(): locationids_param.extend( loc.get_descendants().location_ids()) locationids_param.append(id[3:]) return locationids_param, groupids_param
def get_location_details(self, location_id): result = defaultdict(dict) if not location_id: return result if location_id in self.location_details: return self.location_details[location_id] location = SQLLocation.by_location_id(location_id) if location: locs = location.get_ancestors(include_self=True).select_related('location_type') for loc in locs: result[loc.location_type.code] = { 'location_id': loc.location_id, 'name': loc.name, 'site_code': loc.site_code, } self.location_details[location_id] = result return result
def host_case_owner_location_from_case(domain, case): if not case: return None try: host = case.host except (CaseNotFound, ResourceNotFound): return None if not host: return None location_id = host.owner_id if not location_id: return None location = SQLLocation.by_location_id(location_id) if not location or location.is_archived or location.domain != domain: return None return location
if group.domain != self.domain: return None return group elif self.recipient_type == self.RECIPIENT_TYPE_USER_GROUP: try: group = Group.get(self.recipient_id) except ResourceNotFound: return None if group.domain != self.domain: return None return group elif self.recipient_type == self.RECIPIENT_TYPE_LOCATION: location = SQLLocation.by_location_id(self.recipient_id) if location is None: return None if location.domain != self.domain: return None return location else: raise UnknownRecipientType(self.recipient_type) @staticmethod def recipient_is_an_individual_contact(recipient): return ( isinstance(recipient, (CommCareUser, WebUser)) or
if not case: return None try: host = case.host except (CaseNotFound, ResourceNotFound): return None if not host: return None location_id = host.owner_id if not location_id: return None location = SQLLocation.by_location_id(location_id) if not location or location.is_archived or location.domain != reminder.domain: return None return [location] def host_case_owner_location_parent(handler, reminder): result = host_case_owner_location(handler, reminder) if not result: return None parent_location = result[0].parent if not parent_location: return None
def sql_location(self): from corehq.apps.locations.models import SQLLocation if self.location_id: return SQLLocation.by_location_id(self.location_id)
def load_restricted_locs(domain, selected_loc_id=None, user=None, show_test=False): """ Returns a dict of a set of locations based on location selected and accessible locations. 1. User with full organisation access: a) with no location selected: use root locations. b) with a location selected: along with root locations include the hierarchal path uptil the selected location from the root. For example : if loc2 is selected and hierarchy is loc1>loc2>loc3 then it will return [{ name:loc1, children: [{ name: loc2 }] }, { name: loc5 } ] 2. User with limited access(only locations under user access are included) a) with no location selected: By default selected location is user's primary location and rest is same as point 1.b b) with a location selected: Same as Point 1.b Basic logic is: Start with all root locations, put all children of location which is drilled(based on user permission) but drill only the location which lies in hierarchy of selected location. """ def loc_to_json(loc): return { 'name': loc.name, 'location_type': loc.location_type.name, # todo: remove when types aren't optional 'uuid': loc.location_id, 'is_archived': loc.is_archived, 'can_edit': True } def _get_ancestor_loc_dict(location_list, location_id): for parent_location in location_list: if parent_location['uuid'] == location_id: return parent_location return None def location_transform(locations): """ It returns a dict of locations mapped with their parents. :param locations: list of locations(list of SQLLocation objects) :return: { None: [loc1, loc2, loc3], # None signifies No parent. i.e locs are root locations loc1.id: [loc5, loc6, loc7], } """ loc_dict = defaultdict(list) for loc in locations: # do not include test locations if not show_test and loc.metadata.get('is_test_location', 'real') == 'test': continue parent_id = loc.parent_id loc_dict[parent_id].append(loc) return loc_dict accessible_location = SQLLocation.objects.accessible_to_user(domain, user) location_level = 0 lineage = [] if selected_loc_id: location = SQLLocation.by_location_id(selected_loc_id) location_level = LOCATION_TYPES.index(location.location_type.name) lineage = location.get_ancestors() all_ancestors = [loc.id for loc in lineage] # No need to pull the locations whose parents or they themselves do not present in the # hierarchy of the location selected. Also check for parent_id null in case root location # is selected accessible_location = accessible_location.filter(Q(parent_id__in=all_ancestors) | Q(parent_id__isnull=True)) # Only pull locations which are above or at level of location selected accessible_location = accessible_location.filter( location_type__name__in=LOCATION_TYPES[:location_level + 1]) parent_child_dict = location_transform(set(accessible_location).union(set(lineage))) locations_list = [loc_to_json(loc) for loc in parent_child_dict[None]] # if a location is selected, we need to pre-populate its location hierarchy # so that the data is available client-side to pre-populate the drop-downs if selected_loc_id: json_at_level = locations_list for loc in lineage: # Get the ancestor_dict out of all present at the level # which needs to be drilled down ancestor_loc_dict = _get_ancestor_loc_dict(json_at_level, loc.location_id) # could not find the ancestor at the level, # user should not have reached at this point to try and access an ancestor that is not permitted if ancestor_loc_dict is None: break children = parent_child_dict.get(loc.id, []) ancestor_loc_dict['children'] = [loc_to_json(loc) for loc in children] # reset level to one level down to find ancestor in next iteration json_at_level = ancestor_loc_dict['children'] return locations_list