def test_increment_api(self): from django.conf import settings settings.REDIS_ANALYTICS_DATABASE = 15 from analytics.shortcuts import (increment_api_locations, increment_api_queries) increment_api_queries("Search Query", account=self.account) increment_api_queries("Search Query") increment_api_locations("Edinburgh", account=self.account) increment_api_locations("Edinburgh")
def resource_search(request): def _resource_result(r): result = { 'id': r['res_id'], 'title': r['title'], 'description': r.get('short_description', ''), # 'resource_type': r[''] resource_type or '', 'uri': r.get('uri', ''), 'locations': r.get('pt_location', []), 'locationnames': r.get('loc_labels', []), # u'loc_labels': [u'EH17 8QG, Liberton/Gilmerton, of Edinburgh'], u'pt_location': [u'55.9062925785, -3.13446285433'] 'tags': r.get('keywords', ''), 'accounts': r.get('accounts', ''), 'score': r['score'] # 'last_modified': r[''] .item_metadata.last_modified, } if r.get('event_start'): result['event_start'] = r.get('event_start') if r.get('event_end'): result['event_end'] = r.get('event_end') return result location = request.REQUEST.get('location', '') accounts = request.REQUEST.get('accounts', '') collections = request.REQUEST.get('collections', '') if collections: accounts = '' event = request.REQUEST.get('event', None) query = request.REQUEST.get('query') max = request.REQUEST.get('max', unicode(settings.SOLR_ROWS)) start = request.REQUEST.get('start', 0) output = request.REQUEST.get('output', 'json') boost_location = request.REQUEST.get('boostlocation', (settings.SOLR_LOC_BOOST_DEFAULT)) callback = request.REQUEST.get('callback') increment_api_queries(query) increment_api_locations(location) result_code = 200 errors = [] # if not query: # result_code = 10 # errors.append('Param \'query\' must be valid search query') if not _check_int(max) or int(max) > settings.SOLR_ROWS: result_code = 10 errors.append('Param \'max\' must be positive integer maximum value of %s. You sent %s' % (settings.SOLR_ROWS, max)) if not _check_int(start) or int(start) < 0: result_code = 10 errors.append('Param \'start\' must be positive integer. You sent %s' % start) if not _check_int(boost_location) or int(boost_location) > int(settings.SOLR_LOC_BOOST_MAX): result_code = 10 errors.append('Param \'boostlocation\' must be an integer number between 0 and %s. You sent %s' % (int(settings.SOLR_LOC_BOOST_MAX), boost_location)) if event and event != '*': result_code = 10 errors.append('Param \'event\' must be * if present.') if not errors: loc, resources = find_by_place_or_kwords( location, query, boost_location, start=start, max=int(max), accounts=accounts.split(), collections=collections.split(), event=event) if location and not loc: result_code = 10 errors.append('Location \'%s\' not found.' % location) if errors: return JsonResponse(errors=[{ 'code': result_code, 'message': '. '.join(errors)}], callback=callback) else: results = [_resource_result(r) for r in resources] data = [ { 'query': query, 'max': max, 'start': start, 'numfound': resources.hits, 'output': output, 'location': _loc_to_str(loc['lat_lon']), 'event': event, 'boostlocation': boost_location, 'accounts': accounts, 'collections': collections, 'results': results } ] return JsonResponse(data=data, callback=callback)
def resource_search(request): def _resource_result(r): result = { 'id': r['res_id'], 'title': r['title'], 'description': r.get('short_description', ''), # 'resource_type': r[''] resource_type or '', 'uri': r.get('uri', ''), 'locations': r.get('pt_location', []), 'locationnames': r.get('loc_labels', []), # u'loc_labels': [u'EH17 8QG, Liberton/Gilmerton, of Edinburgh'], u'pt_location': [u'55.9062925785, -3.13446285433'] 'tags': r.get('keywords', ''), 'accounts': r.get('accounts', ''), 'score': r['score'] # 'last_modified': r[''] .item_metadata.last_modified, } if r.get('event_start'): result['event_start'] = r.get('event_start') if r.get('event_end'): result['event_end'] = r.get('event_end') return result location = request.REQUEST.get('location', '') accounts = request.REQUEST.get('accounts', '') collections = request.REQUEST.get('collections', '') if collections: accounts = '' event = request.REQUEST.get('event', None) query = request.REQUEST.get('query') max = request.REQUEST.get('max', unicode(settings.SOLR_ROWS)) start = request.REQUEST.get('start', 0) output = request.REQUEST.get('output', 'json') boost_location = request.REQUEST.get('boostlocation', (settings.SOLR_LOC_BOOST_DEFAULT)) callback = request.REQUEST.get('callback') increment_api_queries(query) increment_api_locations(location) result_code = 200 errors = [] # if not query: # result_code = 10 # errors.append('Param \'query\' must be valid search query') if not _check_int(max) or int(max) > settings.SOLR_ROWS: result_code = 10 errors.append('Param \'max\' must be positive integer maximum value of %s. You sent %s' % (settings.SOLR_ROWS, max)) if not _check_int(start) or int(start) < 0: result_code = 10 errors.append('Param \'start\' must be positive integer. You sent %s' % start) if not _check_int(boost_location) or int(boost_location) > int(settings.SOLR_LOC_BOOST_MAX): result_code = 10 errors.append('Param \'boostlocation\' must be an integer number between 0 and %s. You sent %s' % (int(settings.SOLR_LOC_BOOST_MAX), boost_location)) if event and event != '*': result_code = 10 errors.append('Param \'event\' must be * if present.') if not errors: loc, resources = find_by_place_or_kwords( location, query, boost_location, start=start, max=int(max), accounts=accounts.split(), collections=collections.split(), event=event) if location and not loc: result_code = 10 errors.append('Location \'%s\' not found.' % location) if errors: return JsonResponse(errors=[{ 'code': result_code, 'message': '. '.join(errors)}], callback=callback) else: results = [_resource_result(r) for r in resources] data = [ { 'query': query, 'max': max, 'start': start, 'numfound': resources.hits, 'output': output, 'location': _loc_to_str(loc['lat_lon']) if loc else '', 'event': event, 'boostlocation': boost_location, 'accounts': accounts, 'collections': collections, 'results': results } ] return JsonResponse(data=data, callback=callback)