From 899487245dfecd2f2788a06b695effa31513540d Mon Sep 17 00:00:00 2001 From: Samuel ORTION Date: Thu, 26 May 2022 13:58:46 +0200 Subject: [PATCH] Update model & translations' --- app.py | 44 ++++++++++++++++-- babel.cfg | 4 ++ language/message.pot | 35 ++++++++++++++ .../translations/fr/LC_MESSAGES/messages.mo | Bin 0 -> 596 bytes .../translations/fr/LC_MESSAGES/messages.po | 36 ++++++++++++++ make_migration.sh | 2 + .../d9b0e75aca5e_initial_migration.py | 34 -------------- model.py | 20 +++----- requirements.txt | 3 ++ templates/auth/login.html | 8 ++-- templates/auth/signup.html | 8 ++-- templates/base.html | 2 +- templates/game/answer.html | 3 ++ templates/game/new.html | 2 +- templates/game/question.html | 2 +- templates/index.html | 2 +- templates/lang.html | 11 +++++ templates/menu.html | 9 +++- 18 files changed, 160 insertions(+), 65 deletions(-) create mode 100644 babel.cfg create mode 100644 language/message.pot create mode 100644 language/translations/fr/LC_MESSAGES/messages.mo create mode 100644 language/translations/fr/LC_MESSAGES/messages.po create mode 100644 make_migration.sh delete mode 100644 migrations/versions/d9b0e75aca5e_initial_migration.py create mode 100644 templates/lang.html diff --git a/app.py b/app.py index 00c94e2..5d55524 100644 --- a/app.py +++ b/app.py @@ -4,8 +4,11 @@ from flask import session from flask import request from flask import redirect from flask import url_for +from flask import g from flask_migrate import Migrate +from flask_babel import Babel, gettext + from werkzeug.security import generate_password_hash, check_password_hash from config import secret_key, database_uri @@ -18,8 +21,41 @@ app.secret_key = secret_key app.config['SQLALCHEMY_DATABASE_URI'] = database_uri app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False +app.config['BABEL_DEFAULT_LOCALE'] = 'en' + +babel = Babel(app) + db.init_app(app) +@babel.localeselector +def get_locale(): + # if a user is logged in, use the locale from the user settings + user = getattr(g, 'user', None) + if user is not None: + return user.locale + elif session['lang']: + return session['lang'] + # otherwise try to guess the language from the user accept + # header the browser transmits. We support fr/en in this + # example. The best match wins. + return request.accept_languages.best_match(['fr', 'en']) + +@babel.timezoneselector +def get_timezone(): + user = getattr(g, 'user', None) + if user is not None: + return user.timezone + +@app.route('/lang') +def get_lang(): + return render_template('lang.html') + +@app.route('/lang', methods=["POST"]) +def set_lang(): + lang = request.form["lang"] + session['lang'] = lang + return redirect('/') + @app.route("/") def home(): if 'username' in session: @@ -33,10 +69,10 @@ def signup(): username = request.form['username'] email = request.form['email'] password = request.form['password'] - registered_user = User.query.filter_by(username=username).first() + registered_user = User.query.filter_by(user_name=username).first() if registered_user is None: password_hash = generate_password_hash(password) - registered_user = User(username=username, email=email, password=password_hash) + registered_user = User(user_name=username, user_email=email, user_password=password_hash) db.session.add(registered_user) db.session.commit() else: @@ -50,11 +86,11 @@ def login(): if request.method == "POST": username = request.form['username'] password = request.form['password'] - user = User.query.filter_by(username=username).first() + user = User.query.filter_by(user_name=username).first() if user is None: return render_template("auth/login.html", message="No user with this username already registered") else: - password_hash = user.password + password_hash = user.user_password if check_password_hash(password_hash, password): session["username"] = username return redirect(url_for("home")) diff --git a/babel.cfg b/babel.cfg new file mode 100644 index 0000000..751ac40 --- /dev/null +++ b/babel.cfg @@ -0,0 +1,4 @@ +[python: **.py] +[jinja2: templates/**.html] +encoding = utf-8 +extensions=jinja2.ext.autoescape,jinja2.ext.with_ \ No newline at end of file diff --git a/language/message.pot b/language/message.pot new file mode 100644 index 0000000..1133c9f --- /dev/null +++ b/language/message.pot @@ -0,0 +1,35 @@ +# Translations template for PROJECT. +# Copyright (C) 2022 ORGANIZATION +# This file is distributed under the same license as the PROJECT project. +# FIRST AUTHOR , 2022. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: PROJECT VERSION\n" +"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" +"POT-Creation-Date: 2022-05-26 12:11+0200\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=utf-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Generated-By: Babel 2.10.1\n" + +#: templates/base.html:15 +msgid "Welcome" +msgstr "" + +#: templates/index.html:3 +msgid "Welcome to BirdQuizz !" +msgstr "" + +#: templates/menu.html:3 +msgid "Home" +msgstr "" + +#: templates/menu.html:4 +msgid "Game" +msgstr "" + diff --git a/language/translations/fr/LC_MESSAGES/messages.mo b/language/translations/fr/LC_MESSAGES/messages.mo new file mode 100644 index 0000000000000000000000000000000000000000..a2432433bdedbef0b8e783cdd9f6fcf27407563a GIT binary patch literal 596 zcmZuuO>Yx15H(PQ*dqtTfx~b>qCyVqMWD)QqUK{;QtdWjvy`*DnJh+*9eKTl_QF5l z20xGA!h|5wN?3Y+^Xw;KgyJ?TUAr!(*F~@M77$Sz+sjh8QpQd?ng7zdS&>r^Q%UJUH_3yFX_Uz?7gti z2eIFc@=2cVMj|?KG;%herKYad6czAmc(pc4+b&d}ly5Nf+OXsYJKv`HC3>FP$}P05 xB^+Gn5S^e!PZOe9UP&0L3p9{)MBEV(JH?g_s;Wh8*ifUrMBC!70@dF7z29LypI-m~ literal 0 HcmV?d00001 diff --git a/language/translations/fr/LC_MESSAGES/messages.po b/language/translations/fr/LC_MESSAGES/messages.po new file mode 100644 index 0000000..1e6e152 --- /dev/null +++ b/language/translations/fr/LC_MESSAGES/messages.po @@ -0,0 +1,36 @@ +# French translations for PROJECT. +# Copyright (C) 2022 ORGANIZATION +# This file is distributed under the same license as the PROJECT project. +# FIRST AUTHOR , 2022. +# +msgid "" +msgstr "" +"Project-Id-Version: PROJECT VERSION\n" +"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n" +"POT-Creation-Date: 2022-05-26 12:11+0200\n" +"PO-Revision-Date: 2022-05-26 13:10+0200\n" +"Last-Translator: FULL NAME \n" +"Language: fr\n" +"Language-Team: fr \n" +"Plural-Forms: nplurals=2; plural=(n > 1);\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=utf-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Generated-By: Babel 2.10.1\n" + +#: templates/base.html:15 +msgid "Welcome" +msgstr "Bienvenue" + +#: templates/index.html:3 +msgid "Welcome to BirdQuizz !" +msgstr "Bienvenue au BirdQuizz !" + +#: templates/menu.html:3 +msgid "Home" +msgstr "Accueil" + +#: templates/menu.html:4 +msgid "Game" +msgstr "Jeu" + diff --git a/make_migration.sh b/make_migration.sh new file mode 100644 index 0000000..3e1f815 --- /dev/null +++ b/make_migration.sh @@ -0,0 +1,2 @@ +flask db migrate -m "Initial migration." +flask db upgrade \ No newline at end of file diff --git a/migrations/versions/d9b0e75aca5e_initial_migration.py b/migrations/versions/d9b0e75aca5e_initial_migration.py deleted file mode 100644 index f84c16e..0000000 --- a/migrations/versions/d9b0e75aca5e_initial_migration.py +++ /dev/null @@ -1,34 +0,0 @@ -"""Initial migration. - -Revision ID: d9b0e75aca5e -Revises: -Create Date: 2022-05-26 10:57:06.959999 - -""" -from alembic import op -import sqlalchemy as sa - - -# revision identifiers, used by Alembic. -revision = 'd9b0e75aca5e' -down_revision = None -branch_labels = None -depends_on = None - - -def upgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.create_table('userscore', - sa.Column('user_id', sa.Integer(), nullable=False), - sa.Column('score_value', sa.Integer(), nullable=True), - sa.Column('user_level', sa.Integer(), nullable=True), - sa.ForeignKeyConstraint(['user_id'], ['user.id'], ), - sa.PrimaryKeyConstraint('user_id') - ) - # ### end Alembic commands ### - - -def downgrade(): - # ### commands auto generated by Alembic - please adjust! ### - op.drop_table('userscore') - # ### end Alembic commands ### diff --git a/model.py b/model.py index 65b51ea..8583156 100644 --- a/model.py +++ b/model.py @@ -6,17 +6,9 @@ db = SQLAlchemy() class User(db.Model): __tablename__ = 'user' - id = db.Column(db.Integer, primary_key=True) - username = db.Column(db.String(64), unique=True) - email = db.Column(db.String(64), unique=True, index=True) - password = db.Column(db.String(128)) - -class Score(db.Model): - __tablename__ = 'userscore' - - user_id = db.Column(db.Integer, db.ForeignKey(User.id), primary_key=True) - score_value = db.Column(db.Integer) - user_level = db.Column(db.Integer, default = 1) - user = relationship("User", foreign_keys="User.id") - - \ No newline at end of file + user_id = db.Column(db.Integer, primary_key=True) + user_name = db.Column(db.String(64), unique=True) + user_email = db.Column(db.String(64), unique=True, index=True) + user_password = db.Column(db.String(128)) + user_level = db.Column(db.Integer, default=1) + user_score = db.Column(db.Integer, default=0) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 18f21ff..2a26a35 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,8 @@ alembic==1.7.7 +Babel==2.10.1 click==8.1.3 Flask==2.1.2 +Flask-Babel==2.0.0 Flask-Migrate==3.1.0 Flask-SQLAlchemy==2.5.1 greenlet==1.1.2 @@ -11,5 +13,6 @@ MarkupSafe==2.1.1 mysql-connector-python==8.0.29 protobuf==3.20.1 PyMySQL==1.0.2 +pytz==2022.1 SQLAlchemy==1.4.36 Werkzeug==2.1.2 diff --git a/templates/auth/login.html b/templates/auth/login.html index e885d5d..675e724 100644 --- a/templates/auth/login.html +++ b/templates/auth/login.html @@ -9,11 +9,13 @@ {% endif %}
- + - + - +
+{{ _('Sign up') }} + {% endblock %} diff --git a/templates/auth/signup.html b/templates/auth/signup.html index 4f7a04f..39280d0 100644 --- a/templates/auth/signup.html +++ b/templates/auth/signup.html @@ -9,13 +9,13 @@ {% endif %}
- + - + - + - +
{% endblock %} diff --git a/templates/base.html b/templates/base.html index c1fcad8..1e211e7 100644 --- a/templates/base.html +++ b/templates/base.html @@ -12,7 +12,7 @@ {% include 'menu.html' %}

BirdQuizz

{% if username is defined %} -

Welcome {{ username }}

+

{{ _('Welcome') }} {{ username }}

{% endif %}
diff --git a/templates/game/answer.html b/templates/game/answer.html index 2fb15c9..414bb3b 100644 --- a/templates/game/answer.html +++ b/templates/game/answer.html @@ -1,4 +1,7 @@ {% extends "base.html" %} {% block content %}

{{ message }}

+

+ {{ _('New Question') }} +

{% endblock %} \ No newline at end of file diff --git a/templates/game/new.html b/templates/game/new.html index 1adc497..0631b5a 100644 --- a/templates/game/new.html +++ b/templates/game/new.html @@ -1,5 +1,5 @@ {% extends "base.html" %} - New Game + {{ _('New Game') }} \ No newline at end of file diff --git a/templates/game/question.html b/templates/game/question.html index dfe8a83..ee0267a 100644 --- a/templates/game/question.html +++ b/templates/game/question.html @@ -7,7 +7,7 @@ {% endfor %} - + {% endblock %} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index a4de76c..a9cf47f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,4 +1,4 @@ {% extends "base.html" %} {% block content %} -Welcome to BirdQuizz ! +{{ _('Welcome to BirdQuizz !') }} {% endblock %} \ No newline at end of file diff --git a/templates/lang.html b/templates/lang.html new file mode 100644 index 0000000..ffe741f --- /dev/null +++ b/templates/lang.html @@ -0,0 +1,11 @@ +{% extends "base.html" %} +{% block content %} +

{{ _('Select your language !') }}

+
+ + +
+{% endblock %} \ No newline at end of file diff --git a/templates/menu.html b/templates/menu.html index 7c61e92..3877ce3 100644 --- a/templates/menu.html +++ b/templates/menu.html @@ -1,6 +1,11 @@ \ No newline at end of file