From: Guillaume Subiron Date: Mon, 4 Jun 2012 09:38:35 +0000 (+0200) Subject: Users can vote X-Git-Url: http://git.cyclocoop.org/?p=cavote.git;a=commitdiff_plain;h=33346b25d357b3ad3242cfac21ab09e92cb2024b Users can vote --- diff --git a/main.py b/main.py index a807a51..dca985f 100755 --- 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/') +@app.route('/vote/', 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')) diff --git a/schema.sql b/schema.sql index 20ddccb..891cf76 100644 --- a/schema.sql +++ b/schema.sql @@ -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 diff --git a/templates/vote.html b/templates/vote.html index 30602cb..fd93e5b 100644 --- a/templates/vote.html +++ b/templates/vote.html @@ -16,14 +16,21 @@ {% if vote.is_transparent %} - + {% for user in users %} + + {{ user.username }} + {% for choice in choices %} + {% if choice in user.choices %}OUI{% else %}NON{% endif %} + {% endfor %} + + {% endfor %} {% else %}
Ce sondage n'est pas transparent, vous ne pouvez pas voir les votes des autres.
{% endif %} {% if can_vote %} -
+ {% if vote.is_multiple %} @@ -32,7 +39,7 @@ {% endfor %} {% else %} {% for choice in choices %} - + {% endfor %} {% endif %}