From c99b9d04a42f493cf7b8bf1e81022bb65e2b939b Mon Sep 17 00:00:00 2001 From: Guillaume Subiron Date: Tue, 29 May 2012 12:03:22 +0200 Subject: [PATCH] Admins can edit votes --- main.py | 37 +++++++++++++++++++++++++++++++------ schema.sql | 13 +++++++++++-- templates/edit_vote.html | 37 +++++++++++++++++-------------------- templates/login.html | 2 +- templates/new_vote.html | 6 +++--- 5 files changed, 63 insertions(+), 32 deletions(-) diff --git a/main.py b/main.py index c26c2cb..bdbd032 100755 --- a/main.py +++ b/main.py @@ -201,8 +201,11 @@ def add_vote(): public = 1 if 'multiplechoice' in request.form.keys(): multiplechoice = 1 - 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]) + 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:20120528: Bug possible car le titre n'est pas unique @@ -214,7 +217,8 @@ def add_vote(): 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') + groups = query_db('select * from roles') + return render_template('new_vote.html', groups=groups) @app.route('/votes/admin/edit/', methods=['GET', 'POST']) def edit_vote(voteid): @@ -223,10 +227,31 @@ def edit_vote(voteid): vote = query_db('select * from votes where id = ?', [voteid], one=True) if vote is None: abort(404) - #if request.method == 'POST': - # :TODO:maethor:20120528 + 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': + 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:20120529: 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('edit_vote.html', vote=vote, choices=choices) + return render_template('edit_vote.html', vote=vote, group=group, choices=choices) @app.route('/votes/admin/addchoice/', methods=['POST']) def add_choice(voteid): diff --git a/schema.sql b/schema.sql index 5529a31..39f1170 100644 --- a/schema.sql +++ b/schema.sql @@ -1,5 +1,6 @@ drop table if exists choices; drop table if exists votes; +drop table if exists roles; drop table if exists users; create table users ( @@ -12,6 +13,11 @@ create table users ( key TEXT ); +create table roles ( + id INTEGER primary key autoincrement, + name TEXT +); + create table votes ( id INTEGER primary key autoincrement, title TEXT not null, @@ -25,9 +31,9 @@ create table votes ( is_weighted INTEGER default 0 not null, is_open INTEGER default 0 not null, id_author INTEGER, -- :COMMENT:maethor:120528: not null ? - --id_role INTEGER, + id_role INTEGER default 1 not null, FOREIGN KEY(id_author) REFERENCES users(id) - --FOREIGN KEY(id_role) REFERENCES role(id) + FOREIGN KEY(id_role) REFERENCES roles(id) ); create table choices ( @@ -40,4 +46,7 @@ create table choices ( -- Test data insert into users (email, password, name, organization, is_admin, key) values ("admin@admin.fr", "admin", "Toto (admin) Tata", "World corp", 1, "test"); +insert into roles (id, name) values (1, "Tous"); +insert into roles (name) values ("CA"); +insert into roles (name) values ("Members"); diff --git a/templates/edit_vote.html b/templates/edit_vote.html index 4319631..881b6d7 100644 --- a/templates/edit_vote.html +++ b/templates/edit_vote.html @@ -15,7 +15,7 @@
- +
@@ -28,60 +28,57 @@
- +

Groupe d'utilisateur concernés par le vote et ayant le droit de voter

-
- - -
-
- + Options -
-
-
+ Du {{ vote.date_begin }} au {{ vote.date_end }}
+
+
+
+
+
+ + Options +
+
+
- {% if not vote.is_open %} - {% endif %}
diff --git a/templates/login.html b/templates/login.html index 85d5b07..332195e 100644 --- a/templates/login.html +++ b/templates/login.html @@ -1,7 +1,7 @@ {% extends "layout.html" %} {% block body %}
-
+
Connexion diff --git a/templates/new_vote.html b/templates/new_vote.html index 0adca60..dd24bce 100644 --- a/templates/new_vote.html +++ b/templates/new_vote.html @@ -28,9 +28,9 @@

Groupe d'utilisateur concernés par le vote et ayant le droit de voter

-- 2.20.1