From 1dfbd755dae26562b182d81f8818c5c3dcc6694b Mon Sep 17 00:00:00 2001 From: Guillaume Subiron Date: Mon, 4 Jun 2012 16:13:33 +0200 Subject: [PATCH] Renamed role table into groups, and debug --- main.py | 76 +++++++++---------- schema.sql | 24 +++--- .../{admin_roles.html => admin_groups.html} | 12 +-- templates/admin_user_new.html | 4 +- templates/admin_users.html | 2 +- templates/admin_vote_edit.html | 2 +- templates/admin_vote_new.html | 4 +- templates/admin_votes.html | 4 +- templates/layout.html | 2 +- templates/vote.html | 4 +- templates/votes.html | 4 +- 11 files changed, 69 insertions(+), 69 deletions(-) rename templates/{admin_roles.html => admin_groups.html} (65%) diff --git a/main.py b/main.py index e2e7d81..b3f50ba 100755 --- a/main.py +++ b/main.py @@ -65,7 +65,7 @@ def get_userid(): 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') @@ -121,7 +121,7 @@ def login_key(userid, key): 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/', methods=['GET', 'POST']) @@ -167,18 +167,18 @@ def user_password(userid): 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()) @@ -198,11 +198,11 @@ def admin_user_add(): 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') @@ -211,43 +211,43 @@ def admin_user_add(): 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/') -def admin_role_del(idrole): +@app.route('/admin/groups/delete/') +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 @@ -256,7 +256,7 @@ def admin_role_del(idrole): 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': @@ -293,7 +293,7 @@ def has_voted(idvote, iduser=-1): @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) + 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()): @@ -319,15 +319,15 @@ def vote(idvote): 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//') -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]) @@ -341,7 +341,7 @@ def vote_deletechoices(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']) @@ -361,11 +361,11 @@ def admin_vote_add(): 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 @@ -377,7 +377,7 @@ def admin_vote_add(): 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/', methods=['GET', 'POST']) @@ -409,7 +409,7 @@ def admin_vote_edit(voteid): # :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) diff --git a/schema.sql b/schema.sql index 891cf76..7f6a0bc 100644 --- a/schema.sql +++ b/schema.sql @@ -2,8 +2,8 @@ drop table if exists user_choice; 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 ( @@ -16,18 +16,18 @@ 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 ( @@ -43,9 +43,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 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 ( @@ -74,7 +74,7 @@ create table user_choice ( -- 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"); diff --git a/templates/admin_roles.html b/templates/admin_groups.html similarity index 65% rename from templates/admin_roles.html rename to templates/admin_groups.html index 7772337..966372e 100644 --- a/templates/admin_roles.html +++ b/templates/admin_groups.html @@ -5,8 +5,8 @@

Groupes


- {% if not roles %} -
Il n'y a aucun role.
+ {% if not groups %} +
Il n'y a aucun group.
{% else %} @@ -18,12 +18,12 @@ - {% for role in roles %} + {% for group in groups %} - + - + {% endfor %} @@ -35,7 +35,7 @@
-
+
Ajouter un groupe
diff --git a/templates/admin_user_new.html b/templates/admin_user_new.html index 82c615f..0cab732 100644 --- a/templates/admin_user_new.html +++ b/templates/admin_user_new.html @@ -25,9 +25,9 @@
- +
- {% for group in groups %} {% endfor %} diff --git a/templates/admin_users.html b/templates/admin_users.html index d45f800..3c39bf0 100644 --- a/templates/admin_users.html +++ b/templates/admin_users.html @@ -21,7 +21,7 @@
- + - + - + {% endfor %} diff --git a/templates/layout.html b/templates/layout.html index a73f0e6..321610b 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -32,7 +32,7 @@
  • Nouvel utilisateur
  • Administrer les utilisateurs
  • -
  • Administrer les groupes
  • +
  • Administrer les groupes
  • {% endif %} diff --git a/templates/vote.html b/templates/vote.html index 47b1d29..c6da367 100644 --- a/templates/vote.html +++ b/templates/vote.html @@ -39,7 +39,7 @@ - {% if vote.is_multiple %} + {% if vote.is_multiplechoice %} {% for choice in choices %} {% endfor %} @@ -74,7 +74,7 @@
    Publié par
    maethor
    Début le
    {{ vote.date_begin }}
    Deadline le
    {{ vote.date_end }} -
    Groupe
    {{ vote.rolename }} +
    Groupe
    {{ vote.groupname }}
    Catégorie
    {{ vote.category }}
    diff --git a/templates/votes.html b/templates/votes.html index d770c2b..55ab248 100644 --- a/templates/votes.html +++ b/templates/votes.html @@ -7,14 +7,14 @@ Liste des votes

    Deadline : {{ vote.date_end }}

    -

    Groupe : {{ vote.rolename }}

    +

    Groupe : {{ vote.group }}

    Categorie : {{ vote.category }}

    -- 2.20.1
    {{ role.name }}{{ group.name }} TODO TODO{% if role.system %}system{% else %}Supprimer{% endif %}{% if group.system %}system{% else %}Supprimer{% endif %}
    {{ user.email }} {{ user.username }}{% for role in user.roles %}{{ role }} {% endfor %}{% for group in user.groups %}{{ group }} {% endfor %} {% if user.is_admin %}Oui{% else %}Non{% endif %} Éditer diff --git a/templates/admin_vote_edit.html b/templates/admin_vote_edit.html index 321ae85..d2f4ac1 100644 --- a/templates/admin_vote_edit.html +++ b/templates/admin_vote_edit.html @@ -3,7 +3,7 @@
    - +
    Édition du vote
    diff --git a/templates/admin_vote_new.html b/templates/admin_vote_new.html index a9019bb..03bf355 100644 --- a/templates/admin_vote_new.html +++ b/templates/admin_vote_new.html @@ -25,9 +25,9 @@
    - +
    - {% for group in groups %} {% endfor %} diff --git a/templates/admin_votes.html b/templates/admin_votes.html index 9ce015a..605dc46 100644 --- a/templates/admin_votes.html +++ b/templates/admin_votes.html @@ -24,14 +24,14 @@
    {{ vote.title }} {% if vote.is_open %}Ouvert{% else %}Fermé{% endif %} {{ vote.date_end }}{{ vote.rolename }}{{ vote.groupname }} {{ vote.category }} {% if vote.is_transparent %}transparent{% endif %} {% if vote.is_public %}public{% endif %} {% if vote.is_multiplechoice %}choix multiple{% endif %} ÉditerÉditer