Renamed role table into groups, and debug
authorGuillaume Subiron <maethor@subiron.org>
Mon, 4 Jun 2012 14:13:33 +0000 (16:13 +0200)
committerJulien Rabier <taziden@flexiden.org>
Mon, 4 Jun 2012 22:39:12 +0000 (00:39 +0200)
12 files changed:
main.py
schema.sql
templates/admin_groups.html [new file with mode: 0644]
templates/admin_roles.html [deleted file]
templates/admin_user_new.html
templates/admin_users.html
templates/admin_vote_edit.html
templates/admin_vote_new.html
templates/admin_votes.html
templates/layout.html
templates/vote.html
templates/votes.html

diff --git a/main.py b/main.py
index e2e7d81..b3f50ba 100755 (executable)
--- 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/<userid>', 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/<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
@@ -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/<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()):
@@ -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/<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])
@@ -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/<voteid>', 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)
index 891cf76..7f6a0bc 100644 (file)
@@ -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_groups.html b/templates/admin_groups.html
new file mode 100644 (file)
index 0000000..966372e
--- /dev/null
@@ -0,0 +1,48 @@
+{% extends "layout.html" %}
+{% block subtitle %}Administrer les votes{% endblock %}
+{% block body %}
+<div class="row">
+<div class="span6 well">
+  <h2>Groupes</h2>
+  <hr />
+  {% if not groups %}
+  <div class="alert">Il n'y a aucun group.</div>
+  {% else %}
+  <table class="table table-stripped">
+    <thead>
+    <tr>
+      <th>Nom</th>
+      <th>Membres</th>
+      <th>Votes</th>
+      <th>Actions</th>
+    </tr>
+    </thead>
+    <tbody>
+    {% for group in groups %}
+    <tr>
+        <td><strong>{{ group.name }}</strong></td>
+        <td>TODO</td>
+        <td>TODO</td>
+        <td>{% if group.system %}<span class="label label-warning">system</span>{% else %}<a href="{{ url_for('admin_group_del', idgroup=group.id) }}" class="btn btn-danger btn-mini">Supprimer</a>{% endif %}</td>
+    </tr>
+    {% endfor %}
+    </tbody>
+  </table>
+  <p>
+  <small>Les groupes « system » ne sont pas modifiables.</small>
+  </p>
+  {% endif %}
+</div>
+
+<div class="span5 well">
+  <form action="{{ url_for('admin_group_add') }}" method="post" class="form-inline">
+  <fieldset><legend>Ajouter un groupe</legend>
+      <br />
+      <input type="text" name="name" id="name" value="Nom" />
+      <input type="submit" class="btn btn-primary" value="Ajouter" />
+  </fieldset>
+  </form>
+</div>
+</div>
+{% endblock %}
+
diff --git a/templates/admin_roles.html b/templates/admin_roles.html
deleted file mode 100644 (file)
index 7772337..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-{% extends "layout.html" %}
-{% block subtitle %}Administrer les votes{% endblock %}
-{% block body %}
-<div class="row">
-<div class="span6 well">
-  <h2>Groupes</h2>
-  <hr />
-  {% if not roles %}
-  <div class="alert">Il n'y a aucun role.</div>
-  {% else %}
-  <table class="table table-stripped">
-    <thead>
-    <tr>
-      <th>Nom</th>
-      <th>Membres</th>
-      <th>Votes</th>
-      <th>Actions</th>
-    </tr>
-    </thead>
-    <tbody>
-    {% for role in roles %}
-    <tr>
-        <td><strong>{{ role.name }}</strong></td>
-        <td>TODO</td>
-        <td>TODO</td>
-        <td>{% if role.system %}<span class="label label-warning">system</span>{% else %}<a href="{{ url_for('admin_role_del', idrole=role.id) }}" class="btn btn-danger btn-mini">Supprimer</a>{% endif %}</td>
-    </tr>
-    {% endfor %}
-    </tbody>
-  </table>
-  <p>
-  <small>Les groupes « system » ne sont pas modifiables.</small>
-  </p>
-  {% endif %}
-</div>
-
-<div class="span5 well">
-  <form action="{{ url_for('admin_role_add') }}" method="post" class="form-inline">
-  <fieldset><legend>Ajouter un groupe</legend>
-      <br />
-      <input type="text" name="name" id="name" value="Nom" />
-      <input type="submit" class="btn btn-primary" value="Ajouter" />
-  </fieldset>
-  </form>
-</div>
-</div>
-{% endblock %}
-
index 82c615f..0cab732 100644 (file)
@@ -25,9 +25,9 @@
     </div>
   </div>
   <div class="control-group">
-    <label class="control-label" for="roles">Groupes</label>
+    <label class="control-label" for="groups">Groupes</label>
     <div class="controls">
-      <select name="roles" id="roles" multiple>
+      <select name="groups" id="groups" multiple>
         {% for group in groups %}
         <option value="{{ group.id }}">{{ group.name }}</option>
         {% endfor %}
index d45f800..3c39bf0 100644 (file)
@@ -21,7 +21,7 @@
     <tr>
       <td>{{ user.email }}</td>
       <td>{{ user.username }}</td>
-      <td>{% for role in user.roles %}<span class="label">{{ role }}</span> {% endfor %}</td>
+      <td>{% for group in user.groups %}<span class="label">{{ group }}</span> {% endfor %}</td>
       <td>{% if user.is_admin %}<span class="label label-success">Oui</span>{% else %}<span class="label">Non</span>{% endif %}</td>
       <td>
         <a href="" class="btn btn-mini">Éditer</a>
index 321ae85..d2f4ac1 100644 (file)
@@ -3,7 +3,7 @@
 
 <div class="row">
   <div class="span6 well">
-    <form action="{{ url_for('admin_vote_edit', voteid=vote['id']) }}" method="post" class="form-horizontal">
+    <form action="{{ url_for('admin_vote_edit', voteid=vote.id) }}" method="post" class="form-horizontal">
       <fieldset><legend>Édition du vote</legend>
       <div class="control-group">
         <label class="control-label" for="title">Titre</label>
index a9019bb..03bf355 100644 (file)
@@ -25,9 +25,9 @@
     </div>
   </div>
   <div class="control-group">
-    <label class="control-label" for="role">Groupe</label>
+    <label class="control-label" for="group">Groupe</label>
     <div class="controls">
-      <select name="role" id="role">
+      <select name="group" id="group">
         {% for group in groups %}
         <option>{{ group.name }}</option>
         {% endfor %}
index 9ce015a..605dc46 100644 (file)
       <td>{{ vote.title }}</td>
       <td>{% if vote.is_open %}<span class="label label-success">Ouvert</span>{% else %}<span class="label label-important">Fermé</span>{% endif %}</td>
       <td>{{ vote.date_end }}</td>
-      <td>{{ vote.rolename }}</td>
+      <td>{{ vote.groupname }}</td>
       <td>{{ vote.category }}</td>
       <td>
         {% if vote.is_transparent %}<span class="label">transparent</span>{% endif %}
         {% if vote.is_public %}<span class="label">public</span>{% endif %}
         {% if vote.is_multiplechoice %}<span class="label">choix multiple</span>{% endif %}
       </td>
-      <td><a href="{{ url_for('admin_vote_edit', voteid=vote.id) }}" class="btn btn-mini">Éditer</a></td>
+      <td><a href="{{ url_for('admin_vote_edit', voteid=vote.voteid) }}" class="btn btn-mini">Éditer</a></td>
     </tr>
     {% endfor %}
     </tbody>
index a73f0e6..321610b 100644 (file)
@@ -32,7 +32,7 @@
         <!--<li><a href="404">Utilisateurs</a></li>-->
         <li><a href="{{ url_for('admin_user_add') }}">Nouvel utilisateur</a></li>
         <li><a href="{{ url_for('admin_users') }}">Administrer les utilisateurs</a></li>
-        <li><a href="{{ url_for('admin_roles') }}">Administrer les groupes</a></li>
+        <li><a href="{{ url_for('admin_groups') }}">Administrer les groupes</a></li>
       </ul>
       {% endif %}
     </div>
index 47b1d29..c6da367 100644 (file)
@@ -39,7 +39,7 @@
     <form class="form-inline" action="{{ url_for('vote', idvote=vote.id) }}" method="post">
     <tr>
       <th><input type='text' name="username" value='{{ session.user.name }}' disabled /></th>
-      {% if vote.is_multiple %}
+      {% if vote.is_multiplechoice %}
       {% for choice in choices %}
       <td><input type='checkbox' name="{{ choice.id }}" /></td>
       {% endfor %}
@@ -74,7 +74,7 @@
   <dt>Publié par <dd><code>maethor</code>
   <dt>Début le <dd><code>{{ vote.date_begin }}</code>
   <dt>Deadline le <dd><code>{{ vote.date_end }}</code>
-  <dt>Groupe <dd><code>{{ vote.rolename }}</code>
+  <dt>Groupe <dd><code>{{ vote.groupname }}</code>
   <dt>Catégorie <dd><code>{{ vote.category }}</code>
 </dl>
 <dl>
index d770c2b..55ab248 100644 (file)
@@ -7,14 +7,14 @@ Liste des votes
   <article>
     <div class="row well">
       <div class="span4">
-        <h3><a href="{{ url_for('vote', idvote=vote.id) }}">{{ vote.title }}</a></h3>
+        <h3><a href="{{ url_for('vote', idvote=vote.voteid) }}">{{ vote.title }}</a></h3>
         <div class="progress progress-striped">
           <div class="bar" style="width: 60{{ vote.percent }}%;"></div>
         </div>
       </div>
       <div class="span3">
         <h4>Deadline : {{ vote.date_end }} </h4>
-        <h4>Groupe : {{ vote.rolename }}</h4>
+        <h4>Groupe : {{ vote.group }}</h4>
         <h4>Categorie : {{ vote.category }}</h4>
       </div>
       <div class="span3">