Show vote
authorGuillaume Subiron <maethor@subiron.org>
Tue, 29 May 2012 11:41:05 +0000 (13:41 +0200)
committerJulien Rabier <taziden@flexiden.org>
Tue, 29 May 2012 11:43:22 +0000 (13:43 +0200)
main.py
templates/show_votes.html
templates/vote.html [new file with mode: 0644]

diff --git a/main.py b/main.py
index 7ff598f..bff42a0 100755 (executable)
--- a/main.py
+++ b/main.py
@@ -207,15 +207,41 @@ def del_role(idrole):
 def show_votes(votes):
     today = date.today()
     if votes == 'all':
-        votes = query_db('select title, description, date_begin, date_end from votes order by id desc')
+        votes = query_db('select * 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])
+        votes = query_db('select * 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])
+        votes = query_db('select * from votes where date_end >= (?) order by id desc', [today])
     else:
         abort(404)
     return render_template('show_votes.html', votes=votes)
 
+#------
+# Vote
+
+def can_see_vote(idvote, iduser=-1):
+    user = query_db('select * from users where id=?', [iduser], one=True)
+    vote = query_db('select * from votes where id=?', [idvote], one=True)
+    if user is None and not vote.is_public:
+        return False
+    return True # :TODO:maethor:20120529: Check others things
+
+def can_vote(idvote, iduser=-1):
+    if not can_see_vote(idvote, iduser):
+        return False
+    return True # :TODO:maethor:20120529: Check others things
+
+@app.route('/vote/<idvote>')
+def show_vote(idvote):
+    vote = query_db('select * from votes where id=?', [idvote], one=True)
+    if vote is None:
+        abort(404)
+    if can_see_vote(idvote, session.get('user').get('id')):
+        choices = query_db('select * from choices where id_vote=?', [idvote])
+        return render_template('vote.html', vote=vote, choices=choices, can_vote=can_vote(idvote, session.get('user').get('id')))
+    flash('Vous n\'avez pas le droit de voir ce vote, désolé.')
+    return(url_for('home'))
+
 #-------------
 # Votes admin
 
@@ -272,7 +298,7 @@ def edit_vote(voteid):
             if 'public' in request.form.keys():
                 public = 1
             isopen = 0
-            if request.form['status'] == 'Ouvert':
+            if request.form['status'] == 'Ouvert': # :TODO:maethor:20120529: Check if there is at least 2 choices before
                 isopen = 1
             g.db.execute('update votes set title = ?, description = ?, category = ?, is_transparent = ?, is_public = ?, is_open = ? where id = ?',
                     [request.form['title'], request.form['description'], request.form['category'], transparent, public, isopen, voteid])
index 079632b..5d7442e 100644 (file)
@@ -4,7 +4,7 @@
   {% for vote in votes %}
   <article>
     <div class="well">
-    <h3>{{ vote.title }}</h3>
+    <h3><a href="{{ url_for('show_vote', idvote=vote.id) }}">{{ vote.title }}</a></h3>
     <hr />
     <div class="row">
     <div class="span4">
@@ -26,4 +26,3 @@
   <div class="alert">Il n'y a pas encore de votes. Désolé.</div>
   {% endfor %}
 {% endblock %}
-
diff --git a/templates/vote.html b/templates/vote.html
new file mode 100644 (file)
index 0000000..b51896f
--- /dev/null
@@ -0,0 +1,58 @@
+{% extends "layout.html" %}
+{% block body %}
+<h2>{{ vote.title }}</h2>
+<div class="row">
+<div class="span10 offset1">
+<table class="table table-condensed table-striped table-bordered">
+  <thead>
+    <tr>
+      <th></th>
+      {% for choice in choices %}
+      <th>{{ choice.name }}</th>
+      {% endfor %}
+      <th></th>
+    </tr>
+  </thead>
+
+  <tbody>
+  {% if vote.is_transparent %}
+  <!-- :TODO:maethor:20120528: Afficher les votes ici -->
+  {% else %}
+    <div class="alert alert-info">Ce sondage n'est pas transparent, vous ne pouvez pas voir les votes des autres.</div>
+    <!-- :TODO:maethor:20120528: Afficher le vote de l'utilisateur -->
+  {% endif %}
+
+  {% if can_vote %}
+    <form class="form-inline" action=""> <!-- :TODO:maethor:20120528: Route -->
+    <tr>
+      <td><input type='text' name="username" value='{{ session.user.name }}' disabled /></td>
+      {% if vote.is_multiple %}
+      {% for choice in choices %}
+      <td><input type='checkbox' name="{{ choice.id }}" /></td>
+      {% endfor %}
+      {% else %}
+      {% for choice in choices %}
+      <td><input type='radio' name="choice" value="{{ choice.id }}" /></td>
+      {% endfor %}
+      {% endif %}
+      <td><input type="submit" class="btn btn-primary" value="OK" /></td>
+    </tr>
+    </form>
+  {% endif %}
+  </tbody>
+  <tfoot>
+  {% if vote.is_transparent %}
+    <tr>
+      <td style="text-align: right">Somme</td>
+      {% for choice in choices %}
+      <td>nb</td>
+      {% endfor %}
+      <td></td>
+    </tr>
+  {% endif %}
+  </tfoot>
+</table>
+</div>
+</div>
+{% endblock %}
+