def geolocate_post(request): data = request.validated session = request.db_slave_session result = None api_key = request.GET.get('key', None) if api_key is not None: if data['wifiAccessPoints']: result = search_wifi_ap(session, data) else: result = search_cell_tower(session, data) if result is None: result = HTTPNotFound() result.content_type = 'application/json' result.body = NOT_FOUND return result return { "location": { "lat": result['lat'], "lng": result['lon'], }, "accuracy": float(result['accuracy']), }
def geolocate_view(request): data, errors = preprocess_request( request, schema=GeoLocateSchema(), response=JSONParseError, accept_empty=True, ) data = map_data(data) session = request.db_slave_session result = search_all_sources(session, 'geolocate', data, client_addr=request.client_addr, geoip_db=request.registry.geoip_db, api_key_log=getattr(request, 'api_key_log', False), api_key_name=getattr(request, 'api_key_name', None)) if not result: result = HTTPNotFound() result.content_type = 'application/json' result.body = NOT_FOUND return result return { "location": { "lat": result['lat'], "lng": result['lon'], }, "accuracy": float(result['accuracy']), }
def geolocate_view(request): data, errors = preprocess_request( request, schema=GeoLocateSchema(), extra_checks=(geolocate_validator, ), response=JSONParseError, accept_empty=True, ) data = map_data(data) session = request.db_slave_session result = search_all_sources( session, 'geolocate', data, client_addr=request.client_addr, geoip_db=request.registry.geoip_db, api_key_log=getattr(request, 'api_key_log', False), api_key_name=getattr(request, 'api_key_name', None)) if not result: result = HTTPNotFound() result.content_type = 'application/json' result.body = NOT_FOUND return result return { "location": { "lat": result['lat'], "lng": result['lon'], }, "accuracy": float(result['accuracy']), }
def process_single(request): stats_client = get_stats_client() locate_data, locate_errors = preprocess_request( request, schema=GeoLocateSchema(), extra_checks=(geolocate_validator, ), response=JSONParseError, accept_empty=True, ) data, errors = preprocess_request( request, schema=GeoSubmitSchema(), extra_checks=(geosubmit_validator, ), response=None, ) data = {'items': [data]} nickname = request.headers.get('X-Nickname', u'') email = request.headers.get('X-Email', u'') upload_items = flatten_items(data) errors = process_upload(nickname, email, upload_items) if errors is not SENTINEL and errors: # pragma: no cover stats_client.incr('geosubmit.upload.errors', len(errors)) first_item = data['items'][0] if first_item['latitude'] == -255 or first_item['longitude'] == -255: data = map_data(data['items'][0]) session = request.db_slave_session result = search_all_sources( session, 'geosubmit', data, client_addr=request.client_addr, geoip_db=request.registry.geoip_db, api_key_log=getattr(request, 'api_key_log', False), api_key_name=getattr(request, 'api_key_name', None)) else: result = { 'lat': first_item['latitude'], 'lon': first_item['longitude'], 'accuracy': first_item['accuracy'] } if result is None: stats_client.incr('geosubmit.miss') result = HTTPNotFound() result.content_type = 'application/json' result.body = NOT_FOUND return result return { "location": { "lat": result['lat'], "lng": result['lon'], }, "accuracy": float(result['accuracy']), }
def JsonHTTPNotFound(message=None): response = HTTPNotFound() response.content_type = 'application/json' if message: if isinstance(message, dict): msg = json.dumps(message) response.text = unicode(msg) return response
def __call__(self, environ, start_response): not_found = HTTPNotFound() accept = environ.get('HTTP_ACCEPT', '') if accept and 'json' in accept: not_found.body = format_error_response_to_json(not_found) not_found.content_type = 'application/json' return not_found(environ, start_response)
def geolocate_view(request): heka_client = get_heka_client() data, errors = preprocess_request( request, schema=GeoLocateSchema(), extra_checks=(geolocate_validator, ), response=JSONError, accept_empty=True, ) session = request.db_slave_session result = None if data and data['wifiAccessPoints']: result = search_wifi_ap(session, data) if result is not None: heka_client.incr('geolocate.wifi_hit') heka_client.timer_send('geolocate.accuracy.wifi', result['accuracy']) elif data: result = search_cell_tower(session, data) if result is not None: heka_client.incr('geolocate.cell_hit') heka_client.timer_send('geolocate.accuracy.cell', result['accuracy']) if result is None: result = search_cell_tower_lac(session, data) if result is not None: heka_client.incr('geolocate.cell_lac_hit') heka_client.timer_send('geolocate.accuracy.cell_lac', result['accuracy']) if result is None and request.client_addr: result = search_geoip(request.registry.geoip_db, request.client_addr) if result is not None: heka_client.incr('geolocate.geoip_hit') heka_client.timer_send('geolocate.accuracy.geoip', result['accuracy']) if result is None: heka_client.incr('geolocate.miss') result = HTTPNotFound() result.content_type = 'application/json' result.body = NOT_FOUND return result return { "location": { "lat": result['lat'], "lng": result['lon'], }, "accuracy": float(result['accuracy']), }
def process_single(request): stats_client = get_stats_client() locate_data, locate_errors = preprocess_request( request, schema=GeoLocateSchema(), extra_checks=(geolocate_validator, ), response=JSONParseError, accept_empty=True, ) data, errors = preprocess_request( request, schema=GeoSubmitSchema(), extra_checks=(geosubmit_validator,), response=None, ) data = {'items': [data]} nickname = request.headers.get('X-Nickname', u'') email = request.headers.get('X-Email', u'') upload_items = flatten_items(data) errors = process_upload(nickname, email, upload_items) if errors is not SENTINEL and errors: # pragma: no cover stats_client.incr('geosubmit.upload.errors', len(errors)) first_item = data['items'][0] if first_item['latitude'] == -255 or first_item['longitude'] == -255: data = map_data(data['items'][0]) session = request.db_slave_session result = search_all_sources( session, 'geosubmit', data, client_addr=request.client_addr, geoip_db=request.registry.geoip_db, api_key_log=getattr(request, 'api_key_log', False), api_key_name=getattr(request, 'api_key_name', None)) else: result = {'lat': first_item['latitude'], 'lon': first_item['longitude'], 'accuracy': first_item['accuracy']} if result is None: stats_client.incr('geosubmit.miss') result = HTTPNotFound() result.content_type = 'application/json' result.body = NOT_FOUND return result return { "location": { "lat": result['lat'], "lng": result['lon'], }, "accuracy": float(result['accuracy']), }
def geolocate_view(request): heka_client = get_heka_client() data, errors = preprocess_request( request, schema=GeoLocateSchema(), extra_checks=(geolocate_validator, ), response=JSONError, ) session = request.db_slave_session result = None if data['wifiAccessPoints']: result = search_wifi_ap(session, data) if result is not None: heka_client.incr('geolocate.wifi_hit') else: result = search_cell_tower(session, data) if result is not None: heka_client.incr('geolocate.cell_hit') if result is None: result = search_cell_tower_lac(session, data) if result is not None: heka_client.incr('geolocate.cell_lac_hit') if result is None and request.client_addr: result = search_geoip(request.registry.geoip_db, request.client_addr) if result is not None: heka_client.incr('geolocate.geoip_hit') if result is None: heka_client.incr('geolocate.miss') result = HTTPNotFound() result.content_type = 'application/json' result.body = NOT_FOUND return result return { "location": { "lat": result['lat'], "lng": result['lon'], }, "accuracy": float(result['accuracy']), }
def geolocate_post(request): data = request.validated session = request.db_slave_session result = None if data['wifiAccessPoints']: result = search_wifi_ap(session, data) else: result = search_cell_tower(session, data) if result is None: result = HTTPNotFound() result.content_type = 'application/json' result.body = NOT_FOUND return result return { "location": { "lat": result['lat'], "lng": result['lon'], }, "accuracy": float(result['accuracy']), }
def not_found(self): result = HTTPNotFound() result.content_type = 'application/json' result.body = NOT_FOUND return result