+
+ # :TODO:maethor:120529: Calculer la durée du vote (différence date_end - date_begin)
+ vote['duration'] = 15
+ group = query_db('select name from groups where id = ?', [vote['id_group']], one=True)
+ choices = query_db('select * from choices where id_vote = ?', [voteid])
+ attachments = query_db('select * from attachments where id_vote = ?', [voteid])
+ return render_template('admin_vote_edit.html', vote=vote, group=group, choices=choices, attachments=attachments)
+
+@app.route('/admin/votes/addchoice/<voteid>', methods=['POST'])
+def admin_vote_addchoice(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('admin_vote_edit', voteid=voteid))
+
+@app.route('/admin/votes/editchoice/<voteid>/<choiceid>', methods=['POST', 'DELETE'])
+def admin_vote_editchoice(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()
+ return redirect(url_for('admin_vote_edit', voteid=voteid))
+
+@app.route('/admin/votes/deletechoice/<voteid>/<choiceid>')
+def admin_vote_deletechoice(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()
+ 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'])
+def admin_vote_addattachment(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 attachments (url, id_vote) values (?, ?)', [request.form['url'], voteid])
+ g.db.commit()
+ return redirect(url_for('admin_vote_edit', voteid=voteid))
+
+@app.route('/admin/votes/deleteattachment/<voteid>/<attachmentid>')
+def admin_vote_deleteattachment(voteid, attachmentid):
+ if not session.get('user').get('is_admin'):
+ abort(401)
+ attachment = query_db('select * from attachments where id = ? and id_vote = ?', [attachmentid, voteid], one=True)
+ if attachment is None:
+ abort(404)
+ g.db.execute('delete from attachments where id = ? and id_vote = ?', [attachmentid, voteid])
+ g.db.commit()
+ return redirect(url_for('admin_vote_edit', voteid=voteid))