From 772fbd55c7ab9cbe95bfde27a54d376127ee2cd5 Mon Sep 17 00:00:00 2001 From: Ant Zucaro Date: Sun, 16 Oct 2016 09:50:51 -0400 Subject: [PATCH] Convert the server index to a class-based view callable. Add a JSON view. --- xonstat/__init__.py | 21 ++++++++++++++----- xonstat/views/__init__.py | 5 ++--- xonstat/views/server.py | 44 ++++++++++++++++++--------------------- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/xonstat/__init__.py b/xonstat/__init__.py index 10632d6..961aa3d 100644 --- a/xonstat/__init__.py +++ b/xonstat/__init__.py @@ -9,6 +9,7 @@ from xonstat.models import initialize_db from xonstat.views import * from xonstat.security import * + def main(global_config, **settings): """ This function returns a Pyramid WSGI application. """ @@ -133,11 +134,21 @@ def main(global_config, **settings): config.add_view(game_finder_json, route_name="game_index_json", renderer="jsonp") # SERVER ROUTES - config.add_route("server_index", "/servers") - config.add_view(server_index, route_name="server_index", renderer="server_index.mako") - - config.add_route("server_index_json", "/servers.json") - config.add_view(server_index_json, route_name="server_index_json", renderer="jsonp") + config.add_route("server_index", "/servers") + config.add_view( + view=ServerIndex, + route_name="server_index", + attr="html", + renderer="server_index.mako", + accept="text/html" + ) + config.add_view( + view=ServerIndex, + route_name="server_index", + attr="json", + renderer="json", + accept="text/json" + ) config.add_route("server_info", "/server/{id:\d+}") config.add_view(server_info, route_name="server_info", renderer="server_info.mako") diff --git a/xonstat/views/__init__.py b/xonstat/views/__init__.py index ec60ac8..56727ad 100644 --- a/xonstat/views/__init__.py +++ b/xonstat/views/__init__.py @@ -17,9 +17,8 @@ from xonstat.views.map import map_info, map_index from xonstat.views.map import map_info_json, map_index_json from xonstat.views.map import map_captimes, map_captimes_json -from xonstat.views.server import server_info, server_index -from xonstat.views.server import server_info_json -from xonstat.views.server import server_index_json +from xonstat.views.server import server_info, server_info_json +from xonstat.views.server import ServerIndex from xonstat.views.search import search_q, search from xonstat.views.search import search_json diff --git a/xonstat/views/server.py b/xonstat/views/server.py index 6270ed0..1baeac1 100644 --- a/xonstat/views/server.py +++ b/xonstat/views/server.py @@ -9,37 +9,33 @@ from xonstat.views.helpers import RecentGame, recent_games_q log = logging.getLogger(__name__) -def _server_index_data(request): - if request.params.has_key('page'): - current_page = request.params['page'] - else: - current_page = 1 - try: - server_q = DBSession.query(Server).\ - order_by(Server.server_id.desc()) - - servers = Page(server_q, current_page, items_per_page=25, url=page_url) +class ServerIndex(object): + def __init__(self, request): + self.request = request + self.page = request.params.get("page", 1) + self.servers = self._data() - except Exception as e: - servers = None + def _data(self): + try: + server_q = DBSession.query(Server).order_by(Server.server_id.desc()) + servers = Page(server_q, self.page, items_per_page=25, url=page_url) - return {'servers':servers, } + except: + servers = None + return servers -def server_index(request): - """ - Provides a list of all the current servers. - """ - return _server_index_data(request) + def html(self): + return { + 'servers': self.servers, + } - -def server_index_json(request): - """ - Provides a list of all the current servers. JSON. - """ - return [{'status':'not implemented'}] + def json(self): + return { + 'servers': [s.to_dict() for s in self.servers], + } def _server_info_data(request): -- 2.39.2