X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=main.py;h=22d8c87ade3746d249100dbcac7f58916bea5941;hb=e3913800e8bccbdc364b7f54d28a8d8efcfa2e1b;hp=bb092acbb8232511128a5aa49640a65b7809bdf7;hpb=df5ece768e1f6197ca618c89490563e0420fce9d;p=cavote.git diff --git a/main.py b/main.py index bb092ac..22d8c87 100755 --- a/main.py +++ b/main.py @@ -293,17 +293,23 @@ def votes(votes): max_votes ='select id_group, count(*) as max_votes from user_group group by id_group' basequery = 'select votes.*, max_votes from votes join (' + max_votes + ') as max_votes on votes.id_group = max_votes.id_group' nb_votes = 'select id_vote, count(*) as nb_votes from (select id_user, id_vote from user_choice join choices on id_choice = choices.id group by id_user, id_vote) group by id_vote' - basequery = 'select * from (' + basequery + ') join (' + nb_votes + ') on id = id_vote' + basequery = 'select * from (' + basequery + ') left join (' + nb_votes + ') on id = id_vote' basequery = 'select *, votes.id as voteid, groups.name as groupname from (' + basequery + ') as votes join groups on groups.id = id_group where is_open=1' if votes == 'all': votes = query_db(basequery + ' order by id desc') elif votes == 'archive': - votes = query_db(basequery + ' and date_end < (?) order by id desc', [today]) + votes = query_db(basequery + ' and is_terminated=1 order by id desc') elif votes == 'current': - votes = query_db(basequery + ' and date_end >= (?) order by id desc', [today]) + votes = query_db(basequery + ' and is_terminated=0 order by id desc') + elif votes == 'waiting': + basequery = 'select votes.* from user_group join (' + basequery + ') as votes on votes.id_group = user_group.id_group where user_group.id_user = ?' + already_voted = 'select id_vote from user_choice join choices on user_choice.id_choice = choices.id where id_user = ?' + votes = query_db(basequery + ' and votes.id not in (' + already_voted + ') and is_terminated=0', [get_userid(), get_userid()]) else: abort(404) for vote in votes: + if not vote.get('nb_votes'): + vote['nb_votes'] = 0 vote['percent'] = int((float(vote['nb_votes']) / float(vote['max_votes'])) * 100) return render_template('votes.html', votes=votes, active_button=active_button) @@ -313,18 +319,23 @@ def votes(votes): def can_see_vote(idvote, iduser=-1): vote = query_db('select * from votes where id=?', [idvote], one=True) if vote is None: - abort(404) + return False if not vote['is_public']: user = query_db('select * from users where id=?', [iduser], one=True) - if user is None: # :TODO:maethor:120604: Check others things (groups) + if query_db('select * from user_group where id_user = ? and id_group = ?', [iduser, vote['id']], one=True) is None: return False return True def can_vote(idvote, iduser=-1): - if iduser > 0: - if can_see_vote(idvote, iduser): - if not has_voted(idvote, iduser): - return True # :TODO:maethor:120529: Check others things (groups) + vote = query_db('select * from votes where id=?', [idvote], one=True) + if vote is None: + return False + if not vote['is_finished']: + if iduser > 0: + if can_see_vote(idvote, iduser): + if not has_voted(idvote, iduser): + if query_db('select * from user_group where id_user = ? and id_group = ?', [iduser, vote['id']], one=True): + return True return False def has_voted(idvote, iduser=-1): @@ -373,8 +384,8 @@ def vote(idvote): vote['nb_votes'] = tmp['nb'] vote['percent'] = int((float(vote['nb_votes']) / float(vote['max_votes'])) * 100) return render_template('vote.html', vote=vote, attachments=attachments, choices=choices, users=users.values(), can_vote=can_vote(idvote, get_userid())) - flash('Vous n\'avez pas le droit de voir ce vote, désolé.') - return(url_for('home')) + flash(u'Vous n\'avez pas le droit de voir ce vote, désolé.') + return redirect(url_for('home')) @app.route('/vote/deletechoices//') def vote_deletechoices(idvote, iduser): @@ -451,14 +462,19 @@ def admin_vote_edit(voteid): if 'public' in request.form.keys(): public = 1 isopen = 0 + isterminated = 0 if request.form['status'] == 'Ouvert': choices = query_db('select id_vote, count(*) as nb from choices where id_vote = ? group by id_vote', [voteid], one=True) if choices is not None and choices['nb'] >= 2: isopen = 1 else: flash(u'Vous devez proposer au moins deux choix pour ouvrir le vote.', 'error') - 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]) + elif request.form['status'] == u'Terminé': + isterminated = 1 + if vote['is_open']: + isopen = 1 + g.db.execute('update votes set title = ?, description = ?, category = ?, is_transparent = ?, is_public = ?, is_open = ?, is_terminated = ? where id = ?', + [request.form['title'], request.form['description'], request.form['category'], transparent, public, isopen, isterminated, voteid]) g.db.commit() vote = query_db('select * from votes where id = ?', [voteid], one=True) flash(u"Le vote a bien été mis à jour.", "success")