We cannot open a vote if there is less than 2 choices
authorGuillaume Subiron <maethor@subiron.org>
Mon, 4 Jun 2012 16:22:53 +0000 (18:22 +0200)
committerJulien Rabier <taziden@flexiden.org>
Mon, 4 Jun 2012 22:39:15 +0000 (00:39 +0200)
main.py
templates/admin_votes.html
templates/vote.html

diff --git a/main.py b/main.py
index b3f50ba..7870ee7 100755 (executable)
--- 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/<voteid>/<choiceid>')
@@ -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/<voteid>', methods=['POST'])
index 605dc46..786cec3 100644 (file)
         {% if vote.is_public %}<span class="label">public</span>{% endif %}
         {% if vote.is_multiplechoice %}<span class="label">choix multiple</span>{% endif %}
       </td>
-      <td><a href="{{ url_for('admin_vote_edit', voteid=vote.voteid) }}" class="btn btn-mini">Éditer</a></td>
+      <td>
+        <a href="{{ url_for('vote', idvote=vote.voteid) }}" class="btn btn-success btn-mini">Voir</a>
+        <a href="{{ url_for('admin_vote_edit', voteid=vote.voteid) }}" class="btn btn-mini">Éditer</a>
+      </td>
     </tr>
     {% endfor %}
     </tbody>
index c6da367..b6ea499 100644 (file)
       <th>{% if 'user' in session and user.userid == session.user.id %}<i class="icon-user"></i>{% endif %} {{ user.username }}</th>
       {% for choice in choices %}
       {% if choice.id in user.choices %}
-      <td class="yes">OUI</td>
+      <td class="yes"><i class="icon-ok icon-white"></i></td>
       {% else %}
       <td class="no"></td>{% endif %}
       {% endfor %}
-      <td>{% if 'user' in session and user.userid == session.user.id %}<a href="{{ url_for('vote_deletechoices', idvote=vote.id, iduser=session.user.id) }}" class="btn btn-mini btn-danger" title="Supprimer">X</a>{% endif %}</td>
+      <td>{% if 'user' in session and user.userid == session.user.id %}<a href="{{ url_for('vote_deletechoices', idvote=vote.id, iduser=session.user.id) }}" class="btn btn-mini btn-danger" title="Supprimer"><i class="icon-remove icon-white"></a>{% endif %}</td>
     </tr>
   {% endif %}
   {% endfor %}