Progress bars
[cavote.git] / main.py
diff --git a/main.py b/main.py
index 74cb0f5..bb092ac 100755 (executable)
--- a/main.py
+++ b/main.py
@@ -290,7 +290,11 @@ def admin_group_del(idgroup):
 def votes(votes):
     today = date.today()
     active_button = votes
-    basequery = 'select *, votes.id as voteid, groups.name as groupname from votes join groups on groups.id = id_group where is_open=1'
+    max_votes ='select id_group, count(*) as max_votes from user_group group by id_group'
+    basequery = 'select votes.*, max_votes from votes join (' + max_votes + ') as max_votes on votes.id_group = max_votes.id_group'
+    nb_votes = 'select id_vote, count(*) as nb_votes from (select id_user, id_vote from user_choice join choices on id_choice = choices.id group by id_user, id_vote) group by id_vote'
+    basequery = 'select * from (' + basequery + ') join (' + nb_votes + ') on id = id_vote'
+    basequery = 'select *, votes.id as voteid, groups.name as groupname from (' + basequery + ') as 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':
@@ -299,6 +303,8 @@ def votes(votes):
         votes = query_db(basequery + ' and date_end >= (?) order by id desc', [today])
     else:
         abort(404)
+    for vote in votes:
+        vote['percent'] = int((float(vote['nb_votes']) / float(vote['max_votes'])) * 100)
     return render_template('votes.html', votes=votes, active_button=active_button)
 
 #------
@@ -355,6 +361,17 @@ def vote(idvote):
                 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 order by id', [idvote])
         attachments = query_db('select * from attachments where id_vote=?', [idvote])
+        tmp = query_db('select id_group, count(*) as nb from user_group where id_group = ? group by id_group', [vote['id_group']], one=True)
+        if tmp is None:
+            vote['percent'] = 0
+        else:
+            vote['max_votes'] = tmp['nb']
+            tmp = query_db('select id_vote, count(*) as nb from (select id_user, id_vote from user_choice join choices on id_choice = choices.id group by id_user, id_vote) where id_vote = ? group by id_vote', [idvote], one=True)
+            if tmp is None:
+                vote['percent'] = 0
+            else:
+                vote['nb_votes'] = tmp['nb']
+                vote['percent'] = int((float(vote['nb_votes']) / float(vote['max_votes'])) * 100)
         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'))