Skip to content

Commit

Permalink
Allow wtforms to use localized strings
Browse files Browse the repository at this point in the history
While this is not a solution for #59, it is a necessary first step
to address it properly. Please see the discussion in #59 why this
is not a sufficient fix.

Another shortcoming here is that we should probably forward *all*
languages accepted by the user agent to the form to provide the
best degration we can if the primary language picked by the user
is not offered by wtforms.
  • Loading branch information
horazont committed Mar 20, 2021
1 parent 8d72a26 commit 4f54ab4
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 85 deletions.
17 changes: 8 additions & 9 deletions snikket_web/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
abort,
flash,
)
import flask_wtf

from flask_babel import lazy_gettext as _l, _

from . import prosodyclient
from .infra import client, circle_name
from .infra import client, circle_name, BaseForm

bp = Blueprint("admin", __name__, url_prefix="/admin")

Expand All @@ -35,7 +34,7 @@ async def index() -> str:
return await render_template("admin_home.html")


class PasswordResetLinkPost(flask_wtf.FlaskForm): # type: ignore
class PasswordResetLinkPost(BaseForm):
action_create = wtforms.StringField()
action_revoke = wtforms.StringField()

Expand All @@ -55,7 +54,7 @@ async def users() -> str:
)


class DeleteUserForm(flask_wtf.FlaskForm): # type:ignore
class DeleteUserForm(BaseForm):
action_delete = wtforms.SubmitField(
_l("Delete user permanently")
)
Expand Down Expand Up @@ -132,11 +131,11 @@ async def create_password_reset_link() -> typing.Union[str, quart.Response]:
)


class InvitesListForm(flask_wtf.FlaskForm): # type:ignore
class InvitesListForm(BaseForm):
action_revoke = wtforms.StringField()


class InvitePost(flask_wtf.FlaskForm): # type:ignore
class InvitePost(BaseForm):
circles = wtforms.SelectMultipleField(
_l("Invite to circle"),
# NOTE: This is for when/if we ever support multi-group invites.
Expand Down Expand Up @@ -230,7 +229,7 @@ async def invitations() -> typing.Union[str, quart.Response]:
)


class InviteForm(flask_wtf.FlaskForm): # type:ignore
class InviteForm(BaseForm):
action_revoke = wtforms.SubmitField(
_l("Revoke")
)
Expand Down Expand Up @@ -302,7 +301,7 @@ async def edit_invite(id_: str) -> typing.Union[str, quart.Response]:
)


class CirclePost(flask_wtf.FlaskForm): # type:ignore
class CirclePost(BaseForm):
name = wtforms.StringField(
_l("Name"),
validators=[wtforms.validators.InputRequired()],
Expand Down Expand Up @@ -350,7 +349,7 @@ async def create_circle() -> typing.Union[str, quart.Response]:
)


class EditCircleForm(flask_wtf.FlaskForm): # type:ignore
class EditCircleForm(BaseForm):
name = wtforms.StringField(
_l("Name"),
validators=[wtforms.validators.InputRequired()],
Expand Down
12 changes: 12 additions & 0 deletions snikket_web/infra.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
)

import flask_babel
import flask_wtf
from flask_babel import _

from . import prosodyclient
Expand Down Expand Up @@ -55,3 +56,14 @@ def generate_error_id() -> str:
return base64.b32encode(secrets.token_bytes(8)).decode(
"ascii"
).rstrip("=")


class BaseForm(flask_wtf.FlaskForm): # type:ignore
def __init__(self, *args: typing.Any, **kwargs: typing.Any):
meta = kwargs["meta"] = dict(kwargs.get("meta", {}))
if "locales" not in meta:
locale = flask_babel.get_locale()
if locale:
meta["locales"] = [str(locale)]

super().__init__(*args, **kwargs)
7 changes: 3 additions & 4 deletions snikket_web/invite.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

import wtforms

import flask_wtf
from flask_babel import lazy_gettext as _l

from .infra import client, selected_locale
from .infra import client, selected_locale, BaseForm


bp = Blueprint("invite", __name__)
Expand Down Expand Up @@ -101,7 +100,7 @@ async def view(id_: str) -> typing.Union[quart.Response,
)


class RegisterForm(flask_wtf.FlaskForm): # type:ignore
class RegisterForm(BaseForm):
localpart = wtforms.StringField(
_l("Username"),
)
Expand Down Expand Up @@ -172,7 +171,7 @@ async def register(id_: str) -> typing.Union[str, quart.Response]:
)


class ResetForm(flask_wtf.FlaskForm): # type:ignore
class ResetForm(BaseForm):
password = wtforms.PasswordField(
_l("Password"),
)
Expand Down
5 changes: 2 additions & 3 deletions snikket_web/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@
import wtforms

import flask_wtf

from flask_babel import lazy_gettext as _l, _

from . import xmpputil, _version
from .infra import client
from .infra import client, BaseForm


bp = quart.Blueprint("main", __name__)


class LoginForm(flask_wtf.FlaskForm): # type:ignore
class LoginForm(BaseForm):
address = wtforms.TextField(
_l("Address"),
validators=[wtforms.validators.InputRequired()],
Expand Down
Loading

0 comments on commit 4f54ab4

Please sign in to comment.