def get_location_fixture_queryset(user): if toggles.SYNC_ALL_LOCATIONS.enabled(user.domain): return SQLLocation.active_objects.filter( domain=user.domain).prefetch_related('location_type') user_locations = user.get_sql_locations(user.domain) if user_locations.query.is_empty(): return user_locations user_location_ids = list(user_locations.order_by().values_list("id", flat=True)) if toggles.RELATED_LOCATIONS.enabled(user.domain): # Retrieve all of the locations related to a user's location and child # location and add them to the flat fixture related_location_ids = LocationRelation.from_locations( SQLLocation.objects.get_descendants( Q(domain=user.domain, id__in=user_location_ids))) user_location_ids.extend( list( SQLLocation.objects.filter( location_id__in=related_location_ids).values_list( 'id', flat=True))) return _location_queryset_helper(user.domain, user_location_ids)
def __call__(self, restore_state): if not toggles.RELATED_LOCATIONS.enabled(restore_state.domain): return [] restore_user = restore_state.restore_user user_locations = restore_user.get_sql_locations(restore_user.domain) related_location_ids = LocationRelation.from_locations(user_locations) related_location_pks = ( SQLLocation.objects.filter(location_id__in=related_location_ids) .values_list('pk', flat=True) ) locations_queryset = _location_queryset_helper(restore_user.domain, list(related_location_pks)) data_fields = _get_location_data_fields(restore_user.domain) return self.serializer.get_xml_nodes(self.id, restore_user, locations_queryset, data_fields)
def get_location_fixture_queryset(user): if toggles.SYNC_ALL_LOCATIONS.enabled(user.domain): return SQLLocation.active_objects.filter(domain=user.domain).prefetch_related('location_type') user_locations = user.get_sql_locations(user.domain) if user_locations.query.is_empty(): return user_locations user_location_ids = list(user_locations.order_by().values_list("id", flat=True)) if toggles.RELATED_LOCATIONS.enabled(user.domain): # Retrieve all of the locations related to a user's location and child # location and add them to the flat fixture related_location_ids = LocationRelation.from_locations( SQLLocation.objects.get_descendants(Q(domain=user.domain, id__in=user_location_ids)) ) user_location_ids.extend( list(SQLLocation.objects.filter(location_id__in=related_location_ids).values_list('id', flat=True)) ) return _location_queryset_helper(user.domain, user_location_ids)
def get_xml_nodes(self, fixture_id, restore_user, locations_queryset, data_fields): all_types = LocationType.objects.filter(domain=restore_user.domain).values_list( 'code', flat=True ) location_type_attrs = ['{}_id'.format(t) for t in all_types if t is not None] attrs_to_index = ['@{}'.format(attr) for attr in location_type_attrs] attrs_to_index.extend(_get_indexed_field_name(field.slug) for field in data_fields if field.index_in_fixture) attrs_to_index.extend(['@id', '@type', 'name', '@distance']) xml_nodes = self._get_fixture_node(fixture_id, restore_user, locations_queryset, location_type_attrs, data_fields) user_locations = restore_user.get_sql_locations(restore_user.domain) distance_dict = LocationRelation.relation_distance_dictionary(user_locations) child_node = xml_nodes.getchildren()[0] for grandchild_node in child_node.getchildren(): location_id = grandchild_node.get('id') if location_id in distance_dict: minimum_distance = min(six.itervalues(distance_dict[location_id])) grandchild_node.set('distance', str(minimum_distance)) return [get_index_schema_node(fixture_id, attrs_to_index), xml_nodes]