X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=main.py;h=46653b34600176c9d1859b9ff29fe40b20267e5b;hb=f43f9263fc772dd9efcaeb62eff129d847ff9e86;hp=bb092acbb8232511128a5aa49640a65b7809bdf7;hpb=df5ece768e1f6197ca618c89490563e0420fce9d;p=cavote.git diff --git a/main.py b/main.py index bb092ac..46653b3 100755 --- a/main.py +++ b/main.py @@ -19,9 +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/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__) @@ -32,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): @@ -87,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')) #----------------- @@ -223,26 +225,56 @@ def admin_user_add(): abort(401) if request.method == 'POST': if request.form['email']: - # :TODO:maethor:120528: Check fields - password = "toto" # :TODO:maethor:120528: Generate password - 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]) - g.db.commit() - user = query_db('select * from users where email = ?', [request.form["email"]], one=True) - if user: - for group in request.form.getlist('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')) + 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: + password = keygen() + admin = 0 + if 'admin' in request.form.keys(): + admin = 1 + 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: + 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: + flash(u'Le nom ' + request.form['username'] + u' est déjà pris ! Veuillez en choisir un autre.', 'error') + else: + flash(u"Vous devez spécifier un nom d'utilisateur.", 'error') else: - flash(u'Une erreur s\'est produite.', 'error') + flash(u'Il existe déjà un compte pour cette adresse e-mail : ' + request.form['email'], 'error') else: flash(u"Vous devez spécifier une adresse email.", 'error') groups = query_db('select * from groups where system=0') @@ -291,20 +323,27 @@ 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 + ') join (' + nb_votes + ') on id = 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' if votes == 'all': votes = query_db(basequery + ' order by id desc') elif votes == 'archive': - votes = query_db(basequery + ' and date_end < (?) order by id desc', [today]) + votes = query_db(basequery + ' and is_terminated=1 order by id desc') elif votes == 'current': - votes = query_db(basequery + ' and date_end >= (?) order by id desc', [today]) + votes = query_db(basequery + ' and is_terminated=0 order by id desc') + 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 = ?' + votes = query_db(basequery + ' and votes.id not in (' + already_voted + ') and is_terminated=0', [get_userid(), get_userid()]) else: abort(404) for vote in votes: - vote['percent'] = int((float(vote['nb_votes']) / float(vote['max_votes'])) * 100) + if not vote.get('nb_votes'): + vote['nb_votes'] = 0 + 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) #------ @@ -313,18 +352,23 @@ def votes(votes): def can_see_vote(idvote, iduser=-1): vote = query_db('select * from votes where id=?', [idvote], one=True) if vote is None: - abort(404) + return False if not vote['is_public']: user = query_db('select * from users where id=?', [iduser], one=True) - if user is None: # :TODO:maethor:120604: Check others things (groups) + if query_db('select * from user_group where id_user = ? and id_group = ?', [iduser, vote['id']], one=True) is None: return False return True def can_vote(idvote, iduser=-1): - if iduser > 0: - if can_see_vote(idvote, iduser): - if not has_voted(idvote, iduser): - return True # :TODO:maethor:120529: Check others things (groups) + vote = query_db('select * from votes where id=?', [idvote], one=True) + if vote is None: + return False + 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_group']], one=True): + return True return False def has_voted(idvote, iduser=-1): @@ -339,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]) @@ -369,12 +417,13 @@ 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) 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')) + flash(u'Vous n\'avez pas le droit de voir ce vote, désolé.') + return redirect(url_for('home')) @app.route('/vote/deletechoices//') def vote_deletechoices(idvote, iduser): @@ -425,6 +474,11 @@ def admin_vote_add(): flash(u'Une erreur est survenue !', 'error') return redirect(url_for('home')) else: + if request.form['pattern'] in PATTERNS.keys(): + pattern = PATTERNS[request.form['pattern']] + for choice in pattern: + g.db.execute('insert into choices (name, id_vote) values (?, ?)', [choice, vote['id']]) + g.db.commit() flash(u"Le vote a été créé", 'info') return redirect(url_for('admin_vote_edit', voteid=vote['id'])) else: @@ -432,7 +486,7 @@ def admin_vote_add(): else: flash(u'Vous devez spécifier un titre.', 'error') groups = query_db('select * from groups') - return render_template('admin_vote_new.html', groups=groups) + return render_template('admin_vote_new.html', groups=groups, patterns=PATTERNS) @app.route('/admin/votes/edit/', methods=['GET', 'POST']) def admin_vote_edit(voteid): @@ -451,14 +505,19 @@ def admin_vote_edit(voteid): if 'public' in request.form.keys(): public = 1 isopen = 0 + isterminated = 0 if request.form['status'] == 'Ouvert': choices = query_db('select id_vote, count(*) as nb from choices where id_vote = ? group by id_vote', [voteid], one=True) if choices is not None and choices['nb'] >= 2: isopen = 1 else: flash(u'Vous devez proposer au moins deux choix pour ouvrir le vote.', 'error') - g.db.execute('update votes set title = ?, description = ?, category = ?, is_transparent = ?, is_public = ?, is_open = ? where id = ?', - [request.form['title'], request.form['description'], request.form['category'], transparent, public, isopen, voteid]) + elif request.form['status'] == u'Terminé': + 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.commit() vote = query_db('select * from votes where id = ?', [voteid], one=True) flash(u"Le vote a bien été mis à jour.", "success")