Add server index and template. Fix links on player index and template.
[xonotic/xonstat.git] / xonstat / views / server.py
1 import datetime\r
2 import logging\r
3 import time\r
4 from pyramid.response import Response\r
5 from sqlalchemy import desc\r
6 from webhelpers.paginate import Page, PageURL\r
7 from xonstat.models import *\r
8 from xonstat.util import page_url\r
9 \r
10 log = logging.getLogger(__name__)\r
11 \r
12 def server_index(request):\r
13     """\r
14     Provides a list of all the current servers. \r
15     """\r
16     if 'page' in request.matchdict:\r
17         current_page = request.matchdict['page']\r
18     else:\r
19         current_page = 1\r
20 \r
21     try:\r
22         server_q = DBSession.query(Server).\\r
23                 order_by(Server.name)\r
24 \r
25         servers = Page(server_q, current_page, url=page_url)\r
26 \r
27         \r
28     except Exception as e:\r
29         servers = None\r
30 \r
31     return {'servers':servers, }\r
32 \r
33 \r
34 def server_info(request):\r
35     """\r
36     List the stored information about a given server.\r
37     """\r
38     server_id = request.matchdict['id']\r
39     try:\r
40         server = DBSession.query(Server).filter_by(server_id=server_id).one()\r
41         recent_games = DBSession.query(Game, Server, Map).\\r
42                 filter(Game.server_id == server_id).\\r
43                 filter(Game.server_id == Server.server_id).\\r
44                 filter(Game.map_id == Map.map_id).\\r
45                 order_by(Game.game_id.desc())[0:10]\r
46 \r
47     except Exception as e:\r
48         server = None\r
49         recent_games = None\r
50     return {'server':server,\r
51             'recent_games':recent_games}\r
52 \r
53 \r
54 def server_game_index(request):\r
55     """\r
56     List the games played on a given server. Paginated.\r
57     """\r
58     server_id = request.matchdict['server_id']\r
59     current_page = request.matchdict['page']\r
60 \r
61     try:\r
62         server = DBSession.query(Server).filter_by(server_id=server_id).one()\r
63 \r
64         games_q = DBSession.query(Game, Server, Map).\\r
65                 filter(Game.server_id == server_id).\\r
66                 filter(Game.server_id == Server.server_id).\\r
67                 filter(Game.map_id == Map.map_id).\\r
68                 order_by(Game.game_id.desc())\r
69 \r
70         games = Page(games_q, current_page, url=page_url)\r
71     except Exception as e:\r
72         server = None\r
73         games = None\r
74         raise e\r
75 \r
76     return {'games':games,\r
77             'server':server}\r