Commits

Chris Mulligan committed c00dcdf

Added karma page to pmxbotweb

Comments (0)

Files changed (3)

pmxbotweb/templates/base.html

 	<div id="header">
 		<h1><img src="{{ logo }}" border="0" alt="PMXBOT" height="50"/>
 		<a href="{{ request.base }}">Log file Viewer</a></h1>
+		<form action="{{ request.base }}/search" method="GET">
 		<h3>
 		    <a href="{{ request.base }}/help">Help</a> | 
 		    <a href="{{ request.base }}/karma">Karma</a> | 
-            <a href="{{ request.base }}/quotes">Quotes</a>
-        </h3>
-		<div>
-			<form action="{{ request.base }}/search" method="GET">
+            <a href="{{ request.base }}/quotes">Quotes</a> | 
 				Search: <input type="text" name="term" />
 				<input type="submit" value="Search" />
-			</form>
-		</div>
+		</h3>
+		</form>
 
     {% block body %}
     {% endblock %}

pmxbotweb/templates/karma.html

+{% extends "base.html" %}
+{% block body %}
+
+<hr/>
+<h3>Karma Lookup</h3>
+<form action="{{ request.base }}/karma" method="GET">
+	Lookup: <input type="text" name="term" value="{{ term }}"/>
+	<input type="submit" value="Lookup" />
+</form>
+<table style="border: none;">
+{% for names, points in lookup %}
+	<tr><td class="left" style="font-weight: bold; font-family: fixed-width; ">{{ names }}
+	</td><td>{{ points }}</td></tr>
+{% endfor %}
+</table>
+
+
+<h3>Top 100 Karmas</h3>
+<table style="border: none;">
+{% for names, points in top100 %}
+	<tr><td class="left" style="font-weight: bold; font-family: fixed-width; ">{{ names }}
+	</td><td>{{ points }}</td></tr>
+{% endfor %}
+</table>
+
+<h3>Bottom 100 Karmas</h3>
+<table style="border: none;">
+{% for names, points in bottom100 %}
+	<tr><td class="left" style="font-weight: bold; font-family: fixed-width; ">{{ names }}</td>
+	<td>{{ points }}</td></tr>
+{% endfor %}
+</table>
+
+
+{% endblock %}

pmxbotweb/viewer.py

 		return page.render(**context)
 	default.exposed = True
 
+
+def karmaList(db, select=0):
+	KARMIC_VALUES_SQL = 'SELECT karmaid, karmavalue from karma_values order by karmavalue desc'
+	KARMA_KEYS_SQL= 'SELECT karmakey from karma_keys where karmaid = ?'
+
+	karmalist = db.execute(KARMIC_VALUES_SQL).fetchall()
+	karmalist.sort(key=lambda x: int(x[1]), reverse=True)
+	if select > 0:
+		selected = karmalist[:select]
+	elif select < 0:
+		selected = karmalist[select:]
+	else:
+		selected = karmalist
+	keysandkarma = []
+	for karmaid, value in selected:
+		keys = [x[0] for x in db.execute(KARMA_KEYS_SQL, [karmaid])]
+		keysandkarma.append((keys, value))
+	return keysandkarma
+
 class KarmaPage(object):
-	pass
+	def default(self, term=""):
+		page = jenv.get_template('karma.html')
+		context = get_context()
+		dbfile = cherrypy.request.app.config['db']['database']
+		db = sqlite.connect(dbfile)
+		db.text_factory = lambda x: unicode(x, "utf-8", "ignore")
+		term = term.strip()
+		if term:
+			context['lookup'] = []
+			context['term'] = term
+			KARMA_SEARCH_SQL = "SELECT distinct karmaid from karma_keys where karmakey like ? "
+			KARMA_VALUE_SQL = "SELECT karmavalue from karma_values where karmaid = ?"
+			KARMA_KEYS_SQL = "SELECT karmakey from karma_keys where karmaid = ?"
+			matches = db.execute(KARMA_SEARCH_SQL, ['%%%s%%' % term])
+			KARMA_VALUE_SQL = "SELECT karmavalue from karma_values where karmaid = ?"
+			for (id,) in matches:
+				karmavalue = db.execute(KARMA_VALUE_SQL, [id]).fetchone()[0]
+				names = db.execute(KARMA_KEYS_SQL, [id]).fetchall()
+				names = sorted([x[0] for x in names])
+				context['lookup'].append((', '.join(names), karmavalue))
+			if not context['lookup']:
+				context['lookup'].append(('NO RESULTS FOUND', ''))
+		context['top100'] = karmaList(db, 100)
+		context['bottom100'] = karmaList(db, -100)
+		return page.render(**context)
+	default.exposed = True
+		
 
 class SearchPage(object):
 	pass