X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=main.py;h=dca985f7f4f35acc8239b413be23b4345466da60;hb=33346b25d357b3ad3242cfac21ab09e92cb2024b;hp=d9488c93a927d343f4e07e6d65c172ad4b57dba6;hpb=6e512c0dae7df7303cfeaabc0044ecc523311de6;p=cavote.git diff --git a/main.py b/main.py index d9488c9..dca985f 100755 --- a/main.py +++ b/main.py @@ -125,7 +125,7 @@ def user_edit(userid): g.db.execute('update users set email = ?, name = ?, organization = ? where id = ?', [request.form['email'], request.form['name'], request.form['organization'], session['user']['id']]) g.db.commit() - disconnect_user() # :TODO:maethor:120528: Maybe useless, but this is simple way to refresh session :D + disconnect_user() user = query_db('select * from users where id=?', [userid], one=True) if user is None: flash(u'Une erreur s\'est produite.', 'error') @@ -158,9 +158,20 @@ def user_password(userid): def admin_users(): if not session.get('user').get('is_admin'): abort(401) - users = query_db('select *, roles.name as rolename from (select *, name as username from users join user_role on id=id_user order by id desc) join roles on id_role=roles.id') - # :TODO:maethor:20120530: Find a way to reduce the dict - return render_template('admin_users.html', users=users) + tuples = query_db('select *, roles.name as rolename from (select *, id as userid, name as username from users join user_role on id=id_user order by id desc) join roles on id_role=roles.id') + users = dict() + for t in tuples: + if t['userid'] in users: + users[t['userid']]['roles'].append(t["rolename"]) + else: + users[t['userid']] = dict() + users[t['userid']]['userid'] = t['userid'] + users[t['userid']]['email'] = t['email'] + users[t['userid']]['username'] = t['username'] + users[t['userid']]['is_admin'] = t['is_admin'] + users[t['userid']]['roles'] = [t['rolename']] + + return render_template('admin_users.html', users=users.values()) @app.route('/admin/users/add', methods=['GET', 'POST']) def admin_user_add(): @@ -255,22 +266,50 @@ def can_see_vote(idvote, iduser=-1): vote = query_db('select * from votes where id=?', [idvote], one=True) if user is None and not vote.is_public: return False - return True # :TODO:maethor:20120529: Check others things + return True # :TODO:maethor:120529: Check others things def can_vote(idvote, iduser=-1): + # :TODO:maethor:120604: Check if user has'nt already vote if not can_see_vote(idvote, iduser): return False - return True # :TODO:maethor:20120529: Check others things + return True # :TODO:maethor:120529: Check others things -@app.route('/vote/') +@app.route('/vote/', 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, session.get('user').get('id')): - choices = query_db('select * from choices where id_vote=?', [idvote]) + choices = query_db('select name, id from choices where id_vote=?', [idvote]) + if request.method == 'POST': + if can_vote(idvote, session.get('user').get('id')): + 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: + choice = dict() + choice['id'] = t['choiceid'] + choice['name'] = t['choicename'] + users[t['userid']]['choices'].append(choice) + else: + users[t['userid']] = dict() + users[t['userid']]['userid'] = t['userid'] + users[t['userid']]['username'] = t['username'] + choice = dict() + choice['id'] = t['choiceid'] + choice['name'] = t['choicename'] + users[t['userid']]['choices'] = [choice] attachments = query_db('select * from attachments where id_vote=?', [idvote]) - return render_template('vote.html', vote=vote, attachments=attachments, choices=choices, can_vote=can_vote(idvote, session.get('user').get('id'))) + return render_template('vote.html', vote=vote, attachments=attachments, choices=choices, users=users.values(), can_vote=can_vote(idvote, session.get('user').get('id'))) flash('Vous n\'avez pas le droit de voir ce vote, désolé.') return(url_for('home')) @@ -308,7 +347,7 @@ def admin_vote_add(): [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:20120528: Bug possible car le titre n'est pas unique + [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')) @@ -337,7 +376,7 @@ def admin_vote_edit(voteid): if 'public' in request.form.keys(): public = 1 isopen = 0 - if request.form['status'] == 'Ouvert': # :TODO:maethor:20120529: Check if there is at least 2 choices before + 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]) @@ -347,11 +386,12 @@ def admin_vote_edit(voteid): else: flash(u'Vous devez spécifier un titre.', 'error') - # :TODO:maethor:20120529: Calculer la durée du vote (différence date_end - date_begin) + # :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]) - return render_template('admin_vote_edit.html', vote=vote, group=group, choices=choices) + 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/', methods=['POST']) def admin_vote_addchoice(voteid): @@ -374,7 +414,7 @@ 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:20120528: I can't find how to use it from template + 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)) @@ -390,6 +430,28 @@ def admin_vote_deletechoice(voteid, choiceid): g.db.commit() return redirect(url_for('admin_vote_edit', voteid=voteid)) +@app.route('/admin/votes/addattachment/', 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//') +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