Basic player search functionality.
authorAnt Zucaro <azucaro@gmail.com>
Fri, 9 Dec 2011 12:01:01 +0000 (07:01 -0500)
committerAnt Zucaro <azucaro@gmail.com>
Fri, 9 Dec 2011 12:01:01 +0000 (07:01 -0500)
xonstat/__init__.py
xonstat/templates/search.mako [new file with mode: 0644]
xonstat/views/__init__.py
xonstat/views/search.py [new file with mode: 0644]

index 49aab4fe285019aafa87f5a132966bdbe7cf98a4..2408c78cbbc9eaff3f74d8a04a091b45c48aac3a 100755 (executable)
@@ -83,4 +83,9 @@ def main(global_config, **settings):
     config.add_route(name="stats_submit", pattern="stats/submit", 
             view=stats_submit, renderer='index.jinja2') 
 
+    # SEARCH ROUTES
+    config.add_route(name="search", pattern="search",
+            view=search, renderer='search.mako')
+
+
     return config.make_wsgi_app()
diff --git a/xonstat/templates/search.mako b/xonstat/templates/search.mako
new file mode 100644 (file)
index 0000000..7520caa
--- /dev/null
@@ -0,0 +1,22 @@
+% if results == None:
+<form action="${request.route_url("search")}" method="get">
+    <input type="hidden" name="form.submitted" />
+    Nick: <input type="text" name="nick" /> <br />
+    <input type="submit" />
+</form>
+% endif
+
+% if result_type == "player":
+<table>
+    <tr>
+        <th>Player</th>
+        <th>Joined</th>
+    </tr>
+    % for player in results:
+    <tr>
+        <td>${player.nick_html_colors()|n}</td>
+        <td>${player.joined_pretty_date()}</td>
+    </tr>
+    % endfor
+</table>
+% endif
index 7fdf4d4436262599123b99dd33e3293640bce75c..9d89e605a32836bed457733a76af9700eecd2faf 100755 (executable)
@@ -3,4 +3,5 @@ from xonstat.views.player import player_index, player_info, player_game_index
 from xonstat.views.game import game_index, game_info\r
 from xonstat.views.map import map_info, map_index\r
 from xonstat.views.server import server_info, server_game_index, server_index\r
+from xonstat.views.search import *\r
 from xonstat.views.main import main_index\r
diff --git a/xonstat/views/search.py b/xonstat/views/search.py
new file mode 100644 (file)
index 0000000..1b3263e
--- /dev/null
@@ -0,0 +1,48 @@
+import datetime
+import logging
+import pyramid.httpexceptions
+import re
+import time
+from pyramid.response import Response
+from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound
+from sqlalchemy.sql import func
+from xonstat.models import *
+from xonstat.util import strip_colors, qfont_decode
+
+log = logging.getLogger(__name__)
+
+def search_q(player_id=None, nick=None, server_id=None, server_name=None,
+        map_id=None, map_name=None, game_id=None, create_dt=None):
+    session     = DBSession()
+    result_type = None
+    results     = None
+
+    # player-only searches
+    if ((player_id or nick) and not server_id and not server_name and not
+            map_id and not map_name and not game_id and not create_dt):
+        result_type = "player"
+        q = session.query(Player)
+        if nick:
+            q = q.filter(func.upper(Player.stripped_nick).like('%'+nick.upper()+'%'))
+        if player_id:
+            q = q.filter(Player.player_id==player_id)
+
+    try:
+        results = q.all()
+    except:
+        result_type = None
+        results = None
+
+    return (result_type, results)
+
+def search(request):
+    result_type = None
+    results = None
+
+    if request.params.has_key('form.submitted'):
+        nick = request.params['nick']
+        (result_type, results) = search_q(nick=nick)
+
+    return {'result_type':result_type,
+            'results':results,
+            }