Debug
[cavote.git] / main.py
diff --git a/main.py b/main.py
index 78998b0..c276c37 100755 (executable)
--- a/main.py
+++ b/main.py
@@ -33,6 +33,7 @@ def connect_db():
 @app.before_request
 def before_request():
     g.db = connect_db()
+    g.db.execute("PRAGMA foreign_keys = ON")
 
 @app.teardown_request
 def teardown_request(exception):
@@ -88,17 +89,17 @@ def login():
     if request.method == 'POST':
         user = valid_login(request.form['username'], request.form['password'])
         if user is None:
-            flash('Invalid username/password', 'error')
+            flash(u'Email ou mot de passe invalide.', 'error')
         else:
             connect_user(user)
-            flash('You were logged in', 'success')
+            flash(u'Vous êtes connecté. Bienvenue, %s !' % user['name'], 'success')
             return redirect(url_for('home'))
     return render_template('login.html')
 
 @app.route('/logout')
 def logout():
     disconnect_user()
-    flash('You were logged out', 'info')
+    flash(u'Vous avez été déconnecté.', 'info')
     return redirect(url_for('home'))
 
 #-----------------
@@ -227,26 +228,45 @@ def admin_user_add():
             if query_db('select * from users where email=?', [request.form['email']], one=True) is None:
                 if request.form['username']:
                     if query_db('select * from users where name=?', [request.form['username']], one=True) is None:
-                        # :TODO:maethor:120528: Check fields
-                        password = "toto" # :TODO:maethor:120528: Generate password
+                        password = keygen()
                         admin = 0
                         if 'admin' in request.form.keys():
                             admin = 1
-                        g.db.execute('insert into users (email, name, organization, password, is_admin, key) values (?, ?, ?, ?, ?, "invalid")',
-                                [request.form['email'], request.form['username'], request.form['organization'], password, admin])
+                        key = keygen()
+                        g.db.execute('insert into users (email, name, organization, password, is_admin, key) values (?, ?, ?, ?, ?, ?)',
+                                [request.form['email'], request.form['username'], request.form['organization'], password, admin, key])
                         g.db.commit()
                         user = query_db('select * from users where email = ?', [request.form["email"]], one=True)
                         if user:
-                          groups = request.form.getlist('groups')
-                          groups.append('1')
-                          for group in groups:
-                              if query_db('select id from groups where id = ?', group, one=True) is None:
-                                  abort(401)
-                              g.db.execute('insert into user_group values (?, ?)', [user['id'], group])
-                              g.db.commit()
-                          # :TODO:maethor:120528: Send mail
-                          flash(u'Le nouvel utilisateur a été créé avec succès', 'success')
-                          return redirect(url_for('admin_users'))
+                            groups = request.form.getlist('groups')
+                            groups.append('1')
+                            for group in groups:
+                                if query_db('select id from groups where id = ?', group, one=True) is None:
+                                    flash(u'Le groupe portant l\'id %s n\'existe pas.' % group, 'warning')
+                                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'])
+                            BODY = string.join((
+                            "From: %s" % EMAIL,
+                            "To: %s" % user['email'],
+                            "Subject: [Cavote] Welcome",
+                            "Date: %s" % time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()),
+                            "X-Mailer: %s" % VERSION,
+                            "",
+                            "Hi %s!" % user['name'],
+                            "Welcome on %s." % TITLE,
+                            "Your account's adresse is : %s." % user['email'],
+                            "",
+                            "To log in for the first time and set your password, please follow this link :",
+                            link,
+                            ""
+                            ), "\r\n")
+                            server = smtplib.SMTP(SMTP_SERVER)
+                            server.sendmail(EMAIL, [user['email']], BODY)
+                            server.quit()
+                            flash(u'Le nouvel utilisateur a été créé avec succès', 'success')
+                            return redirect(url_for('admin_users'))
                         else:
                             flash(u'Une erreur s\'est produite.', 'error')
                     else:
@@ -342,11 +362,11 @@ def can_vote(idvote, iduser=-1):
     vote = query_db('select * from votes where id=?', [idvote], one=True)
     if vote is None:
         return False
-    if not vote['is_finished']:
+    if vote['is_terminated'] == 0:
         if iduser > 0: 
             if can_see_vote(idvote, iduser): 
                 if not has_voted(idvote, iduser):
-                    if query_db('select * from user_group where id_user = ? and id_group = ?', [iduser, vote['id']], one=True):
+                    if query_db('select * from user_group where id_user = ? and id_group = ?', [iduser, vote['id_group']], one=True):
                         return True
     return False
 
@@ -362,14 +382,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])