From 81ed16603dbd50672b3afeca6bcee1aad9a4b0da Mon Sep 17 00:00:00 2001
From: Patrick Jentsch
Date: Mon, 13 Sep 2021 16:36:48 +0200
Subject: [PATCH] Remove current_app and current_user usage in forms.
---
app/__init__.py | 31 ++++++++++++++++++++-----------
app/admin/forms.py | 5 ++---
app/admin/routes.py | 2 +-
app/auth/__init__.py | 3 +++
app/auth/forms.py | 4 ++--
app/settings/forms.py | 13 ++++---------
app/settings/routes.py | 4 ++--
config.py | 1 -
8 files changed, 34 insertions(+), 29 deletions(-)
diff --git a/app/__init__.py b/app/__init__.py
index 9a1f7df8..4649a090 100644
--- a/app/__init__.py
+++ b/app/__init__.py
@@ -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
diff --git a/app/admin/forms.py b/app/admin/forms.py
index a0f796d6..eb0ed231 100644
--- a/app/admin/forms.py
+++ b/app/admin/forms.py
@@ -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()]
diff --git a/app/admin/routes.py b/app/admin/routes.py
index c9a04053..3d7de6fb 100644
--- a/app/admin/routes.py
+++ b/app/admin/routes.py
@@ -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
diff --git a/app/auth/__init__.py b/app/auth/__init__.py
index 6f6ba82d..505e7e42 100644
--- a/app/auth/__init__.py
+++ b/app/auth/__init__.py
@@ -1,5 +1,8 @@
from flask import Blueprint
+USERNAME_REGEX = '^[A-Za-zÄÖÜäöüß0-9_.]*$'
+
+
bp = Blueprint('auth', __name__)
from . import routes
diff --git a/app/auth/forms.py b/app/auth/forms.py
index 98dab654..8509bc44 100644
--- a/app/auth/forms.py
+++ b/app/auth/forms.py
@@ -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')]
)
diff --git a/app/settings/forms.py b/app/settings/forms.py
index 5c822fd9..d402fad2 100644
--- a/app/settings/forms.py
+++ b/app/settings/forms.py
@@ -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
diff --git a/app/settings/routes.py b/app/settings/routes.py
index ebc3a385..9cc9960a 100644
--- a/app/settings/routes.py
+++ b/app/settings/routes.py
@@ -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
diff --git a/config.py b/config.py
index 3af786f9..913229cb 100644
--- a/config.py
+++ b/config.py
@@ -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):