Browse Source

Attachments

Guillaume Subiron 13 years ago
parent
commit
ceddf4b06d
3 changed files with 56 additions and 3 deletions
  1. 24 1
      main.py
  2. 2 2
      schema.sql
  3. 30 0
      templates/admin_vote_edit.html

+ 24 - 1
main.py

@@ -362,7 +362,8 @@ def admin_vote_edit(voteid):
     vote['duration'] = 15
     group = query_db('select name from roles where id = ?', [vote['id_role']], one=True) 
     choices = query_db('select * from choices where id_vote = ?', [voteid])
-    return render_template('admin_vote_edit.html', vote=vote, group=group, choices=choices)
+    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)
 
 @app.route('/admin/votes/addchoice/<voteid>', methods=['POST'])
 def admin_vote_addchoice(voteid):
@@ -401,6 +402,28 @@ def admin_vote_deletechoice(voteid, choiceid):
     g.db.commit()
     return redirect(url_for('admin_vote_edit', voteid=voteid))
 
+@app.route('/admin/votes/addattachment/<voteid>', methods=['POST'])
+def admin_vote_addattachment(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 attachments (url, id_vote) values (?, ?)', [request.form['url'], voteid])
+    g.db.commit()
+    return redirect(url_for('admin_vote_edit', voteid=voteid))
+
+@app.route('/admin/votes/deleteattachment/<voteid>/<attachmentid>')
+def admin_vote_deleteattachment(voteid, attachmentid):
+    if not session.get('user').get('is_admin'):
+        abort(401)
+    attachment = query_db('select * from attachments where id = ? and id_vote = ?', [attachmentid, voteid], one=True)
+    if attachment is None:
+        abort(404)
+    g.db.execute('delete from attachments where id = ? and id_vote = ?', [attachmentid, voteid])
+    g.db.commit()
+    return redirect(url_for('admin_vote_edit', voteid=voteid))
+
 #------
 # Main
 

+ 2 - 2
schema.sql

@@ -48,10 +48,10 @@ create table votes (
 );
 
 create table attachments (
+    id INTEGER primary key autoincrement,
     url TEXT not null,
     id_vote INTEGER not null,
-    FOREIGN KEY(id_vote) REFERENCES vote(id),
-    PRIMARY KEY(url, id_vote)
+    FOREIGN KEY(id_vote) REFERENCES vote(id)
 );
 
 create table choices (

+ 30 - 0
templates/admin_vote_edit.html

@@ -138,6 +138,36 @@
       </table>
     </fieldset>
   </div>
+
+  <div class="span5 well pull-right">
+    <fieldset><legend>Pièces jointes</legend>
+      <table class="table table-stripped table-condensed">
+        <thead>
+          <tr>
+            <th>Lien
+            <th>Actions
+          </tr>
+        </thead>
+        <tbody>
+        {% for attachment in attachments %}
+          <tr>
+            <td>{{ attachment.url }}</td>
+            <td><a href="{{ url_for('admin_vote_deleteattachment', voteid=vote.id, attachmentid=attachment.id) }}" class="btn btn-small btn-danger">Supprimer</a></td>
+          </tr>
+        {% endfor %}
+        </tbody>
+        <tfoot>
+          <tr>
+            <form action="{{ url_for('admin_vote_addattachment', voteid=vote.id) }}" method="post">
+              <td><input type="text" name="url" value="Nouveau document" /></td>
+              <td><input type="submit" class="btn btn-small btn-primary" value="+ Ajouter" />
+            </form>
+          </tr>
+        </tfoot>
+      </table>
+    </fieldset>
+  </div>
+
 </div>
 
 {% endblock %}