def bus_service_search(self, request, query, is_single_app_search): routes = Route.objects.filter(service_id__iexact=query) stops = Entity.objects.filter(stoponroute__route__in=routes) location = get_point(request, None) if location: stops = stops.distance(location).order_by('distance') for stop in stops: result = { 'url': stop.get_absolute_url(), 'application': self.conf.local_name, 'redirect_if_sole_result': True, } result.update(EntityDetailView(self.conf).get_metadata(request, stop.identifier_scheme, stop.identifier_value)) yield result
def handle_GET(self, request, context, entity=None): point = get_point(request, entity) if point is None: return self.render(request, { 'entity': entity }, 'places/entity_without_location') if entity: return_url = reverse('places:entity-nearby-list', args=[entity.primary_type.slug, entity.slug]) else: return_url = reverse('places:nearby-list') # Get entity types to show on nearby page entity_types = EntityType.objects.filter(show_in_nearby_list=True) for et in entity_types: # For each et, get the entities that belong to it et.max_distance = 0 et.entities_found = 0 es = et.entities_completion.filter(location__isnull=False, location__distance_lt=(point, D(km=5))).distance(point).order_by('distance') for e in es: # Selection criteria for whether or not to count this entity if (e.distance.m ** 0.75) * (et.entities_found + 1) > 500: break et.max_distance = e.distance.m et.entities_found += 1 categorised_entity_types = defaultdict(list) for et in filter(lambda et: et.entities_found > 0, entity_types): categorised_entity_types[_(et.category.name)].append(et) # Need to do this other Django evalutes .items as ['items'] categorised_entity_types = dict(categorised_entity_types.items()) context.update({ 'entity_types': categorised_entity_types, 'entity': entity, 'return_url': return_url, # entity is None => we've searched around the user's location 'exposes_user_data': entity is None, }) if entity and not entity.location: return self.render(request, context, 'places/entity_without_location') return self.render(request, context, 'places/nearby_list')
def initial_context(self, request, ptypes, entity=None): point = get_point(request, entity) entity_types = tuple(get_object_or_404(EntityType, slug=t) for t in ptypes.split(';')) if point: entities = Entity.objects.filter(location__isnull=False, is_sublocation=False) for et in entity_types: entities = entities.filter(all_types_completion=et) entities = entities.distance(point).order_by('distance')[:99] else: entities = [] context = super(NearbyDetailView, self).initial_context(request, ptypes, entities) context.update({ 'entity_types': entity_types, 'point': point, 'entities': entities, 'entity': entity, # entity is None => point is the user's location 'exposes_user_data': entity is None, }) return context