Use db_query() function to get entries from db
authorGuillaume Subiron <maethor@subiron.org>
Mon, 28 May 2012 13:23:44 +0000 (15:23 +0200)
committerJulien Rabier <taziden@flexiden.org>
Mon, 28 May 2012 16:29:12 +0000 (18:29 +0200)
main.py
templates/layout.html

diff --git a/main.py b/main.py
index 77310af..f749cc4 100755 (executable)
--- a/main.py
+++ b/main.py
@@ -5,6 +5,7 @@ from flask import Flask, request, session, g, redirect, url_for, abort, \
     render_template, flash
 import sqlite3
 from datetime import date, timedelta
+from contextlib import closing
 import locale
 locale.setlocale(locale.LC_ALL, '')
 
@@ -32,6 +33,17 @@ def teardown_request(exception):
 def home():
     return render_template('index.html')
 
+def query_db(query, args=(), one=False):
+    cur = g.db.execute(query, args)
+    rv = [dict((cur.description[idx][0], value)
+        for idx, value in enumerate(row)) for row in cur.fetchall()]
+    return (rv[0] if rv else None) if one else rv
+
+def init_db():
+    with closing(connect_db()) as db:
+        with app.open_resource('schema.sql') as f:
+            db.cursor().executescript(f.read())
+        db.commit()
 
 #----------------
 # Login / Logout
@@ -79,15 +91,13 @@ def show_settings(username):
 def show_votes(votes):
     today = date.today()
     if votes == 'all':
-        cur = g.db.execute('select title, description, date_begin, date_end from votes order by id desc')
-    elif votes == 'archives':
-        cur = g.db.execute('select title, description, date_begin, date_end from votes where date_end < (?) order by id desc', [today])
-    elif votes == 'currently':
-        cur = g.db.execute('select title, description, date_begin, date_end from votes where date_end >= (?) order by id desc', [today])
+        votes = query_db('select title, description, date_begin, date_end from votes order by id desc')
+    elif votes == 'archive':
+        votes = query_db('select title, description, date_begin, date_end from votes where date_end < (?) order by id desc', [today])
+    elif votes == 'current':
+        votes = query_db('select title, description, date_begin, date_end from votes where date_end >= (?) order by id desc', [today])
     else:
         abort(404)
-    votes = [dict(title=row[0], description=row[1], date_begin=row[2], date_end=row[3], 
-        pourcent=60) for row in cur.fetchall()]
     return render_template('show_votes.html', votes=votes)
 
 #-------------
index ce4b89b..acc8c74 100644 (file)
@@ -20,8 +20,8 @@
   <div class="btn-toolbar">
     <div class="btn-group">
       <a class="btn active" href="/">Accueil</a>
-      <a class="btn" href="{{ url_for('show_votes', votes='currently') }}">Votes en cours</a>
-      <a class="btn" href="{{ url_for('show_votes', votes='archives') }}">Archives</a>
+      <a class="btn" href="{{ url_for('show_votes', votes='current') }}">Votes en cours</a>
+      <a class="btn" href="{{ url_for('show_votes', votes='archive') }}">Archives</a>
       {% if session.is_admin %}
       <a href="#" class="btn btn-warning dropdown-toggle" data-toggle="dropdown">Admin <b class="caret"></b></a>
       <ul class="dropdown-menu pull-right">