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

This commit is contained in:
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] s = [x for x in s if x]
return s return s
def edit_params(original, modifications): def dict_to_params(d):
''' '''
Given a dictionary representing URL parameters, Given a dictionary representing URL parameters, return a URL parameter string.
apply the modifications and 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() if not d:
new_params.update(modifications)
if not new_params:
return '' return ''
new_params = ['%s=%s' % (k, v) for (k, v) in new_params.items() if v] params = ['%s=%s' % (k, v) for (k, v) in d.items() if v]
new_params = '&'.join(new_params) params = '&'.join(params)
if new_params: if params:
new_params = '?' + new_params params = '?' + params
return new_params return params
def fit_into_bounds(image_width, image_height, frame_width, frame_height): 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 # PREV-NEXT PAGE URLS
offset = search_kwargs['offset'] or 0 offset = search_kwargs['offset'] or 0
original_params = request.args.to_dict() original_params = request.args.to_dict()
original_params['limit'] = limit
if len(photos) == 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 next_page_url = '/search' + next_params
else: else:
next_page_url = None next_page_url = None
if offset > 0: 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 prev_page_url = '/search' + prev_params
else: else:
prev_page_url = None prev_page_url = None