]> git.rm.cloudns.org Git - xonotic/xonstat.git/commitdiff
Convert the server index to a class-based view callable. Add a JSON view.
authorAnt Zucaro <azucaro@gmail.com>
Sun, 16 Oct 2016 13:50:51 +0000 (09:50 -0400)
committerAnt Zucaro <azucaro@gmail.com>
Sun, 16 Oct 2016 13:50:51 +0000 (09:50 -0400)
xonstat/__init__.py
xonstat/views/__init__.py
xonstat/views/server.py

index 10632d627b4771b519ca6d0a443d7ba346aec79e..961aa3d253075fbd5f2ff28c4e0d21853fe55b13 100644 (file)
@@ -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")
index ec60ac84a0d5360fcc42faeb463474782cf72996..56727ad3a5689765dffa46c9743aaa803f37eb0a 100644 (file)
@@ -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
index 6270ed0338585e3ea346d2940aa076f81cb532b1..1baeac1295b63ae2ccdb2e6b539ba8a2febc9a83 100644 (file)
@@ -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):