def test_postcode(self): from resources.search import find_by_place_or_kwords postcode = "EH15 1AR" # [55.953899999999997, -3.1164000000000001] loc, results = find_by_place_or_kwords(postcode, "") result = iter(results).next() self.assertEqual(result["title"], "title 0") loc, results = find_by_place_or_kwords(postcode, "green") result = iter(results).next() self.assertEqual(result["title"], "title 1") loc, results = find_by_place_or_kwords("", "pink") self.assertEqual(loc, None) result = iter(results).next() self.assertEqual(result["title"], "title 8")
def test_postcode(self): from resources.search import find_by_place_or_kwords postcode = 'EH15 1AR' # [55.953899999999997, -3.1164000000000001] loc, results = find_by_place_or_kwords(postcode, '') result = iter(results).next() self.assertEqual(result['title'], 'title 0') loc, results = find_by_place_or_kwords(postcode, 'green') result = iter(results).next() self.assertEqual(result['title'], 'title 1') loc, results = find_by_place_or_kwords('', 'pink') self.assertEqual(loc, None) result = iter(results).next() self.assertEqual(result['title'], 'title 8')
def test_curation(self): from resources.search import find_by_place_or_kwords # setting resource failed - no idea why, seems a valid object # reloading works self.resource6 = Resource.objects.get(id=self.resource6.id) curation = Curation(outcome="", tags=["blah"], note="bob curated this", owner=self.bob) curation.item_metadata.update(author=self.bob) add_curation(self.resource6, curation) loc, results = find_by_place_or_kwords("", "blah") self.assertEqual(loc, None) result = iter(results).next() self.assertEqual(result["title"], "title 6")
def clean(self): # if errors in data, cleaned_data may be wiped, and/or fields not available cleaned_data = self.cleaned_data data = cleaned_data.get('post_code', '').strip() kwords = cleaned_data.get('kwords', '').strip() boost_location = cleaned_data.get('boost_location', '') or settings.SOLR_LOC_BOOST_DEFAULT # event = cleaned_data.get('events_only') if not(data or kwords): raise forms.ValidationError("Please enter a location and/or some text and try again.") loc, self.results = find_by_place_or_kwords(data, kwords, boost_location, res_type=settings.SOLR_ACCT) if loc: self.centre = {'name': data, 'location': loc['lat_lon'] } elif data: raise forms.ValidationError("Could not find a location from what you've typed- try again?") return cleaned_data
def clean(self): # if errors in data, cleaned_data may be wiped, and/or fields not available cleaned_data = self.cleaned_data data = cleaned_data.get('post_code', '').strip() kwords = cleaned_data.get('kwords', '').strip() boost_location = cleaned_data.get('boost_location', '') or settings.SOLR_LOC_BOOST_DEFAULT event = cleaned_data.get('events_only') if not(data or kwords): raise forms.ValidationError("Please enter a location and/or some text and try again.") loc, self.results = find_by_place_or_kwords(data, kwords, boost_location, event='*' if event else None) if loc: self.centre = {'loc': Location.objects.get(id=loc['_id']), 'location': loc['lat_lon'] } elif data: increment_failed_locations(data) raise forms.ValidationError("Could not find a location from what you've typed- try again?") return cleaned_data
def test_curation(self): from resources.search import find_by_place_or_kwords # setting resource failed - no idea why, seems a valid object # reloading works self.resource6 = Resource.objects.get(id=self.resource6.id) curation = Curation( outcome='', tags=['blah'], note='bob curated this', owner=self.bob, ) curation.item_metadata.update(author=self.bob) add_curation(self.resource6, curation) loc, results = find_by_place_or_kwords('', 'blah') self.assertEqual(loc, None) result = iter(results).next() self.assertEqual(result['title'], 'title 6')
def clean(self): # if errors in data, cleaned_data may be wiped, and/or fields not available cleaned_data = self.cleaned_data data = cleaned_data.get('post_code', '').strip() kwords = cleaned_data.get('kwords', '').strip() boost_location = cleaned_data.get( 'boost_location', '') or settings.SOLR_LOC_BOOST_DEFAULT # event = cleaned_data.get('events_only') if not (data or kwords): raise forms.ValidationError( "Please enter a location and/or some text and try again.") loc, self.results = find_by_place_or_kwords( data, kwords, boost_location, res_type=settings.SOLR_ACCT) if loc: self.centre = {'name': data, 'location': loc['lat_lon']} elif data: raise forms.ValidationError( "Could not find a location from what you've typed- try again?") return cleaned_data
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)