Change edit user form name

This commit is contained in:
Patrick Jentsch 2019-11-15 14:01:36 +01:00
parent 5fd9711da9
commit fc560933bc
3 changed files with 32 additions and 42 deletions

View File

@ -5,29 +5,22 @@ from wtforms import (BooleanField, SelectField, StringField, SubmitField,
from wtforms.validators import DataRequired, Email, Length, Regexp from wtforms.validators import DataRequired, Email, Length, Regexp
class EditProfileAdminForm(FlaskForm): class EditUserForm(FlaskForm):
email = StringField('Email', email = StringField('Email',
validators=[DataRequired(), Length(1, 64), Email()]) validators=[DataRequired(), Length(1, 64), Email()])
username = StringField( username = StringField('Username',
'Username', validators=[DataRequired(), Length(1, 64),
validators=[ Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0,
DataRequired(), 'Usernames must have only '
Length(1, 64), 'letters, numbers, dots or '
Regexp( 'underscores')])
'^[A-Za-z][A-Za-z0-9_.]*$',
0,
'Usernames must have only letters, numbers, dots or '
'underscores'
)
]
)
confirmed = BooleanField('Confirmed') confirmed = BooleanField('Confirmed')
role = SelectField('Role', coerce=int) role = SelectField('Role', coerce=int)
name = StringField('Real name', validators=[Length(0, 64)]) name = StringField('Real name', validators=[Length(0, 64)])
submit = SubmitField('Update Profile') submit = SubmitField('Update Profile')
def __init__(self, user, *args, **kwargs): def __init__(self, user, *args, **kwargs):
super(EditProfileAdminForm, self).__init__(*args, **kwargs) super(EditUserForm, self).__init__(*args, **kwargs)
self.role.choices = [(role.id, role.name) self.role.choices = [(role.id, role.name)
for role in Role.query.order_by(Role.name).all()] for role in Role.query.order_by(Role.name).all()]
self.user = user self.user = user

View File

@ -6,7 +6,7 @@ from flask import current_app, flash, redirect, render_template, url_for
from flask_login import login_required from flask_login import login_required
from threading import Thread from threading import Thread
from . import admin from . import admin
from .forms import EditProfileAdminForm from .forms import EditUserForm
from .tables import AdminUserItem, AdminUserTable from .tables import AdminUserItem, AdminUserTable
@ -51,19 +51,16 @@ def delete_user(user_id):
@admin_required @admin_required
def edit_user(user_id): def edit_user(user_id):
user = User.query.get_or_404(user_id) user = User.query.get_or_404(user_id)
form = EditProfileAdminForm(user=user) edit_user_form = EditUserForm(user=user)
if form.validate_on_submit(): if edit_user_form.validate_on_submit():
user.email = form.email.data user.email = edit_user_form.email.data
user.username = form.username.data user.username = edit_user_form.username.data
user.confirmed = form.confirmed.data user.confirmed = edit_user_form.confirmed.data
user.role = Role.query.get(form.role.data) user.role = Role.query.get(edit_user_form.role.data)
db.session.add(user) db.session.add(user)
db.session.commit() db.session.commit()
flash('The profile has been updated.') flash('The profile has been updated.')
return redirect(url_for('admin.edit_user', user_id=user.id)) return redirect(url_for('admin.edit_user', user_id=user.id))
form.email.data = user.email return render_template('admin/edit_user.html.j2',
form.username.data = user.username edit_user_form=edit_user_form,
form.confirmed.data = user.confirmed
form.role.data = user.role_id
return render_template('admin/edit_user.html.j2', form=form,
title='Administration: Edit user', user=user) title='Administration: Edit user', user=user)

View File

@ -11,45 +11,45 @@
<div class="card"> <div class="card">
<form method="POST"> <form method="POST">
<div class="card-content"> <div class="card-content">
{{ form.hidden_tag() }} {{ edit_user_form.hidden_tag() }}
<div class="input-field "> <div class="input-field ">
<i class="material-icons prefix">account_circle</i> <i class="material-icons prefix">account_circle</i>
{{ form.username() }} {{ edit_user_form.username() }}
{{ form.username.label }} {{ edit_user_form.username.label }}
{% for error in form.username.errors %} {% for error in edit_user_form.username.errors %}
<span class="helper-text red-text">{{ error }}</span> <span class="helper-text red-text">{{ error }}</span>
{% endfor %} {% endfor %}
</div> </div>
<div class="input-field"> <div class="input-field">
<i class="material-icons prefix">mail</i> <i class="material-icons prefix">mail</i>
{{ form.email() }} {{ edit_user_form.email() }}
{{ form.email.label }} {{ edit_user_form.email.label }}
{% for error in form.email.errors %} {% for error in edit_user_form.email.errors %}
<span class="helper-text red-text">{{ error }}</span> <span class="helper-text red-text">{{ error }}</span>
{% endfor %} {% endfor %}
</div> </div>
<div class="input-field"> <div class="input-field">
<i class="material-icons prefix">swap_vert</i> <i class="material-icons prefix">swap_vert</i>
{{ form.role() }} {{ edit_user_form.role() }}
{{ form.role.label }} {{ edit_user_form.role.label }}
{% for error in form.role.errors %} {% for error in edit_user_form.role.errors %}
<span class="helper-text red-text">{{ error }}</span> <span class="helper-text red-text">{{ error }}</span>
{% endfor %} {% endfor %}
</div> </div>
<div class="switch"> <div class="switch">
<i class="material-icons prefix">check</i> <i class="material-icons prefix">check</i>
<label class="active" for="{{form.confirmed.name}}"> <label class="active" for="{{ edit_user_form.confirmed.name }}">
Confirmed status: Confirmed status:
Off Off
{% if form.confirmed.data == True %} {% if edit_user_form.confirmed.data == True %}
<input type="checkbox" id="{{ form.confirmed.name }}" name="{{ form.confirmed.name }}" checked="checked"> <input type="checkbox" id="{{ edit_user_form.confirmed.name }}" name="{{ edit_user_form.confirmed.name }}" checked="checked">
{% else %} {% else %}
<input type="checkbox" id="{{ form.confirmed.name }}" name="{{ form.confirmed.name }}"> <input type="checkbox" id="{{ edit_user_form.confirmed.name }}" name="{{ edit_user_form.confirmed.name }}">
{% endif %} {% endif %}
<span class="lever"></span> <span class="lever"></span>
On On
</label> </label>
{% for error in form.confirmed.errors %} {% for error in edit_user_form.confirmed.errors %}
<span class="helper-text red-text">{{ error }}</span> <span class="helper-text red-text">{{ error }}</span>
{% endfor %} {% endfor %}
</div> </div>