[chronojump-server] Added column lang and user_id to coach to map a coach with an user and store lan selection Now the s



commit 74907f7f16fa651eaf4cd088b01f46ca7fba4000
Author: Marcos Venteo <mventeo gmail com>
Date:   Mon Mar 19 23:07:46 2018 +0100

    Added column lang and user_id to coach to map a coach with an user and store lan selection
    Now the server gets the lang selected from the table coach to show the expected language

 chronojumpserver/__init__.py          |   15 ++++++++++-----
 chronojumpserver/forms.py             |    3 ++-
 chronojumpserver/models.py            |    2 ++
 chronojumpserver/templates/login.html |    5 +++--
 chronojumpserver/views.py             |    7 ++++---
 5 files changed, 21 insertions(+), 11 deletions(-)
---
diff --git a/chronojumpserver/__init__.py b/chronojumpserver/__init__.py
index af5b95b..c5ec357 100755
--- a/chronojumpserver/__init__.py
+++ b/chronojumpserver/__init__.py
@@ -5,8 +5,8 @@ Chronojump Server Main application...
 """
 import click
 from flask import Flask, send_from_directory
-from flask_login import LoginManager
-from flask_babel import Babel, refresh, gettext
+from flask_login import LoginManager, current_user
+from flask_babel import Babel, refresh, gettext, request
 
 import ConfigParser
 import os
@@ -98,7 +98,7 @@ def shutdown_session(exception=None):
 from chronojumpserver.database import init_db
 init_db()
 
-from chronojumpserver.models import User
+from chronojumpserver.models import User, Coach
 # To retrieve the user from session
 @login_manager.user_loader
 def load_user(user_id):
@@ -106,8 +106,13 @@ def load_user(user_id):
 
 @babel.localeselector
 def get_locale():
-    #return request.accept_languages.best_match(['en','ca','es'])
-    return 'ca'
+    if current_user.is_authenticated:
+        # Get the coach lang
+        coach = Coach.query.filter(Coach.user_id == current_user.id).first()
+        return coach.lang
+    else:
+        #return best accepted by browser
+        return request.accept_languages.best_match(['ca','es','en', 'fr'])
 
 from jinja2 import evalcontextfilter, Markup
 
diff --git a/chronojumpserver/forms.py b/chronojumpserver/forms.py
index 0b7a3ab..954674c 100755
--- a/chronojumpserver/forms.py
+++ b/chronojumpserver/forms.py
@@ -16,6 +16,7 @@ class PersonForm(FlaskForm):
 
 
 class LoginForm(FlaskForm):
-    username = StringField(lazy_gettext('User'), validators=[DataRequired(lazy_gettext('Username is 
required'))])
+    organization = StringField(lazy_gettext('Organization'), 
validators=[DataRequired(lazy_gettext('Organization is required'))])
+    coach = StringField(lazy_gettext('Coach'), validators=[DataRequired(lazy_gettext('Coach is required'))])
     password = PasswordField(lazy_gettext('Password'), validators=[DataRequired(lazy_gettext('Password is 
required'))])
     pass
diff --git a/chronojumpserver/models.py b/chronojumpserver/models.py
index c328011..5fc5784 100755
--- a/chronojumpserver/models.py
+++ b/chronojumpserver/models.py
@@ -52,8 +52,10 @@ class Coach(HelperMixin, Base):
     __tablename__ = 'coach'
 
     org_id = Column(Integer, nullable=False)
+    user_id = Column(Integer, nullable=False)
     name = Column(String(50), unique=True)
     details = Column(String(100), nullable=False)
+    lang = Column(String(5), default='ca')
 
 
 class GroupCoach(HelperMixin, Base):
diff --git a/chronojumpserver/templates/login.html b/chronojumpserver/templates/login.html
index 3ed2c08..dbd2cf8 100755
--- a/chronojumpserver/templates/login.html
+++ b/chronojumpserver/templates/login.html
@@ -20,8 +20,9 @@
                     <form method="post" enctype="multipart/form-data">
                       {{ form.csrf_token }}
 
-                      <p class="text-center">{{_('Enter your user and password')}}</p>
-                      {{ render_field(form.username)}}
+                      <p class="text-center">{{_('Enter the organization, coach identificator and 
password')}}</p>
+                      {{ render_field(form.organization)}}
+                      {{ render_field(form.coach)}}
                       {{ render_field(form.password)}}
                       <button class="btn btn-primary btn-block" type="submit">{{_('Login')}}</button>
                     </form>
diff --git a/chronojumpserver/views.py b/chronojumpserver/views.py
index 1440265..7916702 100755
--- a/chronojumpserver/views.py
+++ b/chronojumpserver/views.py
@@ -6,7 +6,7 @@ from urlparse import urlparse, urljoin
 from flask_wtf.file import FileField
 from chronojumpserver.models import Person, Station, RFIDHistory, User
 from chronojumpserver.forms import PersonForm, LoginForm
-from flask_login import login_required, login_user, logout_user
+from flask_login import login_required, login_user, logout_user, current_user
 from chronojumpserver.database import db_session
 import os
 from time import time
@@ -215,11 +215,12 @@ def login():
     # handle this for us, and we use a custom LoginForm to validate.
     form = LoginForm()
     if request.method == "GET":
-        form.username.data = ""
+        form.organization.data = ""
+        form.coach.data = ""
         form.password.data = ""
     if form.validate_on_submit():
         import md5
-        username = form.username.data
+        username = form.coach.data
         password = md5.md5(form.password.data).hexdigest()
         print password
         user = User.query.filter(User.username == username).first()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]