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

View File

@ -5,7 +5,7 @@ from .forms import EditUserForm
from .. import db
from ..decorators import admin_required
from ..models import Role, User
from ..profile import tasks as profile_tasks
from ..settings import tasks as settings_tasks
@admin.route('/users')
@ -34,7 +34,7 @@ def user(user_id):
@login_required
@admin_required
def delete_user(user_id):
profile_tasks.delete_user(user_id)
settings_tasks.delete_user(user_id)
flash('User has been deleted!')
return redirect(url_for('admin.index'))
@ -54,10 +54,6 @@ def edit_user(user_id):
db.session.commit()
flash('The profile has been updated.')
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',
edit_user_form=edit_user_form,
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_login import current_user, login_required
from . import settings
from . import settings, tasks
from .forms import (ChangePasswordForm, EditGeneralSettingsForm,
EditNotificationSettingsForm)
from .. import db