From d1860bce8a2558a38b1fd2c98a29a330fd60aac7 Mon Sep 17 00:00:00 2001 From: Ant Zucaro Date: Mon, 14 May 2012 16:55:51 -0400 Subject: [PATCH] Make pagination a GET parameter, not part of the URL. It was not the best idea to add the 'page' displayed to be an actual part of the URL. It should be a GET parameter instead, which jives a little better with my understanding of REST-like resources. This changes all paginated views to use a GET parameter and also limits results returned to 10 per page for performance and viewability reasons. --- xonstat/templates/game_index.mako | 2 +- xonstat/templates/map_index.mako | 2 +- xonstat/templates/navlinks.mako | 28 +++++++++++++++++++++------- xonstat/templates/player_index.mako | 2 +- xonstat/templates/search.mako | 2 +- xonstat/templates/server_index.mako | 2 +- xonstat/views/game.py | 6 +++--- xonstat/views/map.py | 7 +++---- xonstat/views/player.py | 15 ++++----------- xonstat/views/search.py | 7 ++++--- xonstat/views/server.py | 6 +++--- 11 files changed, 43 insertions(+), 36 deletions(-) diff --git a/xonstat/templates/game_index.mako b/xonstat/templates/game_index.mako index f9d11de..ecff992 100755 --- a/xonstat/templates/game_index.mako +++ b/xonstat/templates/game_index.mako @@ -28,6 +28,6 @@ Game Index -${navlinks("game_index_paged", games.page, games.last_page)} +${navlinks("game_index", games.page, games.last_page)} % endif diff --git a/xonstat/templates/map_index.mako b/xonstat/templates/map_index.mako index c6ae6b6..fe34956 100755 --- a/xonstat/templates/map_index.mako +++ b/xonstat/templates/map_index.mako @@ -37,6 +37,6 @@ Map Index % endif - ${navlinks("map_index_paged", maps.page, maps.last_page)} + ${navlinks("map_index", maps.page, maps.last_page)} diff --git a/xonstat/templates/navlinks.mako b/xonstat/templates/navlinks.mako index acfb166..5c1fa4f 100755 --- a/xonstat/templates/navlinks.mako +++ b/xonstat/templates/navlinks.mako @@ -1,7 +1,15 @@ <%def name="navlinks(view, curr, last, **kwargs)"> +<% +kwargs['_query'] = {'page': None} + +if 'search_query' in kwargs.keys(): + kwargs['_query'] = dict(kwargs['_query'].items() + kwargs['search_query'].items()) +%> + % if not (curr == last and curr == 1): % if curr != 1: - + <% kwargs['_query']['page'] = curr-1 %> + % endif % if last < 8: @@ -14,29 +22,34 @@ ${link_page(view, i, curr, **kwargs)} % endfor ... - + <% kwargs['_query']['page'] = last %> + % elif last-curr < 6: - + <% kwargs['_query']['page'] = 1 %> + ... % for i in range(last-5, last+1): ${link_page(view, i, curr, **kwargs)} % endfor % else: - + <% kwargs['_query']['page'] = 1 %> + ... % for i in range(curr-2, curr+3): ${link_page(view, i, curr, **kwargs)} % endfor ... - + <% kwargs['_query']['page'] = last %> + % endif % endif % if curr != last: - + <% kwargs['_query']['page'] = curr+1 %> + % endif % endif @@ -45,6 +58,7 @@ % if page_num == curr_page: ${page_num} % else: - + <% kwargs['_query']['page'] = page_num %> + % endif diff --git a/xonstat/templates/player_index.mako b/xonstat/templates/player_index.mako index 44e5fbb..6bfdc7d 100755 --- a/xonstat/templates/player_index.mako +++ b/xonstat/templates/player_index.mako @@ -35,6 +35,6 @@ Player Index % endif - ${navlinks("player_index_paged", players.page, players.last_page)} + ${navlinks("player_index", players.page, players.last_page)} diff --git a/xonstat/templates/search.mako b/xonstat/templates/search.mako index 3c4f794..a7d5454 100755 --- a/xonstat/templates/search.mako +++ b/xonstat/templates/search.mako @@ -106,7 +106,7 @@ % endif -${navlinks("search_paged", results.page, results.last_page, _query=query)} +${navlinks("search", results.page, results.last_page, search_query=query)} % endif <%block name="js"> diff --git a/xonstat/templates/server_index.mako b/xonstat/templates/server_index.mako index 9ec0b38..d6893cd 100755 --- a/xonstat/templates/server_index.mako +++ b/xonstat/templates/server_index.mako @@ -35,6 +35,6 @@ Server Index % endif - ${navlinks("server_index_paged", servers.page, servers.last_page)} + ${navlinks("server_index", servers.page, servers.last_page)} diff --git a/xonstat/views/game.py b/xonstat/views/game.py index 1ce8a9a..4b8bdbf 100755 --- a/xonstat/views/game.py +++ b/xonstat/views/game.py @@ -17,8 +17,8 @@ def game_index(request): These games are ordered by game_id, with the most current ones first. Paginated. """ - if 'page' in request.matchdict: - current_page = request.matchdict['page'] + if request.params.has_key('page'): + current_page = request.params['page'] else: current_page = 1 @@ -27,7 +27,7 @@ def game_index(request): filter(Game.map_id == Map.map_id).\ order_by(Game.game_id.desc()) - games = Page(games_q, current_page, url=page_url) + games = Page(games_q, current_page, items_per_page=10, url=page_url) pgstats = {} for (game, server, map) in games: diff --git a/xonstat/views/map.py b/xonstat/views/map.py index fff321f..66c524b 100755 --- a/xonstat/views/map.py +++ b/xonstat/views/map.py @@ -14,8 +14,8 @@ def map_index(request): """ Provides a list of all the current maps. """ - if 'page' in request.matchdict: - current_page = request.matchdict['page'] + if request.params.has_key('page'): + current_page = request.params['page'] else: current_page = 1 @@ -23,9 +23,8 @@ def map_index(request): map_q = DBSession.query(Map).\ order_by(Map.map_id.desc()) - maps = Page(map_q, current_page, url=page_url) + maps = Page(map_q, current_page, items_per_page=10, url=page_url) - except Exception as e: maps = None diff --git a/xonstat/views/player.py b/xonstat/views/player.py index c20a618..5d67fb9 100755 --- a/xonstat/views/player.py +++ b/xonstat/views/player.py @@ -19,8 +19,8 @@ def player_index(request): """ Provides a list of all the current players. """ - if 'page' in request.matchdict: - current_page = int(request.matchdict['page']) + if request.params.has_key('page'): + current_page = request.params['page'] else: current_page = 1 @@ -33,19 +33,12 @@ def player_index(request): players = Page(player_q, current_page, items_per_page=10, url=page_url) - last_linked_page = current_page + 4 - if last_linked_page > players.last_page: - last_linked_page = players.last_page - - pages_to_link = range(current_page+1, last_linked_page+1) - except Exception as e: players = None raise e - return {'players':players, - 'pages_to_link':pages_to_link, - } + return {'players':players + } def get_games_played(player_id): diff --git a/xonstat/views/search.py b/xonstat/views/search.py index 3e16e77..8ce2200 100755 --- a/xonstat/views/search.py +++ b/xonstat/views/search.py @@ -85,8 +85,9 @@ def search(request): query = None _query = {} - if 'page' in request.matchdict: - current_page = request.matchdict['page'] + + if request.params.has_key('page'): + current_page = request.params['page'] else: current_page = 1 @@ -134,7 +135,7 @@ def search(request): try: if q != None: - results = Page(q, current_page, url=page_url) + results = Page(q, current_page, items_per_page=10, url=page_url) except Exception as e: raise e result_type = None diff --git a/xonstat/views/server.py b/xonstat/views/server.py index 8e4a7c6..daa9885 100755 --- a/xonstat/views/server.py +++ b/xonstat/views/server.py @@ -15,8 +15,8 @@ def server_index(request): """ Provides a list of all the current servers. """ - if 'page' in request.matchdict: - current_page = request.matchdict['page'] + if request.params.has_key('page'): + current_page = request.params['page'] else: current_page = 1 @@ -24,7 +24,7 @@ def server_index(request): server_q = DBSession.query(Server).\ order_by(Server.server_id.desc()) - servers = Page(server_q, current_page, url=page_url) + servers = Page(server_q, current_page, items_per_page=10, url=page_url) except Exception as e: -- 2.39.2