Separate concerns: updating given parameters vs rendering dict to param string

master
voussoir 2017-03-03 22:11:29 -08:00
parent 8856a2fe69
commit 73835e2a7a
2 changed files with 17 additions and 14 deletions

View File

@ -103,22 +103,19 @@ def comma_split(s):
s = [x for x in s if x]
return s
def edit_params(original, modifications):
def dict_to_params(d):
'''
Given a dictionary representing URL parameters,
apply the modifications and return a URL parameter string.
Given a dictionary representing URL parameters, return a URL parameter string.
{'a':1, 'b':2}, {'b':3} => ?a=1&b=3
{'a':1, 'b':2} => ?a=1&b=2
'''
new_params = original.copy()
new_params.update(modifications)
if not new_params:
if not d:
return ''
new_params = ['%s=%s' % (k, v) for (k, v) in new_params.items() if v]
new_params = '&'.join(new_params)
if new_params:
new_params = '?' + new_params
return new_params
params = ['%s=%s' % (k, v) for (k, v) in d.items() if v]
params = '&'.join(params)
if params:
params = '?' + params
return params
def fit_into_bounds(image_width, image_height, frame_width, frame_height):
'''

View File

@ -516,13 +516,19 @@ def get_search_core():
# PREV-NEXT PAGE URLS
offset = search_kwargs['offset'] or 0
original_params = request.args.to_dict()
original_params['limit'] = limit
if len(photos) == limit:
next_params = helpers.edit_params(original_params, {'offset': offset + limit})
next_params = original_params.copy()
next_params['offset'] = offset + limit
next_params = helpers.dict_to_params(next_params)
next_page_url = '/search' + next_params
else:
next_page_url = None
if offset > 0:
prev_params = helpers.edit_params(original_params, {'offset': max(0, offset - limit)})
prev_params = original_params.copy()
prev_params['offset'] = max(0, offset - limit)
prev_params = helpers.dict_to_params(prev_params)
prev_page_url = '/search' + prev_params
else:
prev_page_url = None