Edit vote
authorGuillaume Subiron <maethor@subiron.org>
Mon, 28 May 2012 20:09:37 +0000 (22:09 +0200)
committerJulien Rabier <taziden@flexiden.org>
Mon, 28 May 2012 22:30:48 +0000 (00:30 +0200)
main.py
schema.sql
templates/edit_vote.html [new file with mode: 0644]
templates/new_vote.html

diff --git a/main.py b/main.py
index 4662b59..c26c2cb 100755 (executable)
--- a/main.py
+++ b/main.py
@@ -204,12 +204,67 @@ def add_vote():
             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])
             g.db.commit()
-            flash('New entry was successfully posted', 'info')
-            return redirect(url_for('home'))
+            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
+            if vote is None:
+                flash(u'Une erreur est survenue !', 'error')
+                return redirect(url_for('home'))
+            else:
+                flash(u"Le vote a été créé", 'info')
+                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')
 
+@app.route('/votes/admin/edit/<voteid>', methods=['GET', 'POST'])
+def edit_vote(voteid):
+    if not session.get('user').get('is_admin'):
+        abort(401)
+    vote = query_db('select * from votes where id = ?', [voteid], one=True)
+    if vote is None:
+        abort(404)
+    #if request.method == 'POST':
+    # :TODO:maethor:20120528
+    choices = query_db('select * from choices where id_vote = ?', [voteid])
+    return render_template('edit_vote.html', vote=vote, choices=choices)
+
+@app.route('/votes/admin/addchoice/<voteid>', methods=['POST'])
+def add_choice(voteid):
+    if not session.get('user').get('is_admin'):
+        abort(401)
+    vote = query_db('select * from votes where id = ?', [voteid], one=True)
+    if vote is None:
+        abort(404)
+    g.db.execute('insert into choices (name, id_vote) values (?, ?)', [request.form['title'], voteid])
+    g.db.commit()
+    return redirect(url_for('edit_vote', voteid=voteid))
+
+@app.route('/votes/admin/editchoice/<voteid>/<choiceid>', methods=['POST', 'DELETE'])
+def edit_choice(voteid, choiceid):
+    if not session.get('user').get('is_admin'):
+        abort(401)
+    choice = query_db('select * from choices where id = ? and id_vote = ?', [choiceid, voteid], one=True)
+    if choice is None:
+        abort(404)
+    if request.method == 'POST':
+        g.db.execute('update choices set name=? where id = ? and id_vote = ?', [request.form['title'], choiceid, voteid])
+        g.db.commit()
+    elif request.method == 'DELETE': # :COMMENT:maethor:20120528: I can't find how to use it from template
+        g.db.execute('delete from choices where id = ? and id_vote = ?', [choiceid, voteid])
+        g.db.commt()
+    return redirect(url_for('edit_vote', voteid=voteid))
+
+@app.route('/votes/admin/deletechoice/<voteid>/<choiceid>')
+def delete_choice(voteid, choiceid):
+    if not session.get('user').get('is_admin'):
+        abort(401)
+    choice = query_db('select * from choices where id = ? and id_vote = ?', [choiceid, voteid], one=True)
+    if choice is None:
+        abort(404)
+    g.db.execute('delete from choices where id = ? and id_vote = ?', [choiceid, voteid])
+    g.db.commit()
+    return redirect(url_for('edit_vote', voteid=voteid))
+
 #------
 # Main
 
index b7fa00b..5529a31 100644 (file)
@@ -1,3 +1,4 @@
+drop table if exists choices;
 drop table if exists votes;
 drop table if exists users;
 
@@ -22,13 +23,20 @@ create table votes (
     is_public INTEGER default 1 not null,
     is_multiplechoice INTEGER default 1 not null,
     is_weighted INTEGER default 0 not null,
-    is_closed INTEGER default 0 not null,
+    is_open INTEGER default 0 not null,
     id_author INTEGER, -- :COMMENT:maethor:120528: not null ?
     --id_role INTEGER,
     FOREIGN KEY(id_author) REFERENCES users(id)
     --FOREIGN KEY(id_role) REFERENCES role(id)
 );
 
+create table choices (
+    id INTEGER primary key autoincrement,
+    name TEXT not null,
+    id_vote INTEGER not null,
+    FOREIGN KEY(id_vote) REFERENCES vote(id)
+);
+
 -- Test data
 
 insert into users (email, password, name, organization, is_admin, key) values ("admin@admin.fr", "admin", "Toto (admin) Tata", "World corp", 1, "test");
diff --git a/templates/edit_vote.html b/templates/edit_vote.html
new file mode 100644 (file)
index 0000000..4319631
--- /dev/null
@@ -0,0 +1,146 @@
+{% extends "layout.html" %}
+{% block body %}
+
+<div class="row">
+  <div class="span6 well">
+    <form action="{{ url_for('edit_vote', 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>
+        <div class="controls">
+          <input type="text" size=30 name="title" id="title" value="{{ vote.title }}" />
+          <span class="help-inline"><font color="red">*</font></span>
+        </div>
+      </div>
+      <div class="control-group">
+        <label class="control-label" for="description">Description</label>
+        <div class="controls">
+          <textarea class="input-xlarge" name="description" id="description" value="{{ vote.description }}"></textarea>
+        </div>
+      </div>
+      <div class="accordion">
+        <div class="accordion-group">
+          <div class="accordion-heading">
+            <a class="accordion-toggle" data-toggle="collapse" href="#infos">+ Informations</a>
+          </div>
+          <div id="infos" class="accordion-body collapse">
+            <div class="accordion-inner">
+              <div class="control-group">
+                <label class="control-label" for="category">Catégorie</label>
+                <div class="controls">
+                  <input type="text" data-provide="typeahead" data-source='["ca","membres"]' size=30 name="category" id="category" value="{{ request.form.category }}" />
+                </div>
+              </div>
+              <div class="control-group">
+                <label class="control-label" for="role">Groupe</label>
+                <div class="controls">
+                  <select name="role" id="role" disabled>
+                    <option selected>Tous</option>
+                    <option>CA</option>
+                    <option>Membres</option>
+                  </select>
+                  <p class="help-block">Groupe d'utilisateur concernés par le vote et ayant le droit de voter</p>
+                </div>
+              </div>
+            </div>
+          </div>
+        </div>
+        <div class="accordion-group">
+          <div class="accordion-heading">
+            <a class="accordion-toggle" data-toggle="collapse" href="#options">+ Options</a>
+          </div>
+          <div id="options" class="accordion-body collapse">
+            <div class="accordion-inner">
+              <div class="control-group">
+                <label class="control-label" for="days">Durée (jours)</label>
+                <div class="controls">
+                  <select class="span1" name="days" id="days">
+                    {% for i in range(1, 31) %}
+                    {% if i == 20 %}
+                    <option selected>{{ i }}</option>
+                    {% else %}
+                    <option>{{ i }}</option>
+                    {% endif %}
+                    {% endfor %}
+                  </select>
+                </div>
+              </div>
+              <div class="control-group">
+                <label class="control-label">Options</label>
+                <div class="controls">
+                  <label class="checkbox">
+                    <input type="checkbox" name="transparent" />
+                    Les votants peuvent-ils voir le choix des autres ?
+                  </label>
+                  <label class="checkbox">
+                    <input type="checkbox" name="public" />
+                    Le vote est-il visible par tous ?
+                  </label>
+                  {% if not vote.is_open %}
+                  <label class="checkbox">
+                    <input type="checkbox" name="multiplechoice" disabled/>
+                    Les votants peuvent-ils choisir plusieurs options ?
+                  </label>
+                  {% endif %}
+                </div>
+              </div>
+            </div>
+          </div>
+        </div>
+      </div>
+      <div class="control-group">
+        <label class="control-label" for="status">Statut</label>
+        <div class="controls">
+          <select class="span2" name="status" id="status">
+            {% if vote.is_open %}
+              <option>Fermé</option>
+              <option selected>Ouvert</option>
+            {% else %}
+              <option selected>Fermé</option>
+              <option>Ouvert</option>
+            {% endif %}
+          </select>
+        </div>
+      </div>
+      <div class="form-actions">
+        <input type="submit" class="btn btn-primary" value="Enregistrer" />
+        <input type="reset" class="btn" value="Annuler" />
+      </div>
+    </fieldset>
+    </form>
+  </div>
+
+  <div class="span5 well">
+    <fieldset><legend>Choix</legend>
+      <table class="table table-stripped table-condensed">
+        <thead>
+          <tr>
+            <th>Titre
+            <th>Actions
+          </tr>
+        </thead>
+        <tbody>
+        {% for choice in choices %}
+          <tr>
+            <form action="{{ url_for('edit_choice', voteid=vote.id, choiceid=choice.id) }}" method="post">
+              <td><input type="text" name="title" value="{{ choice.name }}" /></td>
+              <td><input type="submit" class="btn btn-small" value="Sauver" /> 
+                  <a href="{{ url_for('delete_choice', voteid=vote.id, choiceid=choice.id) }}" class="btn btn-small btn-danger">Supprimer</a></td>
+            </form>
+          </tr>
+        {% endfor %}
+        </tbody>
+        <tfoot>
+          <tr>
+            <form action="{{ url_for('add_choice', voteid=vote.id) }}" method="post">
+              <td><input type="text" name="title" value="Nouveau choix" /></td>
+              <td><input type="submit" class="btn btn-small btn-primary" value="+ Ajouter" />
+            </form>
+          </tr>
+        </tfoot>
+      </table>
+    </fieldset>
+  </div>
+</div>
+
+{% endblock %}
index 575e9f6..0adca60 100644 (file)
@@ -67,7 +67,7 @@
     </div>
   </div>
   <div class="form-actions">
-    <input type="submit" class="btn btn-primary" value="Publier" />
+    <input type="submit" class="btn btn-primary" value="Créer le vote" />
   </div>
 </fieldset>
 </form>