def get(self, request, **kwargs): if 'geo_field' not in kwargs: # If it's not a geo request, let ModelDetailView handle it. return super(ModelGeoDetailView, self).get(request, **kwargs) field = kwargs.pop('geo_field') if field not in self.allowed_geo_fields: raise Http404 try: obj = self.get_object(request, self.base_qs.only(field, self.name_field), **kwargs) except ObjectDoesNotExist: raise Http404 geom = getattr(obj, field) name = getattr(obj, self.name_field) format = request.GET.get('format', 'json') if format in ('json', 'apibrowser'): return RawJSONResponse(geom.geojson) elif format == 'wkt': return HttpResponse(geom.wkt, content_type="text/plain") elif format == 'kml': resp = HttpResponse( kml.generate_kml_document([kml.generate_placemark(name, geom)]), content_type="application/vnd.google-earth.kml+xml") resp['Content-Disposition'] = 'attachment; filename="shape.kml"' return resp else: raise NotImplementedError
def get(self, request, **kwargs): if 'geo_field' not in kwargs: # If it's not a geo request, let ModelListView handle it. return super(ModelGeoListView, self).get(request, **kwargs) field = kwargs.pop('geo_field') if field not in self.allowed_geo_fields: raise Http404 qs = self.get_qs(request, **kwargs) try: qs = self.filter(request, qs) except ValueError: raise BadRequest(_("Invalid filter value")) if qs.count() > app_settings.MAX_GEO_LIST_RESULTS: return HttpResponseForbidden( _("Spatial-list queries cannot return more than %(expected)d resources; this query would return %(actual)s. Please filter your query." ) % { 'expected': app_settings.MAX_GEO_LIST_RESULTS, 'actual': qs.count() }) format = request.GET.get('format', 'json') if format in ('json', 'apibrowser'): strings = ['{"objects": ['] strings.append(','.join( ('{"name": "%s", "%s": %s}' % (escapejs(x[1]), field, x[0].geojson) for x in qs.values_list(field, self.name_field)))) strings.append(']}') return RawJSONResponse(''.join(strings)) elif format == 'wkt': return HttpResponse("\n".join( (geom.wkt for geom in qs.values_list(field, flat=True))), content_type="text/plain") elif format == 'kml': placemarks = [ kml.generate_placemark(x[1], x[0]) for x in qs.values_list(field, self.name_field) ] resp = HttpResponse( kml.generate_kml_document(placemarks), content_type="application/vnd.google-earth.kml+xml") resp['Content-Disposition'] = 'attachment; filename="shape.kml"' return resp else: raise NotImplementedError
def get(self, request, **kwargs): if 'geo_field' not in kwargs: # If it's not a geo request, let ModelListView handle it. return super(ModelGeoListView, self).get(request, **kwargs) field = kwargs.pop('geo_field') if field not in self.allowed_geo_fields: raise Http404 qs = self.get_qs(request, **kwargs) try: qs = self.filter(request, qs) except ValueError: raise BadRequest(_("Invalid filter value")) if qs.count() > app_settings.MAX_GEO_LIST_RESULTS: return HttpResponseForbidden( _("Spatial-list queries cannot return more than %(expected)d resources; this query would return %(actual)s. Please filter your query.") % {'expected': app_settings.MAX_GEO_LIST_RESULTS, 'actual': qs.count()}) format = request.GET.get('format', 'json') if format in ('json', 'apibrowser'): strings = ['{"objects": ['] strings.append(','.join(('{"name": "%s", "%s": %s}' % (escapejs(x[1]), field, x[0].geojson) for x in qs.values_list(field, self.name_field)))) strings.append(']}') return RawJSONResponse(''.join(strings)) elif format == 'wkt': return HttpResponse("\n".join((geom.wkt for geom in qs.values_list(field, flat=True))), content_type="text/plain") elif format == 'kml': placemarks = [kml.generate_placemark(x[1], x[0]) for x in qs.values_list(field, self.name_field)] resp = HttpResponse( kml.generate_kml_document(placemarks), content_type="application/vnd.google-earth.kml+xml") resp['Content-Disposition'] = 'attachment; filename="shape.kml"' return resp else: raise NotImplementedError