def resource_search(request): def _resource_result(r): return { 'id': r['res_id'], 'title': r['title'], 'description': r['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, } location = request.REQUEST.get('location', '') accounts = request.REQUEST.get('accounts', '') 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') # print accounts.split() 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 not errors: loc, resources = find_by_place_or_kwords(location, query, boost_location, start=start, max=int(max), accounts=accounts.split()) if 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, 'output': output, 'location': loc, 'boostlocation': boost_location, 'results': results } ] return JsonResponse(data=data, callback=callback)
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('tags', '').strip() boost_location = cleaned_data.get('boost_location', '') or settings.SOLR_LOC_BOOST_DEFAULT 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) if loc: self.centre = {'name': data, 'location': loc } elif data: raise forms.ValidationError("Could not find a location from what you've typed- try again?") # else: # self.centre['location'] = get_place_for_placename('perth')['lat_lon'] #.split(settings.LATLON_SEP) 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('tags', '').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 = {'name': data, 'location': loc} elif data: raise forms.ValidationError( "Could not find a location from what you've typed- try again?") # else: # self.centre['location'] = get_place_for_placename('perth')['lat_lon'] #.split(settings.LATLON_SEP) return cleaned_data
def resource_search(request): def _resource_result(r): result = { 'id': r['res_id'], 'title': r['title'], 'description': r['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', '') 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') 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(), 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, 'output': output, 'location': _loc_to_str(loc), 'event': event, 'boostlocation': boost_location, 'results': results } ] return JsonResponse(data=data, callback=callback)