Remove current_app and current_user usage in forms.

This commit is contained in:
Patrick Jentsch 2021-09-13 16:36:48 +02:00
parent 10483c1e45
commit 81ed16603d
8 changed files with 34 additions and 29 deletions

View File

@ -31,25 +31,34 @@ def create_app(config_name):
socketio.init_app(
app, message_queue=app.config['NOPAQUE_SOCKETIO_MESSAGE_QUEUE_URI'])
with app.app_context():
from .events import socketio as socketio_events
from .events import sqlalchemy as sqlalchemy_events
from .admin import bp as admin_blueprint
from .auth import bp as auth_blueprint
from .corpora import bp as corpora_blueprint
from .errors import bp as errors_blueprint
from .jobs import bp as jobs_blueprint
from .main import bp as main_blueprint
from .services import bp as services_blueprint
from .settings import bp as settings_blueprint
from .events import socketio as socketio_events
from .events import sqlalchemy as sqlalchemy_events
from .admin import bp as admin_blueprint
app.register_blueprint(admin_blueprint, url_prefix='/admin')
from .api import bp as api_blueprint
app.register_blueprint(api_blueprint, url_prefix='/api')
from .auth import bp as auth_blueprint
app.register_blueprint(auth_blueprint, url_prefix='/auth')
from .corpora import bp as corpora_blueprint
app.register_blueprint(corpora_blueprint, url_prefix='/corpora')
from .errors import bp as errors_blueprint
app.register_blueprint(errors_blueprint)
from .jobs import bp as jobs_blueprint
app.register_blueprint(jobs_blueprint, url_prefix='/jobs')
from .main import bp as main_blueprint
app.register_blueprint(main_blueprint)
from .services import bp as services_blueprint
app.register_blueprint(services_blueprint, url_prefix='/services')
from .settings import bp as settings_blueprint
app.register_blueprint(settings_blueprint, url_prefix='/settings')
return app

View File

@ -1,4 +1,3 @@
from flask_login import current_user
from wtforms import BooleanField, SelectField
from ..models import Role
from ..settings.forms import EditGeneralSettingsForm
@ -8,7 +7,7 @@ class EditGeneralSettingsAdminForm(EditGeneralSettingsForm):
confirmed = BooleanField('Confirmed')
role = SelectField('Role', coerce=int)
def __init__(self, user=current_user, *args, **kwargs):
super().__init__(*args, user=user, **kwargs)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.role.choices = [(role.id, role.name)
for role in Role.query.order_by(Role.name).all()]

View File

@ -46,7 +46,7 @@ def delete_user(user_id):
@admin_required
def edit_user(user_id):
user = User.query.get_or_404(user_id)
form = EditGeneralSettingsAdminForm(user=user)
form = EditGeneralSettingsAdminForm(user)
if form.validate_on_submit():
user.setting_dark_mode = form.dark_mode.data
user.email = form.email.data

View File

@ -1,5 +1,8 @@
from flask import Blueprint
USERNAME_REGEX = '^[A-Za-zÄÖÜäöüß0-9_.]*$'
bp = Blueprint('auth', __name__)
from . import routes

View File

@ -1,4 +1,4 @@
from flask import current_app
from . import USERNAME_REGEX
from ..models import User
from flask_wtf import FlaskForm
from wtforms import (BooleanField, PasswordField, StringField, SubmitField,
@ -18,7 +18,7 @@ class RegistrationForm(FlaskForm):
username = StringField(
'Username',
validators=[DataRequired(), Length(1, 64),
Regexp(current_app.config['NOPAQUE_USERNAME_REGEX'],
Regexp(USERNAME_REGEX,
message='Usernames must have only letters, numbers,'
' dots or underscores')]
)

View File

@ -1,9 +1,8 @@
from flask import current_app
from flask_login import current_user
from flask_wtf import FlaskForm
from wtforms import (BooleanField, PasswordField, SelectField, StringField,
SubmitField, ValidationError)
from wtforms.validators import DataRequired, Email, EqualTo, Length, Regexp
from ..auth import USERNAME_REGEX
from ..models import User
@ -18,7 +17,7 @@ class ChangePasswordForm(FlaskForm):
'Confirm new password', validators=[DataRequired()])
submit = SubmitField('Change password')
def __init__(self, user=current_user, *args, **kwargs):
def __init__(self, user, *args, **kwargs):
super().__init__(*args, **kwargs)
self.user = user
@ -35,13 +34,13 @@ class EditGeneralSettingsForm(FlaskForm):
'Benutzername',
validators=[DataRequired(),
Length(1, 64),
Regexp(current_app.config['NOPAQUE_USERNAME_REGEX'],
Regexp(USERNAME_REGEX,
message='Usernames must have only letters, numbers,'
' dots or underscores')]
)
submit = SubmitField('Submit')
def __init__(self, user=current_user, *args, **kwargs):
def __init__(self, user, *args, **kwargs):
super().__init__(*args, **kwargs)
self.user = user
@ -72,7 +71,3 @@ class EditNotificationSettingsForm(FlaskForm):
('none', 'No status update notifications')],
validators=[DataRequired()])
submit = SubmitField('Save settings')
def __init__(self, user=current_user, *args, **kwargs):
super().__init__(*args, **kwargs)
self.user = user

View File

@ -15,7 +15,7 @@ def index():
@bp.route('/change_password', methods=['GET', 'POST'])
@login_required
def change_password():
form = ChangePasswordForm()
form = ChangePasswordForm(current_user._get_current_object())
if form.validate_on_submit():
current_user.password = form.new_password.data
db.session.commit()
@ -28,7 +28,7 @@ def change_password():
@bp.route('/edit_general_settings', methods=['GET', 'POST'])
@login_required
def edit_general_settings():
form = EditGeneralSettingsForm()
form = EditGeneralSettingsForm(current_user._get_current_object())
if form.validate_on_submit():
current_user.email = form.email.data
current_user.setting_dark_mode = form.dark_mode.data

View File

@ -43,7 +43,6 @@ class Config:
NOPAQUE_MAIL_SUBJECT_PREFIX = '[nopaque]'
NOPAQUE_SOCKETIO_MESSAGE_QUEUE_URI = \
os.environ.get('NOPAQUE_SOCKETIO_MESSAGE_QUEUE_URI')
NOPAQUE_USERNAME_REGEX = '^[A-Za-zÄÖÜäöüß0-9_.]*$'
@classmethod
def init_app(cls, app):