Browse Source

add comments support

Julien Moutinho 10 years ago
parent
commit
f2c5f4be15
4 changed files with 51 additions and 10 deletions
  1. 8 5
      main.py
  2. 1 0
      schema.sql
  3. 17 1
      static/css/cavote.css
  4. 25 4
      templates/vote.html

+ 8 - 5
main.py

@@ -530,9 +530,10 @@ def vote(idvote):
                                   order by random()')
                                   order by random()')
                     g.db.execute('delete from user_choice_buffer_anonymous')
                     g.db.execute('delete from user_choice_buffer_anonymous')
                     g.db.commit()
                     g.db.commit()
-                g.db.execute('insert into user_vote (id_user, id_vote) \
-                              values (?, ?)'
-                            , [session.get('user').get('id'), vote['id']])
+                comment = request.form.get('comment', None)
+                g.db.execute('insert into user_vote (id_user, id_vote, comment) \
+                              values (?, ?, ?)'
+                            , [session.get('user').get('id'), vote['id'], comment])
                 g.db.commit()
                 g.db.commit()
             else:
             else:
                 abort(401)
                 abort(401)
@@ -558,7 +559,7 @@ def vote(idvote):
         # ACTION: query users' choices joined with users' identity if not anonymous
         # ACTION: query users' choices joined with users' identity if not anonymous
         user_choices = query_db('select user_choice.id_user as userid, users.name as username, \
         user_choices = query_db('select user_choice.id_user as userid, users.name as username, \
                                         choices.id as choiceid, choices.name as choice_name, \
                                         choices.id as choiceid, choices.name as choice_name, \
-                                        user_choice.weight as weight \
+                                        user_choice.weight as weight, user_vote.comment as comment \
                                  from choices \
                                  from choices \
                                  join user_choice on choices.id = user_choice.id_choice \
                                  join user_choice on choices.id = user_choice.id_choice \
                                  left join users on userid = users.id \
                                  left join users on userid = users.id \
@@ -615,11 +616,12 @@ def vote(idvote):
         # ACTION: list user results per user
         # ACTION: list user results per user
         users = OrderedDict()
         users = OrderedDict()
         if vote['is_anonymous']:
         if vote['is_anonymous']:
-            user_votes = query_db('select users.name, id_user as userid \
+            user_votes = query_db('select users.name, id_user as userid, comment \
                                    from user_vote \
                                    from user_vote \
                                    join users on users.id = id_user where id_vote = ?', [idvote])
                                    join users on users.id = id_user where id_vote = ?', [idvote])
             for uc in user_votes:
             for uc in user_votes:
                 users[uc['userid']] = { 'username':uc['name']
                 users[uc['userid']] = { 'username':uc['name']
+                                      , 'comment':uc['comment']
                                       , 'choices':{}
                                       , 'choices':{}
                                       , 'userid':uc['userid'] }
                                       , 'userid':uc['userid'] }
         else:
         else:
@@ -633,6 +635,7 @@ def vote(idvote):
                 else:
                 else:
                     users[uc['userid']] = { 'userid':uc['userid']
                     users[uc['userid']] = { 'userid':uc['userid']
                                           , 'username':uc['username']
                                           , 'username':uc['username']
+                                          , 'comment':uc['comment']
                                           , 'choices':{uc['choiceid']:value} }
                                           , 'choices':{uc['choiceid']:value} }
         attachments = query_db('select * from attachments where id_vote=?', [idvote])
         attachments = query_db('select * from attachments where id_vote=?', [idvote])
         if query_db('select * from user_group where id_group = ? and id_user = ?'
         if query_db('select * from user_group where id_group = ? and id_user = ?'

+ 1 - 0
schema.sql

@@ -122,6 +122,7 @@ create table user_vote (
     date INTEGER DEFAULT CURRENT_TIMESTAMP NOT NULL,
     date INTEGER DEFAULT CURRENT_TIMESTAMP NOT NULL,
     id_user INTEGER NOT NULL,
     id_user INTEGER NOT NULL,
     id_vote INTEGER NOT NULL,
     id_vote INTEGER NOT NULL,
+    comment TEXT,
     FOREIGN KEY(id_user) REFERENCES users (id) ON DELETE CASCADE,
     FOREIGN KEY(id_user) REFERENCES users (id) ON DELETE CASCADE,
     FOREIGN KEY(id_vote) REFERENCES votes (id) ON DELETE CASCADE,
     FOREIGN KEY(id_vote) REFERENCES votes (id) ON DELETE CASCADE,
     PRIMARY KEY(id_user, id_vote)
     PRIMARY KEY(id_user, id_vote)

+ 17 - 1
static/css/cavote.css

@@ -3,16 +3,32 @@
     margin-top:9px;
     margin-top:9px;
  }
  }
 
 
+.table-comments tbody tr td {
+	text-align:left;
+ }
+
 .table-votes {
 .table-votes {
-	 table-layout:fixed;
+	table-layout:fixed;
  }
  }
 .table-votes thead th {
 .table-votes thead th {
     text-align: right;
     text-align: right;
     vertical-align: middle;
     vertical-align: middle;
 }
 }
+.table-votes thead th.users {
+	text-align:left;
+	padding-left:1em;
+ }
 .table-votes th.choice-name {
 .table-votes th.choice-name {
     text-align: center;
     text-align: center;
 }
 }
+.table-votes thead th.comments {
+	text-align:left;
+	padding-left:1em;
+ }
+.table-votes tbody td.ok {
+	text-align:left;
+	padding-left:1em;
+ }
 
 
 .table-votes input {
 .table-votes input {
     margin: 0;
     margin: 0;

+ 25 - 4
templates/vote.html

@@ -120,12 +120,14 @@
 <table class="table table-condensed table-bordered table-votes cardinal-{{ vote.id_cardinal }}">
 <table class="table table-condensed table-bordered table-votes cardinal-{{ vote.id_cardinal }}">
   <thead>
   <thead>
     <tr>
     <tr>
-      <th></th>
+      <th class="users">Votant-e-s</th>
       {% for choice in choices %}
       {% for choice in choices %}
       <th class="choice-name">{{ choice.name }}</th>
       <th class="choice-name">{{ choice.name }}</th>
       {% endfor %}
       {% endfor %}
       {% if 'user' in session and (can_vote or (not vote['is_terminated'] and not vote['is_anonymous'])) %}
       {% if 'user' in session and (can_vote or (not vote['is_terminated'] and not vote['is_anonymous'])) %}
       <th></th>
       <th></th>
+      {% else %}
+      <th class="comments">Commentaires</th>
       {% endif %}
       {% endif %}
     </tr>
     </tr>
   {% if vote.is_transparent %}
   {% if vote.is_transparent %}
@@ -155,7 +157,8 @@
         </select>
         </select>
       </td>
       </td>
       {% endfor %}
       {% endfor %}
-      <td><input type="submit" class="btn btn-primary" value="OK" /></td>
+      <td><input type="submit" class="btn btn-primary" value="OK" />
+          <textarea name="comment" class="input-xlarge" placeholder="Commentaire"></textarea></td>
     </tr>
     </tr>
     </form>
     </form>
   {% endif %}
   {% endif %}
@@ -171,7 +174,7 @@
       <td class="{{user.choices[choice.id].class}}"><span>{{ user.choices[choice.id].name }}</span></td>
       <td class="{{user.choices[choice.id].class}}"><span>{{ user.choices[choice.id].name }}</span></td>
       {% endfor %}
       {% endfor %}
       {% if 'user' in session and (can_vote or (not vote['is_terminated'] and not vote['is_anonymous'])) %}
       {% if 'user' in session and (can_vote or (not vote['is_terminated'] and not vote['is_anonymous'])) %}
-      <td>{% if user.userid == session.user.id %}
+      <td class="ok">{% if user.userid == session.user.id %}
         <a href="#delete" data-toggle="modal" class="btn btn-danger btn-mini" title="Supprimer"><i class="icon-remove icon-white"></i></a>
         <a href="#delete" data-toggle="modal" class="btn btn-danger btn-mini" title="Supprimer"><i class="icon-remove icon-white"></i></a>
         <div class="modal hide fade" id="delete">
         <div class="modal hide fade" id="delete">
           <div class="modal-header">
           <div class="modal-header">
@@ -186,7 +189,13 @@
             <a href="#" class="btn" data-dismiss="modal">Annuler</a>
             <a href="#" class="btn" data-dismiss="modal">Annuler</a>
           </div>
           </div>
         </div>
         </div>
-      {% endif %}</td>
+        {% endif %}
+        <span class="comment">{% if user.comment %}{{user.comment}}{%endif%}</span>
+      </td>
+      {% else %}
+      <td class="ok">
+        <span class="comment">{% if user.comment %}{{user.comment}}{%endif%}</span>
+      </td>
       {% endif %}
       {% endif %}
     </tr>
     </tr>
   {% endif %}
   {% endif %}
@@ -201,6 +210,18 @@
 {% endif %}
 {% endif %}
 </div>
 </div>
 
 
+{% if vote.is_anonymous %}
+<div class="span9">
+  <h2 class='page-header'>Commentaires</h2>
+  <table class="table table-condensed table-bordered table-comments">
+    <tbody>
+      {% for user in users %}
+      <tr><td><span class="comment">{% if user.comment %}{{user.comment}}{%endif%}</span></td></tr>
+      {% endfor %}
+    </tbody>
+  </table>
+{% endif %}
+
 </div>
 </div>
 {% endblock %}
 {% endblock %}