user = session.get('user')
if user is None:
return -1
- elif not user.get('id') > 0:
+ elif user.get('id') < 0:
return -1
else:
return user.get('id')
def user(userid):
if int(userid) != get_userid():
abort(401)
- groups = query_db('select * from roles join user_role on id=id_role where id_user = ?', userid)
+ groups = query_db('select * from groups join user_group on id=id_group where id_user = ?', userid)
return render_template('user.html', groups=groups)
@app.route('/user/settings/<userid>', methods=['GET', 'POST'])
def admin_users():
if not session.get('user').get('is_admin'):
abort(401)
- 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')
+ tuples = query_db('select *, groups.name as groupname from (select *, id as userid, name as username from users join user_group on id=id_user order by id desc) join groups on id_group=groups.id')
users = dict()
for t in tuples:
if t['userid'] in users:
- users[t['userid']]['roles'].append(t["rolename"])
+ users[t['userid']]['groups'].append(t["groupname"])
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']]
+ users[t['userid']]['groups'] = [t['groupname']]
return render_template('admin_users.html', users=users.values())
g.db.commit()
user = query_db('select * from users where email = ?', [request.form["email"]], one=True)
if user:
- for role in request.form.getlist('roles'):
- # :TODO:maethor:120528: Check if this role exist
- if query_db('select id from roles where id = ?', role, one=True) is None:
+ for group in request.form.getlist('groups'):
+ # :TODO:maethor:120528: Check if this group exist
+ if query_db('select id from groups where id = ?', group, one=True) is None:
abort(401)
- g.db.execute('insert into user_role values (?, ?)', [user['id'], role])
+ g.db.execute('insert into user_group values (?, ?)', [user['id'], group])
g.db.commit()
# :TODO:maethor:120528: Send mail
flash(u'Le nouvel utilisateur a été créé avec succès', 'success')
flash(u'Une erreur s\'est produite.', 'error')
else:
flash(u"Vous devez spécifier une adresse email.", 'error')
- groups = query_db('select * from roles where system=0')
+ groups = query_db('select * from groups where system=0')
return render_template('admin_user_new.html', groups=groups)
#-------------
# Roles admin
-@app.route('/admin/roles')
-def admin_roles():
+@app.route('/admin/groups')
+def admin_groups():
if not session.get('user').get('is_admin'):
abort(401)
- roles = query_db('select * from roles')
- return render_template('admin_roles.html', roles=roles)
+ groups = query_db('select * from groups')
+ return render_template('admin_groups.html', groups=groups)
-@app.route('/admin/roles/add', methods=['POST'])
-def admin_role_add():
+@app.route('/admin/groups/add', methods=['POST'])
+def admin_group_add():
if not session.get('user').get('is_admin'):
abort(401)
if request.method == 'POST':
if request.form['name']:
- g.db.execute('insert into roles (name) values (?)', [request.form['name']])
+ g.db.execute('insert into groups (name) values (?)', [request.form['name']])
g.db.commit()
else:
flash(u"Vous devez spécifier un nom.", "error")
- return redirect(url_for('admin_roles'))
+ return redirect(url_for('admin_groups'))
-@app.route('/admin/roles/delete/<idrole>')
-def admin_role_del(idrole):
+@app.route('/admin/groups/delete/<idgroup>')
+def admin_group_del(idgroup):
if not session.get('user').get('is_admin'):
abort(401)
- role = query_db('select * from roles where id = ?', [idrole], one=True)
- if role is None:
+ group = query_db('select * from groups where id = ?', [idgroup], one=True)
+ if group is None:
abort(404)
- if role['system']:
+ if group['system']:
abort(401)
- g.db.execute('delete from roles where id = ?', [idrole])
+ g.db.execute('delete from groups where id = ?', [idgroup])
g.db.commit()
- return redirect(url_for('admin_roles'))
+ return redirect(url_for('admin_groups'))
#------------
# Votes list
def votes(votes):
today = date.today()
active_button = votes
- basequery = 'select *, roles.name as rolename from votes join roles on roles.id=votes.id_role where is_open=1'
+ basequery = 'select *, votes.id as voteid, groups.name as groupname from votes join groups on groups.id = id_group where is_open=1'
if votes == 'all':
votes = query_db(basequery + ' order by id desc')
elif votes == 'archive':
@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)
+ vote = query_db('select votes.*, groups.name as groupname from votes join groups on groups.id=votes.id_group where votes.id=?', [idvote], one=True)
if vote is None:
abort(404)
if can_see_vote(idvote, get_userid()):
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])
+ 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])
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():
+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])
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')
+ votes = query_db('select *, votes.id as voteid, groups.name as groupname from votes join groups on groups.id=votes.id_group order by id desc')
return render_template('admin_votes.html', votes=votes)
@app.route('/admin/votes/add', methods=['GET', 'POST'])
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']])
+ group = query_db('select id from groups where name = ?', [request.form['group']], one=True)
+ if group is None:
+ group[id] = 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
+ [request.form['title'], request.form['description'], request.form['category'], date_begin, date_end, transparent, public, multiplechoice, group['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
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')
+ groups = query_db('select * from groups')
return render_template('admin_vote_new.html', groups=groups)
@app.route('/admin/votes/edit/<voteid>', methods=['GET', 'POST'])
# :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)
+ 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)
drop table if exists choices;
drop table if exists attachments;
drop table if exists votes;
-drop table if exists user_role;
-drop table if exists roles;
+drop table if exists user_group;
+drop table if exists groups;
drop table if exists users;
create table users (
key TEXT
);
-create table roles (
+create table groups (
id INTEGER primary key autoincrement,
name TEXT,
system INTEGER default 0 not null
);
-create table user_role (
+create table user_group (
id_user INTEGER,
- id_role INTEGER,
+ id_group INTEGER,
FOREIGN KEY(id_user) REFERENCES users(id),
- FOREIGN KEY(id_role) REFERENCES roles(id),
- PRIMARY KEY(id_user, id_role)
+ FOREIGN KEY(id_group) REFERENCES groups(id),
+ PRIMARY KEY(id_user, id_group)
);
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 default 1 not null,
+ id_group INTEGER default 1 not null,
FOREIGN KEY(id_author) REFERENCES users(id)
- FOREIGN KEY(id_role) REFERENCES roles(id)
+ FOREIGN KEY(id_group) REFERENCES groups(id)
);
create table attachments (
-- Test data
insert into users (email, password, name, organization, is_admin, key) values ("admin@admin.fr", "d033e22ae348aeb5660fc2140aec35850c4da997", "Toto (admin) Tata", "World corp", 1, "test"); -- mdp = admin
-insert into roles (id, name, system) values (1, "Tous", 1);
-insert into roles (name) values ("CA");
-insert into roles (name) values ("Members");
+insert into groups (id, name, system) values (1, "Tous", 1);
+insert into groups (name) values ("CA");
+insert into groups (name) values ("Members");