add pagination
This commit is contained in:
parent
8c3230ad43
commit
666d51ba59
@ -55,14 +55,14 @@ class LayarView(object):
|
|||||||
developer_hash = request.GET['developerHash']
|
developer_hash = request.GET['developerHash']
|
||||||
timestamp = request.GET['timestamp']
|
timestamp = request.GET['timestamp']
|
||||||
layer_name = request.GET['layerName']
|
layer_name = request.GET['layerName']
|
||||||
latitude = request.GET['lat']
|
lat = float(request.GET['lat'])
|
||||||
longitude = request.GET['lon']
|
lon = float(request.GET['lon'])
|
||||||
accuracy = request.GET['accuracy']
|
accuracy = int(request.GET['accuracy'])
|
||||||
radius = request.GET['radius']
|
radius = int(request.GET['radius'])
|
||||||
radio_option = request.GET.get('RADIOLIST')
|
radio_option = request.GET.get('RADIOLIST')
|
||||||
search_query = request.GET.get('SEARCHBOX')
|
search = request.GET.get('SEARCHBOX')
|
||||||
custom_slider = request.GET.get('CUSTOM_SLIDER')
|
slider = request.GET.get('CUSTOM_SLIDER')
|
||||||
page_key = request.GET.get('pageKey')
|
page = int(request.GET.get('pageKey', 0))
|
||||||
|
|
||||||
# oauth: oauth_consumer_key, oauth_signature_method, oauth_timestamp,
|
# oauth: oauth_consumer_key, oauth_signature_method, oauth_timestamp,
|
||||||
# oauth_nonce, oauth_version, oauth_signature
|
# oauth_nonce, oauth_version, oauth_signature
|
||||||
@ -77,20 +77,35 @@ class LayarView(object):
|
|||||||
if sha1(key).hexdigest() != developer_hash:
|
if sha1(key).hexdigest() != developer_hash:
|
||||||
raise LayarException(20, 'Bad developerHash')
|
raise LayarException(20, 'Bad developerHash')
|
||||||
|
|
||||||
|
# get ``max_results`` items from queryset
|
||||||
try:
|
try:
|
||||||
poi_func = getattr(self, 'get_%s_pois' % layer_name)
|
qs_func = getattr(self, 'get_%s_queryset' % layer_name)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
raise LayarException(21, 'no such layer: %s' % layer_name)
|
raise LayarException(21, 'no such layer: %s' % layer_name)
|
||||||
|
|
||||||
pois = poi_func(latitude=latitude, longitude=longitude, radius=radius,
|
qs = qs_func(latitude=lat, longitude=lon, radius=radius,
|
||||||
start_index=0,
|
radio_option=radio_option, search_query=search,
|
||||||
radio_option=radio_option, search_query=search_query,
|
slider_value=slider)[:self.max_results]
|
||||||
custom_slider=custom_slider)
|
|
||||||
|
|
||||||
# pagination logic
|
# do pagination if results_per_page is set
|
||||||
if len(pois) > self.max_results:
|
if self.results_per_page:
|
||||||
pois = pois[:self.max_results]
|
start_index = self.results_per_page * page
|
||||||
|
end_index = start_index + self.results_per_page
|
||||||
|
|
||||||
|
# if there are more pages, indicate that in response
|
||||||
|
if end_index < qs.count()-1:
|
||||||
|
layar_response['morePages'] = True
|
||||||
|
layar_response['nextPageKey'] = str(page+1)
|
||||||
|
|
||||||
|
qs = qs[start_index:end_index]
|
||||||
|
|
||||||
|
# convert queryset into POIs
|
||||||
|
try:
|
||||||
|
poi_func = getattr(self, 'poi_from_%s_item' % layer_name)
|
||||||
|
except AttributeError:
|
||||||
|
raise LayarException(21, 'no such layer: %s' % layer_name)
|
||||||
|
|
||||||
|
pois = [poi_func(item) for item in qs]
|
||||||
layar_response['hotspots'] = [poi.to_dict() for poi in pois]
|
layar_response['hotspots'] = [poi.to_dict() for poi in pois]
|
||||||
|
|
||||||
except LayarException, e:
|
except LayarException, e:
|
||||||
|
Loading…
Reference in New Issue
Block a user