X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=main.py;h=4832fd3eddda2064045d5d721fdb8a72c453face;hb=2f8c7318775a89a197598b189c51979cb01dc990;hp=b4601138436acf3cfacfa697b987536d44b37534;hpb=4036f1d5d981c8b9e61b5016b86dd76fcd25b1e9;p=cavote.git diff --git a/main.py b/main.py index b460113..4832fd3 100755 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ from flask import Flask, request, session, g, redirect, url_for, abort, \ render_template, flash -from flaskext.openid import OpenID +from flask_openid import OpenID import sqlite3 from datetime import date, time, timedelta, datetime import time @@ -15,17 +15,9 @@ import hashlib import smtplib import string -DATABASE = '/tmp/cavote.db' -PASSWD_SALT = 'change this value to some random chars!' -SECRET_KEY = '{J@uRKO,xO-PK7B,jF?>iHbxLasF9s#zjOoy=+:' -DEBUG = True -TITLE = u"Cavote FFDN" -EMAIL = '"' + TITLE + '"' + ' <' + u"cavote@ffdn.org" + '>' -VERSION = "cavote 0.1.1" -SMTP_SERVER = "127.0.0.1" -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__) +from settings import * + +app = Flask(__name__) app.config.from_object(__name__) oid = OpenID(app) @@ -190,7 +182,7 @@ def login_key(userid, key): def user(userid): if int(userid) != get_userid(): abort(401) - groups = query_db('select * from groups join user_group on id=id_group where id_user = ?', userid) + groups = query_db('select * from groups join user_group on id=id_group where id_user = ?', (userid,)) return render_template('user.html', groups=groups) @app.route('/user/settings/', methods=['GET', 'POST']) @@ -200,8 +192,8 @@ def user_edit(userid): if request.method == 'POST': if query_db('select * from users where email=? and id!=?', [request.form['email'], userid], one=True) is None: if query_db('select * from users where name=? and id!=?', [request.form['name'], userid], one=True) is None: - g.db.execute('update users set email = ?, name = ?, organization = ? where id = ?', - [request.form['email'], request.form['name'], request.form['organization'], session['user']['id']]) + g.db.execute('update users set email = ?, openid = ?, name = ?, organization = ? where id = ?', + [request.form['email'], request.form['openid'], request.form['name'], request.form['organization'], session['user']['id']]) g.db.commit() disconnect_user() user = query_db('select * from users where id=?', [userid], one=True) @@ -334,8 +326,8 @@ def admin_user_edit(iduser): admin = 0 if 'admin' in request.form.keys(): admin = 1 - g.db.execute('update users set email = ?, name = ?, organization = ?, is_admin = ? where id = ?', - [request.form['email'], request.form['name'], request.form['organization'], admin, iduser]) + g.db.execute('update users set email = ?, name = ?, organization = ?, openid= ?, is_admin = ? where id = ?', + [request.form['email'], request.form['name'], request.form['organization'], request.form['openid'], admin, iduser]) g.db.commit() groups = request.form.getlist('groups') groups.append('1') @@ -418,7 +410,7 @@ def votes(votes): 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' + 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 and is_hidden=0' if votes == 'all': votes = query_db(basequery + ' order by date_end') elif votes == 'archive': @@ -535,7 +527,7 @@ def vote_deletechoices(idvote, iduser): 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') + votes = query_db('select *, votes.id as voteid, groups.name as groupname from votes join groups on groups.id=votes.id_group where is_hidden=0 order by id desc') return render_template('admin_votes.html', votes=votes, today=date.today().strftime("%Y-%m-%d")) @app.route('/admin/votes/add', methods=['GET', 'POST']) @@ -606,14 +598,36 @@ def admin_vote_edit(voteid): 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 + previousvote = query_db('select id, is_open, id_group from votes where id = ?', [voteid], one=True) + if previousvote is None or previousvote['is_open'] == 0: + users_to_vote = query_db('select users.email, users.name from users join user_group on users.id=user_group.id_user where user_group.id_group = ?', [previousvote['id_group']]) + for user in users_to_vote: + link = request.url_root + url_for('vote', idvote=voteid) + BODY = string.join(( + "From: %s" % EMAIL, + "To: %s" % user['email'], + "Subject: [Cavote] A vote has been opened for your group", + "Date: %s" % time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()).decode('utf-8'), + "X-Mailer: %s" % VERSION, + "", + "A vote has been opened and you are in a group concerned by it : %s" % request.form['title'], + "", + "This link will bring you to the form where you will be able to vote :", + link, + "", + "If you think this mail is not for you, please ignore and delete it." + ), "\r\n") + server = smtplib.SMTP(SMTP_SERVER) + server.sendmail(EMAIL, [user['email']], BODY) + server.quit() else: flash(u'Vous devez proposer au moins deux choix pour ouvrir le vote.', 'error') 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 = ?, date_end = ? where id = ?', - [request.form['title'], request.form['description'], request.form['category'], transparent, public, isopen, isterminated, date_end, voteid]) + g.db.execute('update votes set title = ?, description = ?, category = ?, is_transparent = ?, is_public = ?, is_open = ?, is_terminated = ?, date_end = ?, reminder_last_days = ? where id = ?', + [request.form['title'], request.form['description'], request.form['category'], transparent, public, isopen, isterminated, date_end, request.form['reminder'], 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") @@ -628,6 +642,17 @@ def admin_vote_edit(voteid): 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/delete/') +def admin_vote_del(idvote): + if not session.get('user').get('is_admin'): + abort(401) + vote = query_db('select * from votes where id = ?', [idvote], one=True) + if vote is None: + abort(404) + g.db.execute('update votes set is_hidden=1 where id = ?', [idvote]) + g.db.commit() + return redirect(url_for('admin_votes')) + @app.route('/admin/votes/addchoice/', methods=['POST']) def admin_vote_addchoice(voteid): if not session.get('user').get('is_admin'):