From 0b580c50a8568e6927e1914d2408c296a9b30823 Mon Sep 17 00:00:00 2001 From: Guillaume Subiron Date: Mon, 4 Jun 2012 18:22:53 +0200 Subject: [PATCH] We cannot open a vote if there is less than 2 choices --- main.py | 65 ++++++++++++++++++++++---------------- templates/admin_votes.html | 5 ++- templates/vote.html | 4 +-- 3 files changed, 43 insertions(+), 31 deletions(-) diff --git a/main.py b/main.py index b3f50ba..7870ee7 100755 --- a/main.py +++ b/main.py @@ -350,31 +350,34 @@ def admin_vote_add(): abort(401) if request.method == 'POST': if request.form['title']: - date_begin = date.today() - date_end = date.today() + timedelta(days=int(request.form['days'])) - transparent = 0 - public = 0 - multiplechoice = 0 - if 'transparent' in request.form.keys(): - transparent = 1 - if 'public' in request.form.keys(): - public = 1 - if 'multiplechoice' in request.form.keys(): - multiplechoice = 1 - group = query_db('select id from groups where name = ?', [request.form['group']], one=True) - if group is None: - group[id] = 1 - g.db.execute('insert into votes (title, description, category, date_begin, date_end, is_transparent, is_public, is_multiplechoice, id_group, id_author) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', - [request.form['title'], request.form['description'], request.form['category'], date_begin, date_end, transparent, public, multiplechoice, group['id'], session['user']['id']]) - g.db.commit() - vote = query_db('select * from votes where title = ? and date_begin = ? order by id desc', - [request.form['title'], date_begin], one=True) # :DEBUG:maethor:120528: Bug possible car le titre n'est pas unique - if vote is None: - flash(u'Une erreur est survenue !', 'error') - return redirect(url_for('home')) + if query_db('select * from votes where title = ?', [request.form['title']], one=True) is None: + date_begin = date.today() + date_end = date.today() + timedelta(days=int(request.form['days'])) + transparent = 0 + public = 0 + multiplechoice = 0 + if 'transparent' in request.form.keys(): + transparent = 1 + if 'public' in request.form.keys(): + public = 1 + if 'multiplechoice' in request.form.keys(): + multiplechoice = 1 + group = query_db('select id from groups where name = ?', [request.form['group']], one=True) + if group is None: + group[id] = 1 + g.db.execute('insert into votes (title, description, category, date_begin, date_end, is_transparent, is_public, is_multiplechoice, id_group, id_author) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', + [request.form['title'], request.form['description'], request.form['category'], date_begin, date_end, transparent, public, multiplechoice, group['id'], session['user']['id']]) + g.db.commit() + vote = query_db('select * from votes where title = ? and date_begin = ? order by id desc', + [request.form['title'], date_begin], one=True) + if vote is None: + flash(u'Une erreur est survenue !', 'error') + return redirect(url_for('home')) + else: + flash(u"Le vote a été créé", 'info') + return redirect(url_for('admin_vote_edit', voteid=vote['id'])) else: - flash(u"Le vote a été créé", 'info') - return redirect(url_for('admin_vote_edit', voteid=vote['id'])) + flash(u'Le titre que vous avez choisi est déjà pris.', 'error') else: flash(u'Vous devez spécifier un titre.', 'error') groups = query_db('select * from groups') @@ -398,7 +401,11 @@ def admin_vote_edit(voteid): public = 1 isopen = 0 if request.form['status'] == 'Ouvert': # :TODO:maethor:120529: Check if there is at least 2 choices before - isopen = 1 + 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]) g.db.commit() @@ -435,9 +442,6 @@ def admin_vote_editchoice(voteid, choiceid): if request.method == 'POST': g.db.execute('update choices set name=? where id = ? and id_vote = ?', [request.form['title'], choiceid, voteid]) g.db.commit() - elif request.method == 'DELETE': # :COMMENT:maethor:120528: I can't find how to use it from template - g.db.execute('delete from choices where id = ? and id_vote = ?', [choiceid, voteid]) - g.db.commt() return redirect(url_for('admin_vote_edit', voteid=voteid)) @app.route('/admin/votes/deletechoice//') @@ -449,6 +453,11 @@ def admin_vote_deletechoice(voteid, choiceid): abort(404) g.db.execute('delete from choices where id = ? and id_vote = ?', [choiceid, voteid]) g.db.commit() + choices = query_db('select id_vote, count(*) as nb from choices where id_vote = ? group by id_vote', [voteid], one=True) + if choices is None or choices['nb'] < 2: + g.db.execute('update votes set is_open=0 where id = ?', [voteid]) + g.db.commit() + flash(u'Attention ! Il y a moins de deux choix. Le vote a été fermé.', 'error') return redirect(url_for('admin_vote_edit', voteid=voteid)) @app.route('/admin/votes/addattachment/', methods=['POST']) diff --git a/templates/admin_votes.html b/templates/admin_votes.html index 605dc46..786cec3 100644 --- a/templates/admin_votes.html +++ b/templates/admin_votes.html @@ -31,7 +31,10 @@ {% if vote.is_public %}public{% endif %} {% if vote.is_multiplechoice %}choix multiple{% endif %} - Éditer + + Voir + Éditer + {% endfor %} diff --git a/templates/vote.html b/templates/vote.html index c6da367..b6ea499 100644 --- a/templates/vote.html +++ b/templates/vote.html @@ -26,11 +26,11 @@ {% if 'user' in session and user.userid == session.user.id %}{% endif %} {{ user.username }} {% for choice in choices %} {% if choice.id in user.choices %} - OUI + {% else %} {% endif %} {% endfor %} - {% if 'user' in session and user.userid == session.user.id %}X{% endif %} + {% if 'user' in session and user.userid == session.user.id %}{% endif %} {% endif %} {% endfor %} -- 2.20.1