Add locale to user model

This commit is contained in:
rubenwardy 2022-01-22 20:47:43 +00:00
parent d4903f04f1
commit 7b4254da58
4 changed files with 41 additions and 8 deletions

@ -121,16 +121,19 @@ def page_not_found(e):
@babel.localeselector @babel.localeselector
def get_locale(): def get_locale():
if not request:
return None
locales = app.config["LANGUAGES"].keys() locales = app.config["LANGUAGES"].keys()
if request: if current_user.is_authenticated and current_user.locale in locales:
locale = request.cookies.get("locale") return current_user.locale
if locale in locales:
return locale
return request.accept_languages.best_match(locales) locale = request.cookies.get("locale")
if locale in locales:
return locale
return None return request.accept_languages.best_match(locales)
@app.route("/set-locale/", methods=["POST"]) @app.route("/set-locale/", methods=["POST"])
@ -152,4 +155,8 @@ def set_locale():
expire_date = expire_date + datetime.timedelta(days=5*365) expire_date = expire_date + datetime.timedelta(days=5*365)
resp.set_cookie("locale", locale, expires=expire_date) resp.set_cookie("locale", locale, expires=expire_date)
if current_user.is_authenticated:
current_user.locale = locale
models.db.session.commit()
return resp return resp

@ -117,8 +117,8 @@ class ForumTopic(db.Model):
author_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False) author_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable=False)
author = db.relationship("User", back_populates="forum_topics") author = db.relationship("User", back_populates="forum_topics")
wip = db.Column(db.Boolean, server_default="0") wip = db.Column(db.Boolean, default=False, nullable=False)
discarded = db.Column(db.Boolean, server_default="0") discarded = db.Column(db.Boolean, default=False, nullable=False)
type = db.Column(db.Enum(PackageType), nullable=False) type = db.Column(db.Enum(PackageType), nullable=False)
title = db.Column(db.String(200), nullable=False) title = db.Column(db.String(200), nullable=False)

@ -148,6 +148,8 @@ class User(db.Model, UserMixin):
email = db.Column(db.String(255), nullable=True, unique=True) email = db.Column(db.String(255), nullable=True, unique=True)
email_confirmed_at = db.Column(db.DateTime(), nullable=True, server_default=None) email_confirmed_at = db.Column(db.DateTime(), nullable=True, server_default=None)
locale = db.Column(db.String(10), nullable=True, default="")
# User information # User information
profile_pic = db.Column(db.String(255), nullable=True, server_default=None) profile_pic = db.Column(db.String(255), nullable=True, server_default=None)
is_active = db.Column("is_active", db.Boolean, nullable=False, server_default="0") is_active = db.Column("is_active", db.Boolean, nullable=False, server_default="0")

@ -0,0 +1,24 @@
"""empty message
Revision ID: 6e57b2b4dcdf
Revises: 17b303f33f68
Create Date: 2022-01-22 20:35:25.494712
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '6e57b2b4dcdf'
down_revision = '17b303f33f68'
branch_labels = None
depends_on = None
def upgrade():
op.add_column('user', sa.Column('locale', sa.String(length=10), nullable=True))
def downgrade():
op.drop_column('user', 'locale')