Edit vote
[cavote.git] / main.py
diff --git a/main.py b/main.py
index 4662b59..c26c2cb 100755 (executable)
--- a/main.py
+++ b/main.py
@@ -204,12 +204,67 @@ def add_vote():
             g.db.execute('insert into votes (title, description, date_begin, date_end, is_transparent, is_public, is_multiplechoice) values (?, ?, ?, ?, ?, ?, ?)',
                     [request.form['title'], request.form['description'], date_begin, date_end, transparent, public, multiplechoice])
             g.db.commit()
-            flash('New entry was successfully posted', 'info')
-            return redirect(url_for('home'))
+            vote = query_db('select * from votes where title = ? and date_begin = ? order by id desc', 
+                    [request.form['title'], date_begin], one=True) # :DEBUG:maethor:20120528: 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'))
+            else:
+                flash(u"Le vote a été créé", 'info')
+                return redirect(url_for('edit_vote', voteid=vote['id']))
         else:
             flash(u'Vous devez spécifier un titre.', 'error')
     return render_template('new_vote.html')
 
+@app.route('/votes/admin/edit/<voteid>', methods=['GET', 'POST'])
+def edit_vote(voteid):
+    if not session.get('user').get('is_admin'):
+        abort(401)
+    vote = query_db('select * from votes where id = ?', [voteid], one=True)
+    if vote is None:
+        abort(404)
+    #if request.method == 'POST':
+    # :TODO:maethor:20120528
+    choices = query_db('select * from choices where id_vote = ?', [voteid])
+    return render_template('edit_vote.html', vote=vote, choices=choices)
+
+@app.route('/votes/admin/addchoice/<voteid>', methods=['POST'])
+def add_choice(voteid):
+    if not session.get('user').get('is_admin'):
+        abort(401)
+    vote = query_db('select * from votes where id = ?', [voteid], one=True)
+    if vote is None:
+        abort(404)
+    g.db.execute('insert into choices (name, id_vote) values (?, ?)', [request.form['title'], voteid])
+    g.db.commit()
+    return redirect(url_for('edit_vote', voteid=voteid))
+
+@app.route('/votes/admin/editchoice/<voteid>/<choiceid>', methods=['POST', 'DELETE'])
+def edit_choice(voteid, choiceid):
+    if not session.get('user').get('is_admin'):
+        abort(401)
+    choice = query_db('select * from choices where id = ? and id_vote = ?', [choiceid, voteid], one=True)
+    if choice is None:
+        abort(404)
+    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:20120528: 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('edit_vote', voteid=voteid))
+
+@app.route('/votes/admin/deletechoice/<voteid>/<choiceid>')
+def delete_choice(voteid, choiceid):
+    if not session.get('user').get('is_admin'):
+        abort(401)
+    choice = query_db('select * from choices where id = ? and id_vote = ?', [choiceid, voteid], one=True)
+    if choice is None:
+        abort(404)
+    g.db.execute('delete from choices where id = ? and id_vote = ?', [choiceid, voteid])
+    g.db.commit()
+    return redirect(url_for('edit_vote', voteid=voteid))
+
 #------
 # Main