+def can_see_vote(idvote, iduser=-1):
+ vote = query_db('select * from votes where id=?', [idvote], one=True)
+ if vote is None:
+ abort(404)
+ 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)
+ 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)
+ return False
+
+def has_voted(idvote, iduser=-1):
+ vote = query_db('select * from user_choice join choices on id_choice=choices.id where id_vote = ? and id_user = ?', [idvote, iduser], one=True)
+ return (vote is not None)
+
+@app.route('/vote/<idvote>', methods=['GET', 'POST'])
+def vote(idvote):
+ vote = query_db('select *, roles.name as rolename from votes join roles on roles.id=votes.id_role where votes.id=?', [idvote], one=True)
+ if vote is None:
+ abort(404)
+ if can_see_vote(idvote, get_userid()):
+ if request.method == 'POST':
+ if can_vote(idvote, get_userid()):
+ choices = query_db('select name, id from choices where id_vote=?', [idvote])
+ for choice in choices:
+ if str(choice['id']) in request.form.keys():
+ g.db.execute('insert into user_choice (id_user, id_choice) values (?, ?)',
+ [session.get('user').get('id'), choice['id']])
+ g.db.commit()
+ if vote['is_multiplechoice'] == 0:
+ break
+ else:
+ abort(401)
+ tuples = query_db('select choiceid, choicename, users.id as userid, users.name as username from (select choices.id as choiceid, choices.name as choicename, id_user as userid from choices join user_choice on choices.id = user_choice.id_choice where id_vote = ?) join users on userid = users.id', [idvote])
+ users = dict()
+ for t in tuples:
+ if t['userid'] in users:
+ users[t['userid']]['choices'].append(t['choiceid'])
+ else:
+ users[t['userid']] = dict()
+ users[t['userid']]['userid'] = t['userid']
+ users[t['userid']]['username'] = t['username']
+ users[t['userid']]['choices'] = [t['choiceid']]
+ choices = query_db('select choices.name, choices.id, choices.name, choices.id_vote, count(id_choice) as nb from choices left join user_choice on id_choice = choices.id where id_vote = ? group by id_choice, name, id_vote', [idvote])
+ attachments = query_db('select * from attachments where id_vote=?', [idvote])
+ 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'))
+
+@app.route('/vote/deletechoices/<idvote>/<iduser>')
+def vote_deletechoices(idvote):
+ if (not has_voted(idvote, get_userid())) or iduser != get_userid():
+ abort(401)
+ g.db.execute('delete from user_choice where id_user = ? and id_choice in (select id from choices where id_vote = ?)',
+ [iduser, idvote])
+ g.db.commit()
+ return redirect(url_for('vote', idvote=idvote))
+
+#-------------
+# Votes admin
+
+@app.route('/admin/votes/list')
+def admin_votes():
+ if not session.get('user').get('is_admin'):
+ abort(401)
+ votes = query_db('select *, roles.name as rolename from votes join roles on roles.id=votes.id_role order by id desc')
+ return render_template('admin_votes.html', votes=votes)
+
+@app.route('/admin/votes/add', methods=['GET', 'POST'])
+def admin_vote_add():
+ if not session.get('user').get('is_admin'):
+ 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
+ role = query_db('select id from roles where name = ?', [request.form['role']], one=True)
+ if role is None:
+ role[id] = 1
+ g.db.execute('insert into votes (title, description, category, date_begin, date_end, is_transparent, is_public, is_multiplechoice, id_role, id_author) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
+ [request.form['title'], request.form['description'], request.form['category'], date_begin, date_end, transparent, public, multiplechoice, role['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'))
+ else:
+ flash(u"Le vote a été créé", 'info')
+ return redirect(url_for('admin_vote_edit', voteid=vote['id']))
+ else:
+ flash(u'Vous devez spécifier un titre.', 'error')
+ groups = query_db('select * from roles')
+ return render_template('admin_vote_new.html', groups=groups)
+
+@app.route('/admin/votes/edit/<voteid>', methods=['GET', 'POST'])
+def admin_vote_edit(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':
+ if request.form['title']:
+ # :TODO:maethor:120529: Calculer date_begin pour pouvoir y ajouter duration et obtenir date_end
+ transparent = 0
+ public = 0
+ if 'transparent' in request.form.keys():
+ transparent = 1
+ if 'public' in request.form.keys():
+ public = 1
+ isopen = 0
+ if request.form['status'] == 'Ouvert': # :TODO:maethor:120529: Check if there is at least 2 choices before
+ isopen = 1
+ 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()
+ vote = query_db('select * from votes where id = ?', [voteid], one=True)
+ flash(u"Le vote a bien été mis à jour.", "success")
+ else:
+ flash(u'Vous devez spécifier un titre.', 'error')
+
+ # :TODO:maethor:120529: Calculer la durée du vote (différence date_end - date_begin)
+ vote['duration'] = 15
+ group = query_db('select name from roles where id = ?', [vote['id_role']], 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()
+ 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>')
+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()
+ 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))
+
+#------
+# Main