Many TODOs, debug, cleanup, urls
authorGuillaume Subiron <maethor@subiron.org>
Thu, 7 Jun 2012 16:44:04 +0000 (18:44 +0200)
committerJulien Rabier <taziden@flexiden.org>
Thu, 7 Jun 2012 17:34:41 +0000 (19:34 +0200)
main.py
templates/admin_groups.html
templates/admin_users.html
templates/admin_votes.html

diff --git a/main.py b/main.py
index 25d31eb..6b391c3 100755 (executable)
--- a/main.py
+++ b/main.py
@@ -4,7 +4,7 @@
 from flask import Flask, request, session, g, redirect, url_for, abort, \
     render_template, flash
 import sqlite3
-from datetime import date, time, timedelta
+from datetime import date, time, timedelta, datetime
 import time
 from contextlib import closing
 import locale
@@ -19,8 +19,7 @@ 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" # :TODO:maethor:120605: Find a cleaner way to do this
-VERSION = "cavote 0.0.1"
+VERSION = "cavote 0.1.0"
 SMTP_SERVER = "10.33.33.30"
 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']}
 
@@ -116,7 +115,7 @@ def password_lost():
             key = keygen()
             g.db.execute('update users set key = ? where id = ?', [key, user['id']])
             g.db.commit()
-            link = BASEURL + url_for('login_key', userid=user['id'], key=key)
+            link = request.url_root + url_for('login_key', userid=user['id'], key=key)
             BODY = string.join((
                 "From: %s" % EMAIL,
                 "To: %s" % user['email'],
@@ -246,7 +245,7 @@ def admin_user_add():
                                 else:
                                     g.db.execute('insert into user_group values (?, ?)', [user['id'], group])
                                     g.db.commit()
-                            link = BASEURL + url_for('login_key', userid=user['id'], key=user['key'])
+                            link = request.url_root + url_for('login_key', userid=user['id'], key=user['key'])
                             BODY = string.join((
                             "From: %s" % EMAIL,
                             "To: %s" % user['email'],
@@ -380,11 +379,11 @@ def votes(votes):
     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'
     if votes == 'all':
-        votes = query_db(basequery + ' order by id desc')
+        votes = query_db(basequery + ' order by date_end')
     elif votes == 'archive':
-        votes = query_db(basequery + ' and is_terminated=1 order by id desc')
+        votes = query_db(basequery + ' and is_terminated=1 order by date_end desc')
     elif votes == 'current':
-        votes = query_db(basequery + ' and is_terminated=0 order by id desc')
+        votes = query_db(basequery + ' and is_terminated=0 order by date_end')
     elif votes == 'waiting':
         basequery = 'select votes.* from user_group join (' + basequery + ') as votes on votes.id_group = user_group.id_group where user_group.id_user = ?'
         already_voted = 'select id_vote from user_choice join choices on user_choice.id_choice = choices.id where id_user = ?'
@@ -473,7 +472,8 @@ def vote(idvote):
             else:
                 vote['nb_votes'] = tmp['nb']
                 vote['percent'] = int((float(vote['nb_votes']) / float(vote['max_votes'])) * 100)
-        # :TODO:maethor:120606: Flash if user is concerned by the vote
+        if query_db('select * from user_group where id_group = ? and id_user = ?', [vote['id_group'], get_userid()], one=True) and not vote['is_terminated']:
+            flash(u'Ce vote vous concerne !', 'info')
         return render_template('vote.html', vote=vote, attachments=attachments, choices=choices, users=users.values(), can_vote=can_vote(idvote, get_userid()))
     flash(u'Vous n\'avez pas le droit de voir ce vote, désolé.')
     return redirect(url_for('home'))
@@ -495,7 +495,7 @@ def admin_votes():
     if not session.get('user').get('is_admin'):
         abort(401)
     votes = query_db('select *, votes.id as voteid, groups.name as groupname from votes join groups on groups.id=votes.id_group order by id desc')
-    return render_template('admin_votes.html', votes=votes)
+    return render_template('admin_votes.html', votes=votes, today=date.today().strftime("%Y-%m-%d"))
 
 @app.route('/admin/votes/add', methods=['GET', 'POST'])
 def admin_vote_add():
@@ -550,7 +550,9 @@ def admin_vote_edit(voteid):
         abort(404)
     if request.method == 'POST':
         if request.form['title']:
-            # :TODO:maethor:120529: Calculer date_begin pour pouvoir y ajouter duration et obtenir date_end
+            if request.form['days'] > 0:
+                date_end = datetime.strptime(vote['date_begin'], "%Y-%m-%d") + timedelta(days=int(request.form['days']))
+                date_end = date_end.strftime("%Y-%m-%d")
             transparent = 0
             public = 0
             if 'transparent' in request.form.keys():
@@ -569,19 +571,20 @@ def admin_vote_edit(voteid):
                 isterminated = 1
                 if vote['is_open']:
                     isopen = 1
-            g.db.execute('update votes set title = ?, description = ?, category = ?, is_transparent = ?, is_public = ?, is_open = ?, is_terminated = ? where id = ?',
-                    [request.form['title'], request.form['description'], request.form['category'], transparent, public, isopen, isterminated, voteid])
+            g.db.execute('update votes set title = ?, description = ?, category = ?, is_transparent = ?, is_public = ?, is_open = ?, is_terminated = ?, date_end = ? where id = ?',
+                    [request.form['title'], request.form['description'], request.form['category'], transparent, public, isopen, isterminated, date_end, voteid])
             g.db.commit()
             vote = query_db('select * from votes where id = ?', [voteid], one=True)
             flash(u"Le vote a bien été mis à jour.", "success")
         else:
             flash(u'Vous devez spécifier un titre.', 'error')
-
-    # :TODO:maethor:120529: Calculer la durée du vote (différence date_end - date_begin) 
-    vote['duration'] = 15
+    vote['duration'] = (datetime.strptime(vote['date_end'], "%Y-%m-%d") 
+            - datetime.strptime(vote['date_begin'], "%Y-%m-%d")).days
     group = query_db('select name from groups where id = ?', [vote['id_group']], one=True) 
     choices = query_db('select * from choices where id_vote = ?', [voteid])
     attachments = query_db('select * from attachments where id_vote = ?', [voteid])
+    if date.today().strftime("%Y-%m-%d") > vote['date_end']:
+        flash(u'La deadline du vote est expirée, vous devriez terminer le vote.')
     return render_template('admin_vote_edit.html', vote=vote, group=group, choices=choices, attachments=attachments)
 
 @app.route('/admin/votes/addchoice/<voteid>', methods=['POST'])
index 1b66c69..981a9b8 100644 (file)
@@ -32,7 +32,7 @@
         <h3>Suppression d'un groupe</h3>
       </div>
       <div class="modal-body">
-        <p>Voulez-vous vraiment supprimer le groupe {{ group.name }} ?</p>
+        <p>Voulez-vous vraiment supprimer le groupe <strong>{{ group.name }}</strong> ?</p>
       </div>
       <div class="modal-footer">
         <a href="{{ url_for('admin_group_del', idgroup=group.id) }}" class="btn btn-danger">Confirmer</a>
index 3721a89..0a49e3c 100644 (file)
@@ -2,7 +2,7 @@
 {% block subtitle %}Administrer les utilisateurs{% endblock %}
 {% block body %}
 <div class="row">
-  <a class="pull-right btn btn-primary" href="{{ url_for('admin_user_add') }}">+ Ajouter un utilisateur</a>
+  <a class="pull-right btn btn-primary btn-small" href="{{ url_for('admin_user_add') }}">+ Ajouter un utilisateur</a>
   {% if not users %}
   <div class="alert">Il n'y a aucun utilisateur.</div>
   {% else %}
@@ -34,7 +34,7 @@
         <h3>Suppression d'utilisateur</h3>
       </div>
       <div class="modal-body">
-        <p>Voulez-vous vraiment supprimer l'utilisateur {{ user.name }} ?</p>
+        <p>Voulez-vous vraiment supprimer l'utilisateur <strong>{{ user.username }}</strong> ?</p>
       </div>
       <div class="modal-footer">
         <a href="{{ url_for('admin_user_del', iduser=user.userid) }}" class="btn btn-danger">Confirmer</a>
@@ -46,7 +46,7 @@
   </table>
   <p>
   {% endif %}
-  <a class="pull-right btn btn-primary" href="{{ url_for('admin_user_add') }}">+ Ajouter un utilisateur</a>
+  <a class="pull-right btn btn-primary btn-small" href="{{ url_for('admin_user_add') }}">+ Ajouter un utilisateur</a>
 
 {% endblock %}
 
index 1425921..b97e45a 100644 (file)
@@ -2,7 +2,7 @@
 {% block subtitle %}Administrer les votes{% endblock %}
 {% block body %}
 <div class="row">
-  <hr />
+  <a class="pull-right btn btn-primary btn-small" href="{{ url_for('admin_vote_add') }}">+ Nouveau vote</a>
   {% if not votes %}
   <div class="alert">Il n'y a aucun vote.</div>
   {% else %}
     <tr>
       <td>{{ vote.title }}</td>
       <td>{% if vote.is_terminated %}<span class="label label-success">Terminé</span>{% else %}{% if vote.is_open %}<span class="label label-info">Ouvert</span>{% else %}<span class="label label-important">Fermé</span>{% endif %}{% endif %}</td>
-      <td>{{ vote.date_end }}</td>
+      <td><span {% if vote.date_end < today %}style="color: red;"{% endif %}>{{ vote.date_end }}</span></td>
       <td>{{ vote.groupname }}</td>
       <td>{{ vote.category }}</td>
       <td>
         {% if vote.is_transparent %}<span class="label">transparent</span>{% endif %}
         {% if vote.is_public %}<span class="label">public</span>{% endif %}
-        {% if vote.is_multiplechoice %}<span class="label">choix multiple</span>{% endif %}
+        {% if vote.is_multiplechoice %}<span class="label">multiple</span>{% endif %}
       </td>
       <td>
         <a href="{{ url_for('vote', idvote=vote.voteid) }}" class="btn btn-success btn-mini">Voir</a>
     {% endfor %}
     </tbody>
   </table>
+  <a class="pull-right btn btn-primary btn-small" href="{{ url_for('admin_vote_add') }}">+ Nouveau vote</a>
   <p>
   <strong>Légende :</strong>
   <ul class="unstyled">
     <li><span class="label">transparent</span> : Les utilisateurs peuvent voir les votes des autres</li>
     <li><span class="label">public</span> : Tout le monde peut voir le vote</li>
-    <li><span class="label">choix multiple</span> : Les utilisateurs peuvent effectuer plusieurs choix</li>
+    <li><span class="label">multiple</span> : Les utilisateurs peuvent effectuer plusieurs choix</li>
   </ul>
   </p>
   {% endif %}
+</div>
 {% endblock %}