From 61a11c1aae7ed1ded0116b2e1e1fe3323ab7db6b Mon Sep 17 00:00:00 2001 From: Julien Moutinho Date: Sat, 9 Aug 2014 06:30:20 +0200 Subject: [PATCH] modify to range voting --- .gitignore | 1 + main.py | 284 ++++++++++++++++++++++++--------- schema.sql | 73 ++++++++- schema_0.1.X_to_0.2.X.sql | 2 - schema_0.2.X_to_0.3.X.sql | 2 - settings.py.example | 30 +++- static/css/cavote.css | 169 +++++++++++++++++++- templates/admin_vote_edit.html | 26 ++- templates/admin_vote_new.html | 24 ++- templates/admin_votes.html | 6 +- templates/index.html | 2 +- templates/layout.html | 2 +- templates/vote.html | 224 +++++++++++++++++--------- templates/votes.html | 13 +- 14 files changed, 672 insertions(+), 186 deletions(-) create mode 100644 .gitignore delete mode 100644 schema_0.1.X_to_0.2.X.sql delete mode 100644 schema_0.2.X_to_0.3.X.sql diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/main.py b/main.py index 1f5766f..8093465 100755 --- a/main.py +++ b/main.py @@ -16,6 +16,8 @@ import os import hashlib import smtplib import string +import re +from collections import OrderedDict from settings import * @@ -95,8 +97,8 @@ def get_userid(): else: return user.get('id') +#@oid.loginhandler @app.route('/login', methods=['GET', 'POST']) -@oid.loginhandler def login(): if request.method == 'POST': user = valid_login(request.form['username'], request.form['password']) @@ -113,7 +115,7 @@ def login(): return redirect(url_for('home')) return render_template('login.html') -@oid.after_login +#@oid.after_login def create_or_login(resp): openid_url = resp.identity_url user = query_db('select * from users where openid = ?', [openid_url], one=True) @@ -250,7 +252,6 @@ def admin_users(): users[t['userid']]['username'] = t['username'] users[t['userid']]['is_admin'] = t['is_admin'] users[t['userid']]['groups'] = [t['groupname']] - return render_template('admin_users.html', users=users.values()) @app.route('/admin/users/add', methods=['GET', 'POST']) @@ -263,7 +264,7 @@ def admin_user_add(): if request.form['username']: if query_db('select * from users where name=?', [request.form['username']], one=True) is None: admin = 0 - if 'admin' in request.form.keys(): + if 'admin' in request.form: admin = 1 key = 'v%s' % keygen() g.db.execute('insert into users (email, openid, name, organization, password, is_admin, key) \ @@ -331,7 +332,7 @@ def admin_user_edit(iduser): if query_db('select * from users where email=? and id!=?', [request.form['email'], iduser], one=True) is None: if query_db('select * from users where name=? and id!=?', [request.form['name'], iduser], one=True) is None: admin = 0 - if 'admin' in request.form.keys(): + if 'admin' in request.form: admin = 1 g.db.execute('update users set email = ?, name = ?, organization = ?, openid= ?, is_admin = ? where id = ?', [request.form['email'], request.form['name'], request.form['organization'], request.form['openid'], admin, iduser]) @@ -424,8 +425,7 @@ def votes(votes): left join (' + max_votes + ') as max_votes on votes.id_group = max_votes.id_group' nb_votes = 'select id_vote, count(*) as nb_votes \ from (select id_user, id_vote \ - from user_choice \ - join choices on id_choice = choices.id \ + from user_vote \ group by id_user, id_vote) \ group by id_vote' basequery = 'select * from (' + basequery + ') \ @@ -443,8 +443,7 @@ def votes(votes): basequery = 'select votes.* from user_group \ join (' + basequery + ') as votes on votes.id_group = user_group.id_group \ where user_group.id_user = ?' - already_voted = 'select id_vote from user_choice \ - join choices on user_choice.id_choice = choices.id \ + already_voted = 'select id_vote from user_vote \ where id_user = ?' votes = query_db(basequery + ' and votes.id not in (' + already_voted + ') and is_terminated=0', [get_userid(), get_userid()]) else: @@ -482,8 +481,7 @@ def can_vote(idvote, iduser=-1): return False def has_voted(idvote, iduser=-1): - vote = query_db('select * from user_choice \ - join choices on id_choice=choices.id \ + vote = query_db('select * from user_vote \ where id_vote = ? and id_user = ?', [idvote, iduser], one=True) return (vote is not None) @@ -497,60 +495,151 @@ def vote(idvote): abort(404) if can_see_vote(idvote, get_userid()): choices = query_db('select name, id from choices where id_vote=?', [idvote]) + values = query_db('select weight,name from values_ where id_cardinal=? order by weight ASC', [vote['id_cardinal']]) + values.insert(0, {'weight':None,'name':u'=X̄ (Pas d’Opinion)'}) + for v in values: + v['class'] = "value_" + re.sub('[^a-zA-Z0-9-]', '_', v['name']) + weights = OrderedDict([(v['weight'], v) for v in values]) if request.method == 'POST': if can_vote(idvote, get_userid()): - if vote['is_multiplechoice'] == 0: - choice = request.form['choice'] - if choice in [str(c['id']) for c in choices] \ - and query_db('select * from choices where id = ?', [choice], one=True) is not None: - g.db.execute('insert into user_choice (id_user, id_choice) values (?, ?)', - [session.get('user').get('id'), request.form['choice']]) - g.db.commit() - else: - 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() + userid = session.get('user').get('id') if not vote['is_anonymous'] else None + # ACTION: store user's choices + for choice in choices: + if choice['name'] is None: + continue + weight = request.form.get('value-'+str(choice['id']),None) + if weight is not None and len(weight) is 0: + weight = None + if weight is not None: + weight = int(weight) + g.db.execute('insert into user_choice (id_user, id_choice, id_cardinal, weight) \ + values (?, ?, ?, ?)', [userid, choice['id'], vote['id_cardinal'], weight]) + g.db.commit() + # ACTION: randomize storage when anonymous votes + if vote['is_anonymous']: + g.db.execute('delete from user_choice_buffer_anonymous') + g.db.execute('insert into user_choice_buffer_anonymous select * \ + from user_choice where id_choice in (%s)' + % ','.join(['?'] * len(choices)) + , tuple(c['id'] for c in choices)) + g.db.execute('delete from user_choice where id_choice in (%s)' + % ','.join(['?'] * len(choices)) + , tuple(c['id'] for c in choices)) + g.db.execute('insert into user_choice select * \ + from user_choice_buffer_anonymous \ + order by random()') + g.db.execute('delete from user_choice_buffer_anonymous') + g.db.commit() + g.db.execute('insert into user_vote (id_user, id_vote) \ + values (?, ?)' + , [session.get('user').get('id'), vote['id']]) + g.db.commit() 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 order by id', [idvote]) - attachments = query_db('select * from attachments where id_vote=?', [idvote]) - tmp = query_db('select id_group, count(*) as nb from user_group where id_group = ? group by id_group', [vote['id_group']], one=True) - if tmp is None: + # ACTION: count quorum numbers + max_votes = query_db('select id_group, count(*) as nb \ + from user_group where id_group = ? \ + group by id_group', [vote['id_group']], one=True) + if max_votes is None: vote['percent'] = 0 else: - vote['max_votes'] = tmp['nb'] - tmp = query_db('select id_vote, count(*) as nb \ - from (select id_user, id_vote from user_choice \ - join choices on id_choice = choices.id \ - group by id_user, id_vote) \ - where id_vote = ? group by id_vote', [idvote], one=True) - if tmp is None: + vote['max_votes'] = max_votes['nb'] + votes = query_db('select id_vote, count(*) as nb \ + from (select id_user, id_vote from user_vote \ + group by id_user, id_vote) \ + where id_vote = ? group by id_vote', [idvote], one=True) + if votes is None: vote['percent'] = 0 vote['nb_votes'] = 0 else: - vote['nb_votes'] = tmp['nb'] + vote['nb_votes'] = votes['nb'] vote['percent'] = int((float(vote['nb_votes']) / float(vote['max_votes'])) * 100) - if query_db('select * from user_group where id_group = ? and id_user = ?', [vote['id_group'], get_userid()], one=True) and not vote['is_terminated']: + # ACTION: query users' choices joined with users' identity if not anonymous + user_choices = query_db('select user_choice.id_user as userid, users.name as username, \ + choices.id as choiceid, choices.name as choice_name, \ + user_choice.weight as weight \ + from choices \ + join user_choice on choices.id = user_choice.id_choice \ + left join users on userid = users.id \ + left join user_vote on userid = user_vote.id_user and choices.id_vote = user_vote.id_vote \ + where choices.id_vote = ? \ + order by user_vote.date,choices.id', [idvote]) + # ACTION: aggregate user choices per vote + vote['blank'] = 0 + results = OrderedDict() + for c in choices: + choice_values = [(w, { 'nb':0 + , 'idx':i + , 'name':weights[w]['name'] + , 'class':weights[w]['class'] } + ) for (i,w) in enumerate(weights, start=0)] + choice_values = OrderedDict(choice_values) + results[c['id']] = { 'id':c['id'] + , 'name':c['name'] + , 'sum':0 + , 'average':0.0 + , 'nb':0 + , 'blank':0 + , 'values_':choice_values } + for uc in user_choices: + results[uc['choiceid']]['nb'] += 1 + results[uc['choiceid']]['values_'][uc['weight']]['nb'] += 1 + if uc['weight'] is None: + results[uc['choiceid']]['blank'] += 1 + vote['blank'] += 1 + else: + results[uc['choiceid']]['sum'] += uc['weight'] + for c in results: + if results[c]['nb'] - results[c]['blank'] != 0: + results[c]['average'] = results[c]['average'] + (float(results[c]['sum']) / float(results[c]['nb'] - results[c]['blank'])) + previous_percent = 0 + for w in weights: + if results[c]['nb'] > 0: + percent = float(results[c]['values_'][w]['nb'] * 100) / results[c]['nb'] + else: + percent = 0. + results[c]['values_'][w]['percent'] = percent + results[c]['values_'][w]['previous_percent'] = previous_percent + previous_percent += percent + results[c]['values_'] = results[c]['values_'].values() + results = sorted(results.values(), key=lambda c: c['average'], reverse=True) + len_results = len(results) + if len_results % 2 == 0: + medians = results[len_results/2-1:len_results/2+1] + else: + medians = [results[len_results/2]] + results = { 'list':results + , 'medians':[m['id'] for m in medians] + , 'average':sum([r['sum'] for r in results])/len_results } + # ACTION: list user results per user + users = OrderedDict() + if vote['is_anonymous']: + user_votes = query_db('select users.name, id_user as userid \ + from user_vote \ + join users on users.id = id_user where id_vote = ?', [idvote]) + for uc in user_votes: + users[uc['userid']] = { 'username':uc['name'] + , 'choices':{} + , 'userid':uc['userid'] } + else: + for uc in user_choices: + weight = uc['weight'] + value = { 'weight':weight + , 'name':weights[weight]['name'] + , 'class':weights[weight]['class'] } + if uc['userid'] in users: + users[uc['userid']]['choices'][uc['choiceid']] = value + else: + users[uc['userid']] = { 'userid':uc['userid'] + , 'username':uc['username'] + , 'choices':{uc['choiceid']:value} } + attachments = query_db('select * from attachments where id_vote=?', [idvote]) + if query_db('select * from user_group where id_group = ? and id_user = ?' + , [vote['id_group'], get_userid()], one=True) and not vote['is_terminated']: flash(u'Ce vote vous concerne !', 'info') - return render_template('vote.html', vote=vote, attachments=attachments, choices=choices, users=users.values(), can_vote=can_vote(idvote, get_userid())) + return render_template('vote.html', vote=vote, attachments=attachments + , values=values, choices=choices, results=results, users=users.values() + , can_vote=can_vote(idvote, get_userid())) flash(u'Vous n\'avez pas le droit de voir ce vote, désolé.') return redirect(url_for('home')) @@ -558,9 +647,17 @@ def vote(idvote): def vote_deletechoices(idvote, iduser): if int(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() + vote = query_db('select votes.* from votes \ + where votes.id=?', [idvote], one=True) + if not vote['is_terminated'] and not vote['is_anonymous']: + 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() + g.db.execute('delete from user_vote where id_user = ? and id_vote \ + in (select id_vote from choices where id_vote = ?)' + , [iduser, idvote]) + g.db.commit() return redirect(url_for('vote', idvote=idvote)) #------------- @@ -579,6 +676,7 @@ def admin_votes(): def admin_vote_add(): if not session.get('user').get('is_admin'): abort(401) + cardinals= OrderedDict([(len(values), {'name':name,'values':values,'first':first}) for (name, first, values) in CARDINALS]) if request.method == 'POST': if request.form['title']: if query_db('select * from votes where title = ?', [request.form['title']], one=True) is None: @@ -586,29 +684,51 @@ def admin_vote_add(): date_end = date.today() + timedelta(days=int(request.form['days'])) transparent = 0 public = 0 - multiplechoice = 0 - if 'transparent' in request.form.keys(): + anonymous = 0 + if 'transparent' in request.form: transparent = 1 - if 'public' in request.form.keys(): + if 'public' in request.form: public = 1 - if 'multiplechoice' in request.form.keys(): - multiplechoice = 1 + if 'anonymous' in request.form: + anonymous = 1 + try: quorum = float(request.form.get('quorum')) + except ValueError: + quorum = 0 + if not (0 <= quorum and quorum <= 1): + flash(u'Une erreur est survenue !', 'error') group = query_db('select id from groups where name = ?', [request.form['group']], one=True) if group is None: group[id] = 1 + try: cardinal = int(request.form.get('cardinal')) + except ValueError: + cardinal = None + if cardinal in cardinals: + cardinal_name = cardinals[cardinal]['name'] + cardinal_values = cardinals[cardinal]['values'] + weight = cardinals[cardinal]['first'] if not cardinals[cardinal]['first'] is None else -(cardinal/2) + if query_db('select * from cardinals where id = ?', [cardinal], one=True) is None: + g.db.execute('insert into cardinals (id, name) values (?, ?)', [len(cardinal_values), cardinal_name]) + g.db.commit() + for name in cardinal_values: + g.db.execute('insert into values_ (id_cardinal, name, weight) values (?, ?, ?)' + , [cardinal, name, weight]) + g.db.commit() + weight += 1 g.db.execute('insert into votes (title, description, category, \ - date_begin, date_end, is_transparent, is_public, is_multiplechoice, id_group, id_author) \ - values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', + date_begin, date_end, quorum, is_transparent, is_public, \ + is_anonymous, id_group, id_author, id_cardinal) \ + values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', [ request.form['title'], request.form['description'], request.form['category'] - , date_begin, date_end, transparent, public, multiplechoice, group['id'], session['user']['id'] ]) + , date_begin, date_end, quorum, transparent, public, anonymous + , group['id'], session['user']['id'], cardinal ]) 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) + vote = query_db('select * from votes where title = ? and date_begin = ? order by id desc' + , [request.form['title'], date_begin], one=True) if vote is None: flash(u'Une erreur est survenue !', 'error') return redirect(url_for('home')) else: - if request.form['pattern'] in PATTERNS.keys(): + if request.form['pattern'] in PATTERNS: pattern = PATTERNS[request.form['pattern']] for choice in pattern: g.db.execute('insert into choices (name, id_vote) values (?, ?)', [choice, vote['id']]) @@ -619,8 +739,9 @@ def admin_vote_add(): flash(u'Le titre que vous avez choisi est déjà pris.', 'error') else: flash(u'Vous devez spécifier un titre.', 'error') - groups = query_db('select * from groups') - return render_template('admin_vote_new.html', groups=groups, patterns=PATTERNS) + groups = query_db('select * from groups') + return render_template('admin_vote_new.html', groups=groups, cardinals=cardinals + , quorums=QUORUMS, patterns=PATTERNS) @app.route('/admin/votes/edit/', methods=['GET', 'POST']) def admin_vote_edit(voteid): @@ -636,15 +757,17 @@ def admin_vote_edit(voteid): date_end = date_end.strftime("%Y-%m-%d") transparent = 0 public = 0 - if 'transparent' in request.form.keys(): + if 'transparent' in request.form: transparent = 1 - if 'public' in request.form.keys(): + if 'public' in request.form: public = 1 isopen = 0 isterminated = 0 if request.form['status'] == 'Ouvert': - choices = query_db('select id_vote, count(*) as nb from choices where id_vote = ? group by id_vote', [voteid], one=True) - if choices is not None and choices['nb'] >= 2: + choices = query_db('select id_vote, count(*) as nb \ + from choices where id_vote = ? \ + group by id_vote', [voteid], one=True) + if choices is not None and choices['nb'] >= 1: isopen = 1 previousvote = query_db('select id, is_open, id_group from votes where id = ?', [voteid], one=True) if previousvote is None or previousvote['is_open'] == 0: @@ -672,15 +795,15 @@ def admin_vote_edit(voteid): server.sendmail(EMAIL, [user['email']], BODY.encode('utf-8')) server.quit() else: - flash(u'Vous devez proposer au moins deux choix pour ouvrir le vote.', 'error') + flash(u'Vous devez proposer au moins un choix pour ouvrir le vote.', 'error') elif request.form['status'] == u'Terminé': isterminated = 1 if vote['is_open']: isopen = 1 - g.db.execute('update votes set title = ?, description = ?, category = ?, \ + g.db.execute('update votes set title = ?, description = ?, category = ?, quorum = ?, \ is_transparent = ?, is_public = ?, is_open = ?, is_terminated = ?, \ date_end = ?, reminder_last_days = ? where id = ?', - [ request.form['title'], request.form['description'], request.form['category'] + [ request.form['title'], request.form['description'], request.form['category'], request.form['quorum'] , transparent, public, isopen, isterminated, date_end, request.form['reminder'], voteid ]) g.db.commit() vote = query_db('select * from votes where id = ?', [voteid], one=True) @@ -690,10 +813,11 @@ def admin_vote_edit(voteid): vote['duration'] = (datetime.strptime(vote['date_end'], "%Y-%m-%d") - datetime.strptime(vote['date_begin'], "%Y-%m-%d")).days group = query_db('select name from groups where id = ?', [vote['id_group']], one=True) choices = query_db('select * from choices where id_vote = ?', [voteid]) + values_ = query_db('select * from cardinals where id = ?', [vote['id_cardinal']], one=True)['name'] attachments = query_db('select * from attachments where id_vote = ?', [voteid]) if date.today().strftime("%Y-%m-%d") > vote['date_end']: flash(u'La deadline du vote est expirée, vous devriez terminer le vote.') - return render_template('admin_vote_edit.html', vote=vote, group=group, choices=choices, attachments=attachments) + return render_template('admin_vote_edit.html', vote=vote, group=group, values_=values_, choices=choices, attachments=attachments, quorums=QUORUMS) @app.route('/admin/votes/delete/') def admin_vote_del(idvote): @@ -738,7 +862,9 @@ def admin_vote_deletechoice(voteid, choiceid): 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) + 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() diff --git a/schema.sql b/schema.sql index 95ee9e3..7884fe7 100644 --- a/schema.sql +++ b/schema.sql @@ -1,5 +1,9 @@ drop table if exists user_choice; +drop table if exists user_choice_buffer_anonymous; +drop table if exists user_vote; drop table if exists choices; +drop table if exists cardinals; +drop table if exists values_; drop table if exists attachments; drop table if exists votes; drop table if exists user_group; @@ -35,6 +39,20 @@ create table user_group ( PRIMARY KEY(id_user, id_group) ); +create table cardinals ( + id INTEGER UNIQUE NOT NULL PRIMARY KEY, + name TEXT UNIQUE NOT NULL, + first INTEGER NOT NULL +); + +create table values_ ( + name TEXT NOT NULL, + weight INTEGER NOT NULL, + id_cardinal INTEGER NOT NULL, + FOREIGN KEY(id_cardinal) REFERENCES cardinals (id) ON DELETE CASCADE, + PRIMARY KEY(id_cardinal, weight) +); + create table votes ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, @@ -42,21 +60,25 @@ create table votes ( category TEXT, date_begin INTEGER DEFAULT CURRENT_TIMESTAMP NOT NULL, date_end INTEGER NOT NULL, - is_transparent BOOLEAN DEFAULT 1 NOT NULL, + quorum FLOAT DEFAULT 1, + reminder_last_days INTEGER DEFAULT 3 NOT NULL, + is_transparent BOOLEAN DEFAULT 1 NOT NULL, is_public BOOLEAN DEFAULT 1 NOT NULL, - is_multiplechoice BOOLEAN DEFAULT 1 NOT NULL, - is_weighted BOOLEAN DEFAULT 0 NOT NULL, + is_anonymous BOOLEAN DEFAULT 1 NOT NULL, is_open BOOLEAN DEFAULT 0 NOT NULL, is_terminated BOOLEAN DEFAULT 0 NOT NULL, + is_hidden BOOLEAN DEFAULT 0 NOT NULL, id_author INTEGER DEFAULT 1 NOT NULL, id_group INTEGER DEFAULT 1 NOT NULL, + id_cardinal INTEGER NOT NULL, FOREIGN KEY(id_author) REFERENCES users (id) ON DELETE SET DEFAULT, FOREIGN KEY(id_group) REFERENCES groups (id), + FOREIGN KEY(id_cardinal) REFERENCES cardinals (id), CHECK (is_transparent IN (0, 1)), CHECK (is_public IN (0, 1)), - CHECK (is_weighted IN (0, 1)), CHECK (is_open IN (0, 1)), - CHECK (is_terminated IN (0, 1)) + CHECK (is_terminated IN (0, 1)), + CHECK (is_hidden IN (0, 1)) ); create table attachments ( @@ -75,19 +97,54 @@ create table choices ( create table user_choice ( id_user INTEGER, - id_choice INTEGER, + id_choice INTEGER NOT NULL, + id_cardinal INTEGER, weight INTEGER, FOREIGN KEY(id_user) REFERENCES users (id) ON DELETE CASCADE, FOREIGN KEY(id_choice) REFERENCES choices (id) ON DELETE CASCADE, + FOREIGN KEY(id_cardinal, weight) REFERENCES values_ (id_cardinal, weight) ON DELETE CASCADE, PRIMARY KEY(id_user, id_choice) ); +create table user_choice_buffer_anonymous ( + -- NOTE: same table structure as user_choice, used to randomize insertion per vote + id_user INTEGER, + id_choice INTEGER NOT NULL, + id_cardinal INTEGER, + weight INTEGER, + FOREIGN KEY(id_user) REFERENCES users (id) ON DELETE CASCADE, + FOREIGN KEY(id_choice) REFERENCES choices (id) ON DELETE CASCADE, + FOREIGN KEY(id_cardinal, weight) REFERENCES values_ (id_cardinal, weight) ON DELETE CASCADE, + PRIMARY KEY(id_user, id_choice) +); + +create table user_vote ( + date INTEGER DEFAULT CURRENT_TIMESTAMP NOT NULL, + id_user INTEGER NOT NULL, + id_vote INTEGER NOT NULL, + FOREIGN KEY(id_user) REFERENCES users (id) ON DELETE CASCADE, + FOREIGN KEY(id_vote) REFERENCES votes (id) ON DELETE CASCADE, + PRIMARY KEY(id_user, id_vote) +); + -- Test data INSERT INTO users (id, email, password, openid, name, organization, is_admin, key) -VALUES (1, "admin@admin.fr", "", "", "Toto (admin) Tata", "World corp", 1, "victory"); +VALUES (1, "admin+cavote+1@localhost.localdomain", "", "", "admin1", "Organisation", 1, "victory"); +INSERT INTO users (id, email, password, openid, name, organization, is_admin, key) +VALUES (2, "admin+cavote+2@localhost.localdomain", "", "", "admin2", "Organisation", 1, "victory"); +INSERT INTO users (id, email, password, openid, name, organization, is_admin, key) +VALUES (3, "admin+cavote+3@localhost.localdomain", "", "", "admin3", "Organisation", 1, "victory"); +INSERT INTO users (id, email, password, openid, name, organization, is_admin, key) +VALUES (4, "admin+cavote+4@localhost.localdomain", "", "", "admin4", "Organisation", 1, "victory"); +INSERT INTO users (id, email, password, openid, name, organization, is_admin, key) +VALUES (4, "admin+cavote+5@localhost.localdomain", "", "", "admin5", "Organisation", 1, "victory"); -- to login, go to /login/1/victory -INSERT INTO groups (id, name, system) VALUES (1, "Tous", 1); +INSERT INTO groups (id, name, system) VALUES (1, "Tout le monde", 1); INSERT INTO groups (name) VALUES ("CA"); INSERT INTO groups (name) VALUES ("Membres"); INSERT INTO user_group (id_user, id_group) VALUES(1, 1); +INSERT INTO user_group (id_user, id_group) VALUES(2, 1); +INSERT INTO user_group (id_user, id_group) VALUES(3, 1); +INSERT INTO user_group (id_user, id_group) VALUES(4, 1); +INSERT INTO user_group (id_user, id_group) VALUES(5, 1); diff --git a/schema_0.1.X_to_0.2.X.sql b/schema_0.1.X_to_0.2.X.sql deleted file mode 100644 index 32cb736..0000000 --- a/schema_0.1.X_to_0.2.X.sql +++ /dev/null @@ -1,2 +0,0 @@ -ALTER TABLE votes -ADD is_hidden BOOLEAN DEFAULT 0 NOT NULL; diff --git a/schema_0.2.X_to_0.3.X.sql b/schema_0.2.X_to_0.3.X.sql deleted file mode 100644 index 1ffa657..0000000 --- a/schema_0.2.X_to_0.3.X.sql +++ /dev/null @@ -1,2 +0,0 @@ -ALTER TABLE votes -ADD reminder_last_days INTEGER DEFAULT 3 NOT NULL; diff --git a/settings.py.example b/settings.py.example index 7eaa906..bdbc83d 100644 --- a/settings.py.example +++ b/settings.py.example @@ -5,10 +5,32 @@ DATABASE = '/tmp/cavote.db' PASSWD_SALT = 'change this value to some random chars!' SECRET_KEY = '{J@uRKO,xO-PK7B,jF?>iHbxLasF9s#zjOoy=+:' DEBUG = True -TITLE = u"Cavote FFDN" -EMAIL = '"' + TITLE + '"' + ' <' + u"cavote@ffdn.org" + '>' -VERSION = "cavote 0.3.0" +TITLE = u"Cavote" +EMAIL = '"' + TITLE + '"' + ' <' + u"contact+cavote@localhost.localdomain" + '>' +VERSION = "cavote 0.4.0" SMTP_SERVER = "127.0.0.1" -PATTERNS = {u'Oui/Non': [u'Oui', u'Non'], u'Oui/Non/Blanc': [u'Oui', u'Non', u'Blanc'], u'Oui/Non/Peut-être': [u'Oui', u'Non', u'Peut-être']} +PATTERNS = \ + { u'Oui/Non': [u'Oui', u'Non'] \ + } +CARDINALS = \ + [ (u'Sans moi/Avec moi' + , 0, [u'±0 (Sans moi)', u'+1 (Avec moi)']) \ + , (u'Hostile/Indifférent/Favorable' + , -1, [u'-1 (Hostile)', u'±0 (Indifférent)', u'+1 (Favorable)']) \ + , (u'Très Hostile/Hostile/Indifférent/Favorable/Très Favorable' + , -2, [u'-2 (Très Hostile)', u'-1 (Hostile)', u'±0 (Indifférent)', u'+1 (Favorable)', u'+2 (Très Favorable)']) \ + ] +QUORUMS = \ + [ 0 + , 0.1 + , 0.25 + , 0.333 + , 0.5 + , 0.666 + , 0.75 + , 0.9 + , 1 + ] BABEL_DEFAULT_LOCALE = 'fr' BABEL_DEFAULT_TIMEZONE = 'Europe/Paris' +VOTE_URL = "http://localhost:5000/vote/%d" diff --git a/static/css/cavote.css b/static/css/cavote.css index ac17021..8fc25a2 100644 --- a/static/css/cavote.css +++ b/static/css/cavote.css @@ -1,13 +1,27 @@ -.table-votes th { +.page-header { + padding-bottom:0; + margin-top:9px; + } + +.table-votes thead th { text-align: right; + vertical-align: middle; +} +.table-votes th.choice-name { + text-align: center; } .table-votes input { margin: 0; } +.row { + margin-left:0; + } + .table-votes td { text-align: center; + vertical-align: middle; } .table-votes .yes { @@ -32,3 +46,156 @@ .table-votes tr:hover .no { background-color: #C73F38; /* red */ } + +.table-votes input, +.table-votes textarea, +.table-votes select, +.table-votes .uneditable-input { + width:auto; + margin-bottom:0; + } + +.table-votes td { + font-family:sans-serif; + font-size:12px; + } +.table-votes .value_black { + background-color:black !important; + } +.value__X___Pas_d_Opinion_ { + background-color:white !important; + color:black !important; + text-shadow: 0 0 2px #ccc !important; + fill:white; + } +.results .value__X___Pas_d_Opinion_ + text { + fill:black; + } +.table-votes *[class^="value_"] { + color:white; + text-shadow: 0 0 2px #000; + } +.cardinal-2 .value__0__Sans_moi_ { + background-color:#B4B4B4 !important; + fill:#B4B4B4; + } +.cardinal-2 .value__1__Avec_moi_ { + background-color:#20B756 !important; + fill:#20B756; + } +.cardinal-2 .value__1__Favorable_ { + background-color:#20B756 !important; + fill:#20B756; + } +.cardinal-3 .value_-1__Hostile_ { + background-color:#DB4343 !important; + fill:#DB4343; + } +.cardinal-3 .value__0__Indiff_rent_ { + background-color:#B4B4B4 !important; + fill:#B4B4B4; + } +.cardinal-3 .value__1__Favorable_ { + background-color:#20B756 !important; + fill:#20B756; + } +.cardinal-5 .value_-2__Tr_s_Hostile_ { + background-color:#DB4343 !important; + fill:#DB4343; + } +.cardinal-5 .value_-1__Hostile_ { + background-color:#E49F37 !important; + fill:#E49F37; + } +.cardinal-5 .value__0__Indiff_rent_ { + background-color:#B4B4B4 !important; + fill:#B4B4B4; + } +.cardinal-5 .value__1__Favorable_ { + background-color:#B1D039 !important; + fill:#B1D039; + } +.cardinal-5 .value__2__Tr_s_Favorable_ { + background-color:#20B756 !important; + fill:#20B756; + } +.container, +.navbar-fixed-top .container, +.navbar-fixed-bottom .container { + width:auto; + } +.span3 { + width:24em; + } +.span8 { + width:625px; + } +.span8 svg { + margin:0; + padding:0; + } + +.results rect { + //fill: steelblue; + } +.results g.value_-2 rect { + fill: #DB4343; + } +.results g.value_-1 rect { + fill: #E49F37; + } +.results g.value__0 rect { + fill: #B4B4B4; + } +.results g.value__1 rect { + fill: #B1D039; + } +.results g.value__2 rect { + fill: #20B756; + } +.results text.choice { + fill: black; + } +.results text.choice-legend { + fill: black; + font-weight:bold; + } +.results text.choice-legend-item { + fill: black; + } +.results text.choice-sum { + fill: black; + font-weight:bold; + } +.results text.choice-average { + fill: black; + font-weight:bold; + } +.results text.choice-sum-legend { + fill: blue; + font-weight:bold; + } +.results text.choice-average-legend { + fill: blue; + font-weight:bold; + } +.results text.choice-median { + fill: blue; + font-weight:bold; + } +.results text { + fill: white; + font: 10px sans-serif; + text-anchor: end; + } + +.results .axis text { + fill: black; + font: 10px sans-serif; + } +.results .axis path, +.results .axis line { + fill: none; + shape-rendering: crispEdges; + stroke: #000; + } diff --git a/templates/admin_vote_edit.html b/templates/admin_vote_edit.html index 42eef08..1b0c3eb 100644 --- a/templates/admin_vote_edit.html +++ b/templates/admin_vote_edit.html @@ -31,6 +31,20 @@ +
+ +
+ +
+
@@ -92,12 +106,20 @@ {% if not vote.is_terminated == 1 %} {% endif %}
+
+ +
+ +
+
diff --git a/templates/admin_vote_new.html b/templates/admin_vote_new.html index 8ae0b9a..54fb294 100644 --- a/templates/admin_vote_new.html +++ b/templates/admin_vote_new.html @@ -24,6 +24,16 @@ +
+ +
+ +
+
@@ -61,11 +71,21 @@ Le vote est-il visible par tous ?
+
+ +
+ +
+
diff --git a/templates/admin_votes.html b/templates/admin_votes.html index 40358ab..4591ad3 100644 --- a/templates/admin_votes.html +++ b/templates/admin_votes.html @@ -28,8 +28,8 @@ {{ vote.category }} {% if vote.is_transparent %}transparent{% endif %} + {% if vote.is_anonymous %}anonymous{% endif %} {% if vote.is_public %}public{% endif %} - {% if vote.is_multiplechoice %}multiple{% endif %} Voir @@ -57,9 +57,9 @@

Légende :

    -
  • transparent : Les utilisateurs peuvent voir les votes des autres
  • +
  • transparent : Les votants peuvent voir les votes des autres
  • +
  • anonymous : Les suffrages du vote sont déliés de l’identité de leur votant
  • public : Tout le monde peut voir le vote
  • -
  • multiple : Les utilisateurs peuvent effectuer plusieurs choix

{% endif %} diff --git a/templates/index.html b/templates/index.html index 6804c85..45d785f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,5 +1,5 @@ {% extends "layout.html" %} {% block body %} {% if error %}

Error: {{ error }}{% endif %} -

Bienvenue sur cavote, l'outil de vote de la Fédération FDN !

+

Bienvenue sur Cavote, un outil de vote convivial

{% endblock %} diff --git a/templates/layout.html b/templates/layout.html index fff1187..7e6aea8 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -53,7 +53,7 @@
-

Outil de vote du CA FFDN {% block subtitle %}{% endblock %}

+

{% block subtitle %}{% endblock %}

{% with messages = get_flashed_messages(with_categories="true") %} {% if messages %} {% for category, message in messages %} diff --git a/templates/vote.html b/templates/vote.html index e3ecbfc..a832684 100644 --- a/templates/vote.html +++ b/templates/vote.html @@ -1,37 +1,176 @@ {% extends "layout.html" %} +{% block subtitle %}{{vote.title}}{% endblock %} {% block body %}
+
+ +
+
+ {{ vote.nb_votes }} vote{{ 's' if vote.nb_votes > 1 else '' }} / {{ vote.max_votes }} ({{ vote.percent }}%)
+
+
+
Publié par
{{ vote.author }} +
Début le
{{ vote.date_begin }} +
Fin le
{{ vote.date_end }} +
Groupe
{{ vote.groupname }} +
Catégorie
{{ vote.category }} +
Quorum
{{ vote.quorum * 100 }}% +
+
+ {% if vote.description %} +
Description :
{{ vote.description }} + {% endif %} + {% if attachments %} +
Documents :
+ + {% endif %} +
+
+ +{% if vote.is_transparent or (session.user and user.userid == session.user.id )%} +
+ + + + Légende : + + {% for value in values %} + + + + {{ value.name }} + + {% endfor %} + + somme↓ + + + moyenne↓ + + {% for choice in results.list %} + + {{ choice.name }} + + {% set choice_loop = loop %} + {% for value in choice.values_ %} + + {% if value.percent > 0 %} + + {{ value.percent|int }}% + {% endif %} + + {% endfor %} + + {{ choice.sum }} + + + {{ "%.2f"|format(choice.average) }} + + {% if loop.length > 1 and choice.id in results.medians %} + + ← médiane + + {%endif%} + {% endfor %} + + + + 0% + + + + 25% + + + + 50% + + + + 75% + + + + 100% + + + + +
+{% endif %} +
- + +{% if vote.is_anonymous %} +
Ce vote est anonymisé, les suffrages ne sont ni visibles ni modifiables.
+{% endif %} {% if not vote.is_transparent %} -
Ce sondage n'est pas transparent, vous ne pouvez pas voir les votes des autres.
+
Ce vote n’est pas transparent, vous ne pouvez pas voir les votes des autres.
{% endif %} - +{% if (vote['is_anonymous'] and can_vote) or not vote['is_anonymous'] %} +
{% for choice in choices %} - + {% endfor %} - {% if 'user' in session %} + {% if 'user' in session and (can_vote or (not vote['is_terminated'] and not vote['is_anonymous'])) %} {% endif %} + {% if vote.is_transparent %} + + {% endif %} + + {% if can_vote %} + + + + {% for choice in choices %} + + {% endfor %} + + + + {% endif %} + {% if not vote['is_anonymous'] %} {% for user in users %} {% if vote.is_transparent or (session.user and user.userid == session.user.id )%} {% for choice in choices %} - {% if choice.id in user.choices %} - - {% else %} - {% endif %} + {% endfor %} - {% if 'user' in session %} + {% if 'user' in session and (can_vote or (not vote['is_terminated'] and not vote['is_anonymous'])) %} {% endif %} {% endfor %} - - {% if can_vote %} - - - - {% if vote.is_multiplechoice %} - {% for choice in choices %} - - {% endfor %} - {% else %} - {% for choice in choices %} - - {% endfor %} - {% endif %} - - - - {% endif %} - - - {% if vote.is_transparent %} - - - {% for choice in choices %} - - {% endfor %} - {% if 'user' in session %} - - {% endif %} - {% endif %} -
{{ choice.name }}{{ choice.name }}
+ +
{% if 'user' in session and user.userid == session.user.id %}{% endif %} {{ user.username }}{{ user.choices[choice.id].name }}{% if user.userid == session.user.id %}
Somme{{ choice.nb }}
-
- -
-

Informations

-{% if vote.nb_votes == 0 %} -
-
{{ vote.nb_votes }} / {{ vote.max_votes }}
-
{% else %} -
-
{{ vote.nb_votes }} / {{ vote.max_votes }}
-
+{% if not can_vote %} +
Vous avez déjà voté.
+{% endif %} {% endif %} -
-
Publié par
{{ vote.author }} -
Début le
{{ vote.date_begin }} -
Deadline le
{{ vote.date_end }} -
Groupe
{{ vote.groupname }} -
Catégorie
{{ vote.category }} -
-
- {% if vote.description %} -
Description :
{{ vote.description }} - {% endif %} - {% if attachments %} -
Documents :
- - {% endif %} -
diff --git a/templates/votes.html b/templates/votes.html index ddb3ce0..7b01e67 100644 --- a/templates/votes.html +++ b/templates/votes.html @@ -10,20 +10,15 @@ Liste des votes

{{ vote.title }}

- {% if vote.nb_votes == 0 %} -
-
{{ vote.nb_votes }} / {{ vote.max_votes }}
+
+
+ {{ vote.nb_votes }} vote{{ 's' if vote.nb_votes > 1 else '' }} / {{ vote.max_votes }} ({{ vote.percent }}%)
- {% else %} -
-
{{ vote.nb_votes }} / {{ vote.max_votes }}
-
- {% endif %}
-

Deadline : {{ vote.date_end }}

+

Échéance : {{ vote.date_end }}

Groupe : {{ vote.groupname }}

Categorie : {{ vote.category }}

-- 2.20.1