def retrieve(self, request, region, agency, pk): try: stop = Stop.objects.select_related('agency') \ .get(pk=Stop.create_id(Agency.create_id(region, agency), pk)) except Stop.DoesNotExist: raise Http404('No Route matches the given query.') agency = stop.agency arrivals = get_provider(stop.agency).arrivals(stop) # odd if we have a mixture of units, but we currently don't so... # sort the arrivals arrivals.sort(key=attrgetter('away')) # never return more than 15 arrivals, limit before the prefetch to # avoid extra work arrivals = arrivals[:15] prefetch_related_objects(arrivals, ['direction__route__directions']) routes = {} for arrival in arrivals: if arrival.direction: route = arrival.direction.route routes[route.id] = route self.object = StopAdapter(stop, arrivals=arrivals, routes=routes.values()) serializer = self.get_serializer(self.object) return Response(serializer.data)
def retrieve(self, request, region=None, agency=None, *args, **kwargs): try: lat = float(request.GET['lat']) lon = float(request.GET['lon']) radius = float(request.GET.get('radius', 500.0)) if radius < 0 or 5000 < radius: # TODO: bad request, invalid param raise Exception('invalid radius') except KeyError: # TODO: bad request, missing param raise except ValueError: # TODO: bad request, bad param raise # we'll at most return N stops # TODO: limiting before filtering is bad stops = Stop.objects.nearby(lat, lon, radius)[:20] if region and agency: agency_id = Agency.create_id(region, agency) def desired_agency(stop): return stop.agency_id == agency_id stops = filter(desired_agency, stops) prefetch_related_objects( stops, ['agency', 'agency__region', 'directions', 'directions__route']) routes = {} agencies = {} regions = {} for stop in stops: agency = stop.agency agencies[agency.id] = agency region = agency.region regions[region.id] = region for direction in stop.directions.all(): route = direction.route routes[route.id] = direction.route self.object = Adapter(self, agencies=agencies.values(), regions=regions.values(), routes=routes.values(), stops=stops) serializer = self.get_serializer(self.object) return Response(serializer.data)
def retrieve(self, request, region, agency, route, pk): try: stop = Stop.objects.select_related('agency', 'agency__region') \ .get(pk=Stop.create_id(Agency.create_id(region, agency), pk)) except Stop.DoesNotExist: raise Http404('No Route matches the given query.') agency = stop.agency route = get_object_or_404(Route, pk=Route.create_id(agency.id, route)) arrivals = get_provider(stop.agency).arrivals(stop, route) self.object = StopAdapter(stop, arrivals=arrivals, route=route) serializer = self.get_serializer(self.object) return Response(serializer.data)
def retrieve(self, request, region, agency, pk): try: route = Route.objects.select_related('agency', 'agency__region') \ .prefetch_related('directions', 'directions__stops') \ .get(pk=Route.create_id(Agency.create_id(region, agency), pk)) except Route.DoesNotExist: raise Http404('No Route matches the given query.') stops = [] for direction in route.directions.all(): stops.extend(direction.stops.all()) self.object = Adapter(route, stops=stops) serializer = self.get_serializer(self.object) return Response(serializer.data)
def retrieve(self, request, region=None, agency=None, *args, **kwargs): try: lat = float(request.GET['lat']) lon = float(request.GET['lon']) radius = float(request.GET.get('radius', 500.0)) if radius < 0 or 5000 < radius: # TODO: bad request, invalid param raise Exception('invalid radius') except KeyError: # TODO: bad request, missing param raise except ValueError: # TODO: bad request, bad param raise # we'll at most return N stops # TODO: limiting before filtering is bad stops = Stop.objects.nearby(lat, lon, radius)[:20] if region and agency: agency_id = Agency.create_id(region, agency) def desired_agency(stop): return stop.agency_id == agency_id stops = filter(desired_agency, stops) prefetch_related_objects(stops, ['agency', 'agency__region', 'directions', 'directions__route']) routes = {} agencies = {} regions = {} for stop in stops: agency = stop.agency agencies[agency.id] = agency region = agency.region regions[region.id] = region for direction in stop.directions.all(): route = direction.route routes[route.id] = direction.route self.object = Adapter(self, agencies=agencies.values(), regions=regions.values(), routes=routes.values(), stops=stops) serializer = self.get_serializer(self.object) return Response(serializer.data)
def retrieve(self, request, region, pk): self.object = get_object_or_404(Agency, pk=Agency.create_id(region, pk)) serializer = self.get_serializer(self.object) return Response(serializer.data)