From 00a6f73a96b004e9323779ab42fa0eb435be12dd Mon Sep 17 00:00:00 2001 From: Guillaume Subiron Date: Mon, 28 May 2012 17:19:17 +0200 Subject: [PATCH] Admin can add users --- main.py | 65 ++++++++++++++++++++++++++--------------- templates/add_user.html | 53 +++++++++++++++++++++++++++++++++ templates/layout.html | 7 +++-- templates/new_vote.html | 6 ++-- 4 files changed, 103 insertions(+), 28 deletions(-) create mode 100644 templates/add_user.html diff --git a/main.py b/main.py index 7c6521d..cdfc667 100755 --- a/main.py +++ b/main.py @@ -144,6 +144,26 @@ def user_password(userid): #------------ # User admin +@app.route('/users/admin/add', methods=['GET', 'POST']) +def add_user(): + if not session.get('is_admin'): + 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) values (?, ?, ?, ?, ?)', + [request.form['email'], request.form['username'], request.form['organization'], password, admin]) + g.db.commit() + # :TODO:maethor:120528: Send mail + flash(u'Le nouvel utilisateur a été créé avec succès', 'success') + return redirect(url_for('home')) + else: + flash(u"Vous devez spécifier une adresse email.", 'error') + return render_template('add_user.html') #------------ # Votes list @@ -164,32 +184,31 @@ def show_votes(votes): #------------- # Votes admin -@app.route('/votes/admin/new') -def new_vote(): - if not session.get('is_admin'): - abort(401) - return render_template('new_vote.html') - -@app.route('/votes/admin/add', methods=['POST']) +@app.route('/votes/admin/add', methods=['GET', 'POST']) def add_vote(): if not session.get('is_admin'): abort(401) - date_begin = date.today() - date_end = date.today() + timedelta(days=int(request.form['days'])) - transparent = 0 - public = 0 - multiplechoice = 0 - if 'transparent' in request.form.keys(): - transparent = 1 - if 'public' in request.form.keys(): - public = 1 - if 'multiplechoice' in request.form.keys(): - multiplechoice = 1 - g.db.execute('insert into votes (title, description, date_begin, date_end, is_transparent, is_public, is_multiplechoice) values (?, ?, ?, ?, ?, ?, ?)', - [request.form['title'], request.form['description'], date_begin, date_end, transparent, public, multiplechoice]) - g.db.commit() - flash('New entry was successfully posted', 'info') - return redirect(url_for('home')) + if request.method == 'POST': + if request.form['title']: + date_begin = date.today() + date_end = date.today() + timedelta(days=int(request.form['days'])) + transparent = 0 + public = 0 + multiplechoice = 0 + if 'transparent' in request.form.keys(): + transparent = 1 + if 'public' in request.form.keys(): + public = 1 + if 'multiplechoice' in request.form.keys(): + multiplechoice = 1 + g.db.execute('insert into votes (title, description, date_begin, date_end, is_transparent, is_public, is_multiplechoice) values (?, ?, ?, ?, ?, ?, ?)', + [request.form['title'], request.form['description'], date_begin, date_end, transparent, public, multiplechoice]) + g.db.commit() + flash('New entry was successfully posted', 'info') + return redirect(url_for('home')) + else: + flash(u'Vous devez spécifier un titre.', 'error') + return render_template('new_vote.html') #------ # Main diff --git a/templates/add_user.html b/templates/add_user.html new file mode 100644 index 0000000..f80a1b8 --- /dev/null +++ b/templates/add_user.html @@ -0,0 +1,53 @@ +{% extends "layout.html" %} +{% block body %} + +
+
+
+
Ajouter un utilisateur +
+ +
+ + * +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+
+
+
+
+ +{% endblock %} diff --git a/templates/layout.html b/templates/layout.html index 5134106..e3f8372 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -25,12 +25,15 @@ {% if session.is_admin %} Admin {% endif %}
- {% if 'username' in session %} + {% if 'userid' in session %} {{ session.username }}