Users can vote
authorGuillaume Subiron <maethor@subiron.org>
Mon, 4 Jun 2012 09:38:35 +0000 (11:38 +0200)
committerJulien Rabier <taziden@flexiden.org>
Mon, 4 Jun 2012 22:39:05 +0000 (00:39 +0200)
main.py
schema.sql
templates/vote.html

diff --git a/main.py b/main.py
index a807a51..dca985f 100755 (executable)
--- a/main.py
+++ b/main.py
@@ -125,7 +125,7 @@ def user_edit(userid):
                 g.db.execute('update users set email = ?, name = ?, organization = ? where id = ?',
                         [request.form['email'], request.form['name'], request.form['organization'], session['user']['id']])
                 g.db.commit()
-                disconnect_user() # :TODO:maethor:120528: Maybe useless, but this is simple way to refresh session :D
+                disconnect_user()
                 user = query_db('select * from users where id=?', [userid], one=True)
                 if user is None:
                     flash(u'Une erreur s\'est produite.', 'error')
@@ -269,19 +269,47 @@ def can_see_vote(idvote, iduser=-1):
     return True # :TODO:maethor:120529: Check others things
 
 def can_vote(idvote, iduser=-1):
+    # :TODO:maethor:120604: Check if user has'nt already vote
     if not can_see_vote(idvote, iduser):
         return False
     return True # :TODO:maethor:120529: Check others things
 
-@app.route('/vote/<idvote>')
+@app.route('/vote/<idvote>', methods=['GET', 'POST'])
 def vote(idvote):
     vote = query_db('select *, roles.name as rolename from votes join roles on roles.id=votes.id_role where votes.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])
+        choices = query_db('select name, id from choices where id_vote=?', [idvote])
+        if request.method == 'POST':
+            if can_vote(idvote, session.get('user').get('id')):
+                for choice in choices:
+                    if str(choice['id']) in request.form.keys():
+                        g.db.execute('insert into user_choice (id_user, id_choice) values (?, ?)', 
+                                [session.get('user').get('id'), choice['id']])
+                        g.db.commit()
+                        if vote['is_multiplechoice'] == 0:
+                            break
+            else:
+                abort(401)
+        tuples = query_db('select choiceid, choicename, users.id as userid, users.name as username from (select choices.id as choiceid, choices.name as choicename, id_user as userid from choices join user_choice on choices.id = user_choice.id_choice where id_vote = ?) join users on userid = users.id', [idvote])
+        users = dict()
+        for t in tuples:
+            if t['userid'] in users:
+                choice = dict()
+                choice['id'] = t['choiceid']
+                choice['name'] = t['choicename']
+                users[t['userid']]['choices'].append(choice)
+            else:
+                users[t['userid']] = dict()
+                users[t['userid']]['userid'] = t['userid']
+                users[t['userid']]['username'] = t['username']
+                choice = dict()
+                choice['id'] = t['choiceid']
+                choice['name'] = t['choicename']
+                users[t['userid']]['choices'] = [choice]
         attachments = query_db('select * from attachments where id_vote=?', [idvote])
-        return render_template('vote.html', vote=vote, attachments=attachments, choices=choices, can_vote=can_vote(idvote, session.get('user').get('id')))
+        return render_template('vote.html', vote=vote, attachments=attachments, choices=choices, users=users.values(), 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'))
 
index 20ddccb..891cf76 100644 (file)
@@ -1,3 +1,4 @@
+drop table if exists user_choice;
 drop table if exists choices;
 drop table if exists attachments;
 drop table if exists votes;
@@ -61,6 +62,15 @@ create table choices (
     FOREIGN KEY(id_vote) REFERENCES vote(id)
 );
 
+create table user_choice (
+    id_user INTEGER,
+    id_choice INTEGER,
+    weight INTEGER,
+    FOREIGN KEY(id_user) REFERENCES users(id),
+    FOREIGN KEY(id_choice) REFERENCES choices(id),
+    PRIMARY KEY(id_user, id_choice)
+);
+
 -- Test data
 
 insert into users (email, password, name, organization, is_admin, key) values ("admin@admin.fr", "d033e22ae348aeb5660fc2140aec35850c4da997", "Toto (admin) Tata", "World corp", 1, "test"); -- mdp = admin
index 30602cb..fd93e5b 100644 (file)
 
   <tbody>
   {% if vote.is_transparent %}
-  <!-- :TODO:maethor:20120528: Afficher les votes ici -->
+  {% for user in users %}
+    <tr>
+      <td>{{ user.username }}</td>
+      {% for choice in choices %}
+      <th>{% if choice in user.choices %}OUI{% else %}NON{% endif %}</th>
+      {% endfor %}
+    </tr>
+  {% endfor %}
   {% 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 -->
+    <form class="form-inline" action="{{ url_for('vote', idvote=vote.id) }}" method="post">
     <tr>
       <td><input type='text' name="username" value='{{ session.user.name }}' disabled /></td>
       {% if vote.is_multiple %}
@@ -32,7 +39,7 @@
       {% endfor %}
       {% else %}
       {% for choice in choices %}
-      <td><input type='radio' name="choice" value="{{ choice.id }}" /></td>
+      <td><input type='radio' name="{{ choice.id }}" /></td>
       {% endfor %}
       {% endif %}
       <td><input type="submit" class="btn btn-primary" value="OK" /></td>