This commit is contained in:
Patrick Jentsch 2020-10-26 11:17:25 +01:00
parent 4f8426c0b0
commit f4be897c73
4 changed files with 41 additions and 24 deletions

View File

@ -1,3 +1,4 @@
from flask import current_app
from flask_wtf import FlaskForm from flask_wtf import FlaskForm
from wtforms import (BooleanField, SelectField, StringField, SubmitField, from wtforms import (BooleanField, SelectField, StringField, SubmitField,
ValidationError) ValidationError)
@ -6,31 +7,38 @@ from ..models import Role, User
class EditUserForm(FlaskForm): class EditUserForm(FlaskForm):
email = StringField('Email', email = StringField('Email', validators=[DataRequired(), Email()])
validators=[DataRequired(), Length(1, 64), Email()]) username = StringField(
username = StringField('Username', 'Username',
validators=[DataRequired(), Length(1, 64), validators=[DataRequired(),
Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0, Length(1, 64),
'Usernames must have only ' Regexp(current_app.config['ALLOWED_USERNAME_REGEX'],
'letters, numbers, dots or ' message='Usernames must have only letters, numbers,'
'underscores')]) ' dots or underscores')]
)
confirmed = BooleanField('Confirmed') confirmed = BooleanField('Confirmed')
role = SelectField('Role', coerce=int) role = SelectField(
name = StringField('Real name', validators=[Length(0, 64)]) 'Role',
choices = [(role.id, role.name)
for role in Role.query.order_by(Role.name).all()],
coerce=int
)
submit = SubmitField('Update Profile') submit = SubmitField('Update Profile')
def __init__(self, user, *args, **kwargs): def __init__(self, user, *args, **kwargs):
super(EditUserForm, self).__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.role.choices = [(role.id, role.name)
for role in Role.query.order_by(Role.name).all()]
self.user = user self.user = user
self.email.data = self.email.data or user.email
self.username.data = self.username.data or user.username
self.confirmed.data = self.confirmed.data or user.confirmed
self.role.data = self.role.data or user.role_id
def validate_email(self, field): def validate_email(self, field):
if field.data != self.user.email and \ if (field.data != self.user.email
User.query.filter_by(email=field.data).first(): and User.query.filter_by(email=field.data).first()):
raise ValidationError('Email already registered.') raise ValidationError('Email already registered.')
def validate_username(self, field): def validate_username(self, field):
if field.data != self.user.username and \ if (field.data != self.user.username
User.query.filter_by(username=field.data).first(): and User.query.filter_by(username=field.data).first()):
raise ValidationError('Username already in use.') raise ValidationError('Username already in use.')

View File

@ -5,7 +5,7 @@ from .forms import EditUserForm
from .. import db from .. import db
from ..decorators import admin_required from ..decorators import admin_required
from ..models import Role, User from ..models import Role, User
from ..profile import tasks as profile_tasks from ..settings import tasks as settings_tasks
@admin.route('/users') @admin.route('/users')
@ -34,7 +34,7 @@ def user(user_id):
@login_required @login_required
@admin_required @admin_required
def delete_user(user_id): def delete_user(user_id):
profile_tasks.delete_user(user_id) settings_tasks.delete_user(user_id)
flash('User has been deleted!') flash('User has been deleted!')
return redirect(url_for('admin.index')) return redirect(url_for('admin.index'))
@ -54,10 +54,6 @@ def edit_user(user_id):
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))
edit_user_form.email.data = user.email
edit_user_form.username.data = user.username
edit_user_form.confirmed.data = user.confirmed
edit_user_form.role.data = user.role_id
return render_template('admin/edit_user.html.j2', return render_template('admin/edit_user.html.j2',
edit_user_form=edit_user_form, edit_user_form=edit_user_form,
title='Edit user', title='Edit user',

13
web/app/settings/tasks.py Normal file
View File

@ -0,0 +1,13 @@
from .. import db
from ..decorators import background
from ..models import User
@background
def delete_user(user_id, *args, **kwargs):
with kwargs['app'].app_context():
user = User.query.get(user_id)
if user is None:
raise Exception('User {} not found'.format(user_id))
user.delete()
db.session.commit()

View File

@ -1,6 +1,6 @@
from flask import current_app, flash, redirect, render_template, url_for from flask import current_app, flash, redirect, render_template, url_for
from flask_login import current_user, login_required from flask_login import current_user, login_required
from . import settings from . import settings, tasks
from .forms import (ChangePasswordForm, EditGeneralSettingsForm, from .forms import (ChangePasswordForm, EditGeneralSettingsForm,
EditNotificationSettingsForm) EditNotificationSettingsForm)
from .. import db from .. import db