Update model & translations'
This commit is contained in:
parent
3a8faf6d5f
commit
899487245d
44
app.py
44
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"))
|
||||
|
4
babel.cfg
Normal file
4
babel.cfg
Normal file
@ -0,0 +1,4 @@
|
||||
[python: **.py]
|
||||
[jinja2: templates/**.html]
|
||||
encoding = utf-8
|
||||
extensions=jinja2.ext.autoescape,jinja2.ext.with_
|
35
language/message.pot
Normal file
35
language/message.pot
Normal file
@ -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 <EMAIL@ADDRESS>, 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 <EMAIL@ADDRESS>\n"
|
||||
"Language-Team: LANGUAGE <LL@li.org>\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 ""
|
||||
|
BIN
language/translations/fr/LC_MESSAGES/messages.mo
Normal file
BIN
language/translations/fr/LC_MESSAGES/messages.mo
Normal file
Binary file not shown.
36
language/translations/fr/LC_MESSAGES/messages.po
Normal file
36
language/translations/fr/LC_MESSAGES/messages.po
Normal file
@ -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 <EMAIL@ADDRESS>, 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 <EMAIL@ADDRESS>\n"
|
||||
"Language: fr\n"
|
||||
"Language-Team: fr <LL@li.org>\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"
|
||||
|
2
make_migration.sh
Normal file
2
make_migration.sh
Normal file
@ -0,0 +1,2 @@
|
||||
flask db migrate -m "Initial migration."
|
||||
flask db upgrade
|
@ -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 ###
|
20
model.py
20
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")
|
||||
|
||||
|
||||
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)
|
@ -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
|
||||
|
@ -9,11 +9,13 @@
|
||||
{% endif %}
|
||||
|
||||
<form action="/login" method="POST">
|
||||
<label for="username">Username</label>
|
||||
<label for="username">{{ _('Username') }}</label>
|
||||
<input type="text" name="username" id="username">
|
||||
<label for="password">Password</label>
|
||||
<label for="password">{{ _('Password') }}</label>
|
||||
<input type="password" name="password" id="password">
|
||||
<input type="submit" value="Login">
|
||||
<input type="submit" value="{{ _('Login') }}">
|
||||
</form>
|
||||
|
||||
<a href="/signup">{{ _('Sign up') }}</a>
|
||||
|
||||
{% endblock %}
|
||||
|
@ -9,13 +9,13 @@
|
||||
{% endif %}
|
||||
|
||||
<form action="/signup" method="POST">
|
||||
<label for="username">Username</label>
|
||||
<label for="username">{{ _('Username') }}</label>
|
||||
<input type="text" name="username" id="username">
|
||||
<label for="email">Email</label>
|
||||
<label for="email">{{ _('Email') }}</label>
|
||||
<input type="email" name="email" id="email">
|
||||
<label for="password">Password</label>
|
||||
<label for="password">{{ _('Password') }}</label>
|
||||
<input type="password" name="password" id="password">
|
||||
<input type="submit" value="Sign up">
|
||||
<input type="submit" value="{{ _('Sign up') }}">
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
||||
|
@ -12,7 +12,7 @@
|
||||
{% include 'menu.html' %}
|
||||
<h1>BirdQuizz</h1>
|
||||
{% if username is defined %}
|
||||
<p>Welcome <span class="username">{{ username }}</span></p>
|
||||
<p>{{ _('Welcome') }} <span class="username">{{ username }}</span></p>
|
||||
{% endif %}
|
||||
</header>
|
||||
<main>
|
||||
|
@ -1,4 +1,7 @@
|
||||
{% extends "base.html" %}
|
||||
{% block content %}
|
||||
<p>{{ message }}</p>
|
||||
<p>
|
||||
<a href="/game/new">{{ _('New Question') }}</a>
|
||||
</p>
|
||||
{% endblock %}
|
@ -1,5 +1,5 @@
|
||||
{% extends "base.html" %}
|
||||
|
||||
<a href="/game/new" class="button">
|
||||
New Game
|
||||
{{ _('New Game') }}
|
||||
</a>
|
@ -7,7 +7,7 @@
|
||||
<option value="{{ item }}">{{ item }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
<input type="submit" value="Send Answer">
|
||||
<input type="submit" value="{{ _('Send Answer') }}">
|
||||
</form>
|
||||
|
||||
{% endblock %}
|
@ -1,4 +1,4 @@
|
||||
{% extends "base.html" %}
|
||||
{% block content %}
|
||||
Welcome to BirdQuizz !
|
||||
{{ _('Welcome to BirdQuizz !') }}
|
||||
{% endblock %}
|
11
templates/lang.html
Normal file
11
templates/lang.html
Normal file
@ -0,0 +1,11 @@
|
||||
{% extends "base.html" %}
|
||||
{% block content %}
|
||||
<h2>{{ _('Select your language !') }}</h2>
|
||||
<form action="/lang" method="POST">
|
||||
<select name="lang" id="lang">
|
||||
<option value="fr">Français</option>
|
||||
<option value="en">English</option>
|
||||
</select>
|
||||
<input type="submit" value="{{ _('Translate') }}">
|
||||
</form>
|
||||
{% endblock %}
|
@ -1,6 +1,11 @@
|
||||
<nav>
|
||||
<ul>
|
||||
<li><a href="/">Home</a></li>
|
||||
<li><a href="/game">Game</a></li>
|
||||
<li><a href="/">{{ _('Home') }}</a></li>
|
||||
<li><a href="/game">{{ _('Game') }}</a></li>
|
||||
{% if username is defined %}
|
||||
<li><a href="/logout">{{ _('Logout') }}</a></li>
|
||||
{% else %}
|
||||
<li><a href="/login">{{ _('Login') }}</a></li>
|
||||
{% endif %}
|
||||
</ul>
|
||||
</nav>
|
Loading…
x
Reference in New Issue
Block a user