Exemplo n.º 1
0
    def get(self, request, *args, **kwargs):
        geo_levels = request.GET.get('geolevels', None)
        geo_version = request.GET.get('geo_version', None)
        if geo_levels:
            geo_levels = [lev.strip() for lev in geo_levels.split(',')]
            geo_levels = [lev for lev in geo_levels if lev]

        if 'q' in request.GET:
            search_term = request.GET['q']
            places = geo_data.get_locations(search_term, geo_levels, geo_version)
            return render_json_to_response(
                {'results': [p.as_dict() for p in places]}
            )

        elif 'coords' in request.GET and ',' in request.GET['coords']:
            lat, lon = self.request.GET['coords'].split(',', 1)
            try:
                lat = float(lat)
                lon = float(lon)
            except ValueError as e:
                return HttpResponseBadRequest('bad parameter: %s' % e.message)

            places = geo_data.get_locations_from_coords(latitude=lat, longitude=lon, levels=geo_levels, version=geo_version)
            return render_json_to_response({'results': [p.as_dict() for p in places]})

        else:
            return HttpResponseBadRequest('"q" or "coords" parameter is required')
Exemplo n.º 2
0
    def get(self, request, *args, **kwargs):
        geo_levels = request.GET.get('geolevels', None)
        if geo_levels:
            geo_levels = [lev.strip() for lev in geo_levels.split(',')]
            geo_levels = [lev for lev in geo_levels if lev]

        if 'q' in request.GET:
            search_term = request.GET['q']
            places = geo_data.get_locations(search_term, geo_levels)
            return render_json_to_response(
                {'results': [p.as_dict() for p in places]})

        elif 'coords' in request.GET and ',' in request.GET['coords']:
            lat, lon = self.request.GET['coords'].split(',', 1)
            try:
                lat = float(lat)
                lon = float(lon)
            except ValueError as e:
                return HttpResponseBadRequest('bad parameter: %s' % e.message)

            places = geo_data.get_locations_from_coords(latitude=lat,
                                                        longitude=lon,
                                                        levels=geo_levels)
            return render_json_to_response(
                {'results': [p.as_dict() for p in places]})

        else:
            return HttpResponseBadRequest(
                '"q" or "coords" parameter is required')
Exemplo n.º 3
0
    def get(self, request, geo_id, *args, **kwargs):
        try:
            level, code = geo_id.split('-', 1)
            geo = geo_data.get_geography(code, level)
        except (ValueError, LocationNotFound):
            raise Http404

        parents = [g.as_dict() for g in geo.ancestors()]
        return render_json_to_response(parents)
Exemplo n.º 4
0
    def get(self, request, geo_id, *args, **kwargs):
        try:
            level, code = geo_id.split('-', 1)
            geo = geo_data.get_geography(code, level)
        except (ValueError, LocationNotFound):
            raise Http404

        parents = [g.as_dict() for g in geo.ancestors()]
        return render_json_to_response(parents)
Exemplo n.º 5
0
    def show(self, request):
        dataset = ', '.join(sorted(list(set(t.dataset_name for t in self.tables))))
        years = ', '.join(sorted(list(set(t.year for t in self.tables))))

        data = self.get_data(self.data_geos, self.tables)

        return render_json_to_response({
            'release': {
                'name': dataset,
                'years': years,
            },
            'tables': dict((t.id.upper(), t.as_dict()) for t in self.tables),
            'data': data,
            'geography': dict((g.geoid, g.as_dict()) for g in chain(self.data_geos, self.info_geos)),
        })
Exemplo n.º 6
0
    def show(self, request):
        dataset = ', '.join(
            sorted(list(set(t.dataset_name for t in self.tables))))
        years = ', '.join(sorted(list(set(t.year for t in self.tables))))

        data = self.get_data(self.data_geos, self.tables)

        return render_json_to_response({
            'release': {
                'name': dataset,
                'years': years,
            },
            'tables':
            dict((t.id.upper(), t.as_dict()) for t in self.tables),
            'data':
            data,
            'geography':
            dict((g.geoid, g.as_dict())
                 for g in chain(self.data_geos, self.info_geos)),
        })
Exemplo n.º 7
0
    def show(self, request):
        data = self.get_data(self.data_geos, self.tables)

        tables = {}
        for table in self.tables:
            d = table.as_dict()
            d['columns'] = table.columns()
            tables[table.name.upper()] = d

        return render_json_to_response({
            'release':
            self.release.as_dict(),
            'other_releases':
            self.available_releases['other'],
            'tables':
            tables,
            'data':
            data,
            'geography':
            dict((g.geoid, g.as_dict())
                 for g in chain(self.data_geos, self.info_geos)),
        })
Exemplo n.º 8
0
 def get(self, request, *args, **kwargs):
     return render_json_to_response(
         [t.as_dict(columns=False) for t in DATA_TABLES.itervalues()])
Exemplo n.º 9
0
 def get(self, request, *args, **kwargs):
     return render_json_to_response([
         t.as_dict()
         for t in chain(SimpleTable.objects.all(), FieldTable.objects.all())
     ])
Exemplo n.º 10
0
 def get(self, request, *args, **kwargs):
     return render_json_to_response([t.as_dict(columns=False) for t in DATA_TABLES.itervalues()])