X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=main.py;h=21a75000a3010a3e0858ed8ed60adc00083ed86d;hb=d04d0d4a8128e7ac5e0f1a70b5dd06f65ddeed56;hp=c276c3753c3491e34bbc0c25fa23fa8673a5fc7e;hpb=16be4472e8582f8d8d966b625165ef2e57fc21df;p=cavote.git diff --git a/main.py b/main.py index c276c37..21a7500 100755 --- 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,10 +19,9 @@ 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" -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/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__) @@ -93,6 +92,8 @@ def login(): else: connect_user(user) flash(u'Vous êtes connecté. Bienvenue, %s !' % user['name'], 'success') + if request.args.get('continue'): + return redirect(request.args['continue']) return redirect(url_for('home')) return render_template('login.html') @@ -100,6 +101,8 @@ def login(): def logout(): disconnect_user() flash(u'Vous avez été déconnecté.', 'info') + if request.args.get('continue') and not "admin" in request.args.get('continue'): + return redirect(request.args['continue']) return redirect(url_for('home')) #----------------- @@ -116,12 +119,12 @@ 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'], "Subject: [Cavote] Password lost", - "Date: %s" % time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()), + "Date: %s" % time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()).decode('utf-8'), "X-Mailer: %s" % VERSION, "", "You have lost your password.", @@ -246,12 +249,12 @@ 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'], "Subject: [Cavote] Welcome", - "Date: %s" % time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()), + "Date: %s" % time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.gmtime()).decode('utf-8'), "X-Mailer: %s" % VERSION, "", "Hi %s!" % user['name'], @@ -280,6 +283,58 @@ def admin_user_add(): groups = query_db('select * from groups where system=0') return render_template('admin_user_new.html', groups=groups) +@app.route('/admin/users/edit/', methods=['GET', 'POST']) +def admin_user_edit(iduser): + if not session.get('user').get('is_admin'): + abort(401) + user = query_db('select * from users where id = ?', [iduser], one=True) + user['groups'] = query_db('select groups.* from groups join user_group on groups.id = user_group.id_group where id_user = ?', [iduser]) + if user is None: + abort(404) + if request.method == 'POST': + if query_db('select * from users where email=? and id!=?', [request.form['email'], iduser], one=True) is None: + if query_db('select * from users where name=? and id!=?', [request.form['name'], iduser], one=True) is None: + 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.commit() + groups = request.form.getlist('groups') + groups.append('1') + for group in user['groups']: + if not group['id'] in groups: + g.db.execute('delete from user_group where id_user = ? and id_group = ?', [iduser, group['id']]) + g.db.commit() + for group in groups: + group = query_db('select id from groups where id = ?', group, one=True) + if group is None: + flash(u'Le groupe portant l\'id %s n\'existe pas.' % group, 'warning') + else: + if not group in user['groups']: + g.db.execute('insert into user_group values (?, ?)', [user['id'], group['id']]) + g.db.commit() + user = query_db('select * from users where id = ?', [iduser], one=True) + user['groups'] = query_db('select groups.* from groups join user_group on groups.id = user_group.id_group where id_user = ?', [iduser]) + flash(u'Le profil a été mis à jour !', 'success') + else: + flash(u'Le nom ' + request.form['name'] + u' est déjà pris ! Veuillez en choisir un autre.', 'error') + else: + flash(u'Il existe déjà un compte pour cette adresse e-mail : ' + request.form['email'], 'error') + groups = query_db('select * from groups where system=0') + return render_template('admin_user_edit.html', user=user, groups=groups) + +@app.route('/admin/users/delete/') +def admin_user_del(iduser): + if not session.get('user').get('is_admin'): + abort(401) + user = query_db('select * from users where id = ?', [iduser], one=True) + if user is None: + abort(404) + g.db.execute('delete from users where id = ?', [iduser]) + g.db.commit() + return redirect(url_for('admin_users')) + #------------- # Roles admin @@ -287,7 +342,7 @@ def admin_user_add(): def admin_groups(): if not session.get('user').get('is_admin'): abort(401) - groups = query_db('select * from groups') + groups = query_db('select groups.*, count(user_group.id_user) as nb_users from (select groups.*, count(votes.id) as nb_votes from groups left join votes on votes.id_group = groups.id group by groups.id) as groups left join user_group on user_group.id_group = groups.id group by groups.id') return render_template('admin_groups.html', groups=groups) @app.route('/admin/groups/add', methods=['POST']) @@ -323,16 +378,16 @@ 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' 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 = ?' @@ -342,7 +397,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) #------ @@ -416,9 +472,12 @@ 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) + 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')) @@ -440,7 +499,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(): @@ -495,7 +554,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(): @@ -514,19 +575,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/', methods=['POST'])