1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-12-23 01:16:18 +00:00

Web UI: split GET and POST handlers

This commit is contained in:
spl0k 2017-11-22 22:39:54 +01:00
parent b998bb0684
commit b7e9914246
3 changed files with 104 additions and 74 deletions

View File

@ -76,14 +76,22 @@ def update_clients(uid):
flash('Clients preferences updated.') flash('Clients preferences updated.')
return user_profile(uid) return user_profile(uid)
@app.route('/user/<uid>/changeusername', methods = [ 'GET', 'POST' ]) @app.route('/user/<uid>/changeusername')
@admin_only @admin_only
def change_username(uid): def change_username_form(uid):
code, user = UserManager.get(store, uid)
if code != UserManager.SUCCESS:
return redirect(url_for('index'))
return render_template('change_username.html', user = user)
@app.route('/user/<uid>/changeusername', methods = [ 'POST' ])
@admin_only
def change_username_post(uid):
code, user = UserManager.get(store, uid) code, user = UserManager.get(store, uid)
if code != UserManager.SUCCESS: if code != UserManager.SUCCESS:
return redirect(url_for('index')) return redirect(url_for('index'))
if request.method == 'POST':
username = request.form.get('user') username = request.form.get('user')
if username in ('', None): if username in ('', None):
flash('The username is required') flash('The username is required')
@ -98,15 +106,26 @@ def change_username(uid):
user.admin = admin user.admin = admin
store.commit() store.commit()
flash("User '%s' updated." % username) flash("User '%s' updated." % username)
return redirect(url_for('user_profile', uid = uid))
else: else:
flash("No changes for '%s'." % username) flash("No changes for '%s'." % username)
return redirect(url_for('user_profile', uid = uid)) return redirect(url_for('user_profile', uid = uid))
return render_template('change_username.html', user = user) @app.route('/user/<uid>/changemail')
def change_mail_form(uid):
@app.route('/user/<uid>/changemail', methods = [ 'GET', 'POST' ]) if uid == 'me':
def change_mail(uid): user = request.user
elif not request.user.admin:
return redirect(url_for('index'))
else:
code, user = UserManager.get(store, uid)
if code != UserManager.SUCCESS:
return redirect(url_for('index'))
return render_template('change_mail.html', user = user)
@app.route('/user/<uid>/changemail', methods = [ 'POST' ])
def change_mail_post(uid):
if uid == 'me': if uid == 'me':
user = request.user user = request.user
elif not request.user.admin: elif not request.user.admin:
@ -116,17 +135,27 @@ def change_mail(uid):
if code != UserManager.SUCCESS: if code != UserManager.SUCCESS:
return redirect(url_for('index')) return redirect(url_for('index'))
if request.method == 'POST':
mail = request.form.get('mail') mail = request.form.get('mail')
# No validation, lol. # No validation, lol.
user.mail = mail user.mail = mail
store.commit() store.commit()
return redirect(url_for('user_profile', uid = uid)) return redirect(url_for('user_profile', uid = uid))
return render_template('change_mail.html', user = user) @app.route('/user/<uid>/changepass')
def change_password_form(uid):
@app.route('/user/<uid>/changepass', methods = [ 'GET', 'POST' ]) if uid == 'me':
def change_password(uid): user = request.user
elif not request.user.admin:
return redirect(url_for('index'))
else:
code, user = UserManager.get(store, uid)
if code != UserManager.SUCCESS:
return redirect(url_for('index'))
return render_template('change_pass.html', user = user)
@app.route('/user/<uid>/changepass', methods = [ 'POST' ])
def change_password_post(uid):
if uid == 'me': if uid == 'me':
user = request.user user = request.user
elif not request.user.admin: elif not request.user.admin:
@ -136,17 +165,16 @@ def change_password(uid):
if code != UserManager.SUCCESS: if code != UserManager.SUCCESS:
return redirect(url_for('index')) return redirect(url_for('index'))
if request.method == 'POST':
error = False error = False
if uid == 'me' or uid == str(request.user.id): if user.id == request.user.id:
current, new, confirm = map(request.form.get, [ 'current', 'new', 'confirm' ]) current = request.form.get('current')
if current in ('', None): if not current:
flash('The current password is required') flash('The current password is required')
error = True error = True
else:
new, confirm = map(request.form.get, [ 'new', 'confirm' ]) new, confirm = map(request.form.get, [ 'new', 'confirm' ])
if new in ('', None): if not new:
flash('The new password is required') flash('The new password is required')
error = True error = True
if new != confirm: if new != confirm:
@ -154,7 +182,7 @@ def change_password(uid):
error = True error = True
if not error: if not error:
if uid == 'me' or uid == str(request.user.id): if user.id == request.user.id:
status = UserManager.change_password(store, user.id, current, new) status = UserManager.change_password(store, user.id, current, new)
else: else:
status = UserManager.change_password2(store, user.name, new) status = UserManager.change_password2(store, user.name, new)
@ -165,20 +193,22 @@ def change_password(uid):
flash('Password changed') flash('Password changed')
return redirect(url_for('user_profile', uid = uid)) return redirect(url_for('user_profile', uid = uid))
return render_template('change_pass.html', user = user) return change_password_form(uid)
@app.route('/user/add', methods = [ 'GET', 'POST' ]) @app.route('/user/add')
@admin_only @admin_only
def add_user(): def add_user_form():
if request.method == 'GET':
return render_template('adduser.html') return render_template('adduser.html')
@app.route('/user/add', methods = [ 'POST' ])
@admin_only
def add_user_post():
error = False error = False
(name, passwd, passwd_confirm, mail, admin) = map(request.form.get, [ 'user', 'passwd', 'passwd_confirm', 'mail', 'admin' ]) (name, passwd, passwd_confirm, mail, admin) = map(request.form.get, [ 'user', 'passwd', 'passwd_confirm', 'mail', 'admin' ])
if name in (None, ''): if not name:
flash('The name is required.') flash('The name is required.')
error = True error = True
if passwd in (None, ''): if not passwd:
flash('Please provide a password.') flash('Please provide a password.')
error = True error = True
elif passwd != passwd_confirm: elif passwd != passwd_confirm:
@ -198,7 +228,7 @@ def add_user():
else: else:
flash(UserManager.error_str(status)) flash(UserManager.error_str(status))
return render_template('adduser.html') return add_user_form()
@app.route('/user/del/<uid>') @app.route('/user/del/<uid>')
@admin_only @admin_only

View File

@ -39,9 +39,9 @@
<input type="text" class="form-control" id="email" placeholder="{{ user.mail }}" readonly> <input type="text" class="form-control" id="email" placeholder="{{ user.mail }}" readonly>
<div class="input-group-btn"> <div class="input-group-btn">
{% if request.user.id == user.id %} {% if request.user.id == user.id %}
<a href="{{ url_for('change_mail', uid = 'me') }}" class="btn btn-default">Change eMail</a> <a href="{{ url_for('change_mail_form', uid = 'me') }}" class="btn btn-default">Change eMail</a>
{% else %} {% else %}
<a href="{{ url_for('change_mail', uid = user.id) }}" class="btn btn-default">Change eMail</a> <a href="{{ url_for('change_mail_form', uid = user.id) }}" class="btn btn-default">Change eMail</a>
{% endif %} {% endif %}
</div> </div>
</div> </div>
@ -83,10 +83,10 @@
</div> </div>
</div> </div>
{% if request.user.id == user.id %} {% if request.user.id == user.id %}
<a href="{{ url_for('change_password', uid = 'me') }}" class="btn btn-default">Change password</a></li> <a href="{{ url_for('change_password_form', uid = 'me') }}" class="btn btn-default">Change password</a></li>
{% else %} {% else %}
<a href="{{ url_for('change_username', uid = user.id) }}" class="btn btn-default">Change username or admin status</a></li> <a href="{{ url_for('change_username_form', uid = user.id) }}" class="btn btn-default">Change username or admin status</a></li>
<a href="{{ url_for('change_password', uid = user.id) }}" class="btn btn-default">Change password</a></li> <a href="{{ url_for('change_password_form', uid = user.id) }}" class="btn btn-default">Change password</a></li>
{% endif %} {% endif %}
{% if clients.count() %} {% if clients.count() %}
<div class="page-header"> <div class="page-header">

View File

@ -32,7 +32,7 @@
</tbody> </tbody>
</table> </table>
<div class="btn-toolbar" role="toolbar"> <div class="btn-toolbar" role="toolbar">
<a href="{{ url_for('add_user') }}" class="btn btn-default">Add</a> <a href="{{ url_for('add_user_form') }}" class="btn btn-default">Add</a>
<a href="{{ url_for('export_users') }}" class="btn btn-default">Export</a> <a href="{{ url_for('export_users') }}" class="btn btn-default">Export</a>
<a href="{{ url_for('import_users') }}" class="btn btn-default">Import</a> <a href="{{ url_for('import_users') }}" class="btn btn-default">Import</a>
</div> </div>