Nb votes and max votes displayed in progress bar
[cavote.git] / main.py
diff --git a/main.py b/main.py
index 51af2a9..46653b3 100755 (executable)
--- a/main.py
+++ b/main.py
@@ -19,10 +19,10 @@ SECRET_KEY = '{J@uRKO,xO-PK7B,jF?>iHbxLasF9s#zjOoy=+:'
 DEBUG = True
 TITLE = u"Cavote FFDN"
 EMAIL = '"' + TITLE + '"' + ' <' + u"cavote@ffdn.org" + '>'
-BASEURL = "http://localhost:5000"
+BASEURL = "http://localhost:5000" # :TODO:maethor:120605: Find a cleaner way to do this
 VERSION = "cavote 0.0.1"
 SMTP_SERVER = "10.33.33.30"
-PATTERNS = {u'Oui/Non': [u'Oui', u'Non'], u'Oui/Non/Peut-être': [u'Oui', u'Non', u'Peut-être']}
+PATTERNS = {u'Oui/Non': [u'Oui', u'Non'], u'Oui/Non/Blanc': [u'Oui', u'Non', u'Blanc'], u'Oui/Non/Peut-être': [u'Oui', u'Non', u'Peut-être']}
 
 app = Flask(__name__)
 app.config.from_object(__name__)
@@ -89,7 +89,7 @@ def login():
     if request.method == 'POST':
         user = valid_login(request.form['username'], request.form['password'])
         if user is None:
-            flash('Email ou mot de passe invalide.', 'error')
+            flash(u'Email ou mot de passe invalide.', 'error')
         else:
             connect_user(user)
             flash(u'Vous êtes connecté. Bienvenue, %s !' % user['name'], 'success')
@@ -99,7 +99,7 @@ def login():
 @app.route('/logout')
 def logout():
     disconnect_user()
-    flash('Vous avez été déconnecté.', 'info')
+    flash(u'Vous avez été déconnecté.', 'info')
     return redirect(url_for('home'))
 
 #-----------------
@@ -323,7 +323,7 @@ def votes(votes):
     today = date.today()
     active_button = votes
     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'
+    basequery = 'select votes.*, max_votes from votes left 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 + ') left 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'
@@ -342,7 +342,8 @@ def votes(votes):
     for vote in votes:
         if not vote.get('nb_votes'):
             vote['nb_votes'] = 0 
-        vote['percent'] = int((float(vote['nb_votes']) / float(vote['max_votes'])) * 100)
+        if vote.get('max_votes'):
+            vote['percent'] = int((float(vote['nb_votes']) / float(vote['max_votes'])) * 100)
     return render_template('votes.html', votes=votes, active_button=active_button)
 
 #------
@@ -362,7 +363,7 @@ def can_vote(idvote, iduser=-1):
     vote = query_db('select * from votes where id=?', [idvote], one=True)
     if vote is None:
         return False
-    if vote['is_finished'] == 0:
+    if vote['is_terminated'] == 0:
         if iduser > 0: 
             if can_see_vote(idvote, iduser): 
                 if not has_voted(idvote, iduser):
@@ -382,14 +383,18 @@ def vote(idvote):
     if can_see_vote(idvote, get_userid()):
         if request.method == 'POST':
             if can_vote(idvote, get_userid()):
-                choices = query_db('select name, id from choices where id_vote=?', [idvote])
-                for choice in choices:
-                    if str(choice['id']) in request.form.keys():
+                if vote['is_multiplechoice'] == 0:
+                    if query_db('select * from choices where id = ?', [request.form['choice']], one=True) is not None:
                         g.db.execute('insert into user_choice (id_user, id_choice) values (?, ?)', 
-                                [session.get('user').get('id'), choice['id']])
+                                [session.get('user').get('id'), request.form['choice']])
                         g.db.commit()
-                        if vote['is_multiplechoice'] == 0:
-                            break
+                else:
+                    choices = query_db('select name, id from choices where id_vote=?', [idvote])
+                    for choice in choices:
+                        if str(choice['id']) in request.form.keys():
+                            g.db.execute('insert into user_choice (id_user, id_choice) values (?, ?)', 
+                                    [session.get('user').get('id'), choice['id']])
+                            g.db.commit()
             else:
                 abort(401)
         tuples = query_db('select choiceid, choicename, users.id as userid, users.name as username from (select choices.id as choiceid, choices.name as choicename, id_user as userid from choices join user_choice on choices.id = user_choice.id_choice where id_vote = ?) join users on userid = users.id', [idvote])
@@ -412,6 +417,7 @@ def vote(idvote):
             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
+                vote['nb_votes'] = 0
             else:
                 vote['nb_votes'] = tmp['nb']
                 vote['percent'] = int((float(vote['nb_votes']) / float(vote['max_votes'])) * 100)