def validate(request): """ POST /validate Validate GeoJSON data in POST body """ testing = request.GET.get('testing') try: test_geojson = json.loads(request.raw_post_data) if not isinstance(test_geojson, dict): return _geojson_error('POSTed data was not a JSON object.', testing) except: return _geojson_error('POSTed data was not JSON serializeable.', testing) if not 'type' in test_geojson: return _geojson_error('The "type" member is requried and was not found.', testing) try: validate_geojson(test_geojson) except GeoJSONValidationException as e: return _geojson_error(str(e), testing) # Everything checked out. Return 'ok'. if not testing: track_validate() resp = { 'status': 'ok', } return HttpResponse(json.dumps(resp), mimetype='application/json')
def _geojson_error(message): track_validate(valid=False) resp = { 'status': 'error', 'message': message, } return HttpResponse(json.dumps(resp), mimetype='application/json')
def _geojson_error(message, testing=False, status=200): track_validate(valid=False) resp = { 'status': 'error', 'message': message, } return HttpResponse(json.dumps(resp), mimetype='application/json', status=status)
def validate(request): """ POST /validate Validate GeoJSON data in POST body """ testing = request.GET.get('testing') if request.method == 'POST': stringy_json = request.raw_post_data else: # GET try: remote_url = request.GET['url'] stringy_json = get_remote_json(remote_url) except KeyError: # The "url" URL parameter was missing return _geojson_error( 'When validating via GET, a "url" URL parameter is required.', status=400) except NonFetchableURLException: return _geojson_error('The URL passed could not be fetched.') try: test_geojson = json.loads(stringy_json) if not isinstance(test_geojson, dict): return _geojson_error('Data was not a JSON object.', testing) except: return _geojson_error('Data was not JSON serializeable.', testing) if not 'type' in test_geojson: return _geojson_error( 'The "type" member is required and was not found.', testing) try: validate_geojson(test_geojson) except GeoJSONValidationException as e: return _geojson_error(str(e), testing) # Everything checked out. Return 'ok'. track_validate() resp = { 'status': 'ok', } return HttpResponse(json.dumps(resp), mimetype='application/json')
def validate(request): """ POST /validate Validate GeoJSON data in POST body """ try: test_geojson = json.loads(request.raw_post_data) if not isinstance(test_geojson, dict): return _geojson_error('POSTed data was not a JSON object.') except: return _geojson_error('POSTed data was not JSON serializeable.') if not 'type' in test_geojson: return _geojson_error('The "type" member is requried and was not found.') geojson_types = { 'Point': point, 'MultiPoint': multipoint, 'LineString': linestring, 'MultiLineString': multilinestring, 'Polygon': polygon, 'MultiPolygon': multipolygon, 'GeometryCollection': geometrycollection, 'Feature': feature, 'FeatureCollection': featurecollection, } if not test_geojson['type'] in geojson_types: return _geojson_error('"%s" is not a valid GeoJSON type.' % test_geojson['type']) try: validictory.validate(test_geojson, geojson_types[test_geojson['type']]) except validictory.validator.ValidationError as error: return _geojson_error(str(error)) # Everything checked out. Return 'ok'. track_validate() resp = { 'status': 'ok', } return HttpResponse(json.dumps(resp), mimetype='application/json')