Repository: hedyorg/hedy

Issue:
[UI idea] When logged in, set language automatically to user preferred language
**Idea incl level**
Currently the UI is set to a user selected language. When logging in the language will stay the same. However, when we built upon #1477 the language selection will disappear from the menubar when logged in. In this case we would like the user to selected a preferred language in the profile settings to use every time the user is logged in. This should be aligned with #1467 to make sure users are able to change their preferred language on the profile page.


Discussion:
Lovely idea! 

I guess we can ask preferred language on signup for new accounts, and ask existing users to set it once in their profile?

Resolving patch:
diff --git a/app.py b/app.py
--- a/app.py
+++ b/app.py
@@ -288,8 +288,16 @@ def setup_language():
 @app.context_processor
 def enrich_context_with_user_info():
     user = current_user()
-    return dict(username=user.get('username', ''), is_teacher=is_teacher(user), is_admin=is_admin(user))
-
+    data = {'username': user.get('username', ''), 'is_teacher': is_teacher(user), 'is_admin': is_admin(user)}
+    if len(data['username']) > 0: #If so, there is a user -> Retrieve all relevant info
+        user_data = DATABASE.user_by_username(user.get('username'))
+        if 'language' in user_data:
+            if user_data['language'] in ALL_LANGUAGES.keys():
+                g.lang = session['lang'] = user_data['language']
+        data['user_data'] = user_data
+        if 'classes' in user_data:
+            data['user_classes'] = DATABASE.get_student_classes(user.get('username'))
+    return data
 
 @app.context_processor
 def enricht_context_with_translations():
diff --git a/website/auth.py b/website/auth.py
--- a/website/auth.py
+++ b/website/auth.py
@@ -193,7 +193,9 @@ def login():
 
     @app.route('/auth/signup', methods=['POST'])
     def signup():
+        print("Er wordt een account aangemaakt!")
         body = request.json
+        print(body)
         # Validations, mandatory fields
         if not isinstance(body, dict):
             return 'body must be an object', 400
@@ -220,6 +222,9 @@ def signup():
         if 'birth_year' in body:
             if not isinstance(body.get('birth_year'), int) or body['birth_year'] <= 1900 or body['birth_year'] > datetime.datetime.now().year:
                 return 'birth_year must be a year between 1900 and ' + datetime.datetime.now().year, 400
+        if 'language' in body:
+            if not isinstance(body.get('language'), str):
+                return 'language must be a valid language', 400
         if 'gender' in body:
             if body['gender'] != 'm' and body['gender'] != 'f' and body['gender'] != 'o':
                 return 'gender must be m/f/o', 400
@@ -266,7 +271,7 @@ def signup():
             'last_login': timems()
         }
 
-        for field in['country', 'birth_year', 'gender', 'prog_experience', 'experience_languages']:
+        for field in['country', 'birth_year', 'gender', 'language', 'prog_experience', 'experience_languages']:
            if field in body:
                if field == 'experience_languages' and len(body[field]) == 0:
                    continue
@@ -380,6 +385,9 @@ def update_profile(user):
         if 'birth_year' in body:
             if not isinstance(body.get('birth_year'), int) or body['birth_year'] <= 1900 or body['birth_year'] > datetime.datetime.now().year:
                 return 'birth_year must be a year between 1900 and ' + str(datetime.datetime.now().year), 400
+        if 'language' in body:
+            if not isinstance(body.get('language'), str):
+                return 'language must be a valid language', 400
         if 'gender' in body:
             if body['gender'] != 'm' and body['gender'] != 'f' and body['gender'] != 'o':
                 return 'body.gender must be m/f/o', 400
@@ -421,7 +429,7 @@ def update_profile(user):
         username = user['username']
 
         updates = {}
-        for field in['country', 'birth_year', 'gender', 'prog_experience', 'experience_languages']:
+        for field in['country', 'birth_year', 'gender', 'language', 'prog_experience', 'experience_languages']:
            if field in body:
                if field == 'experience_languages' and len(body[field]) == 0:
                    updates[field] = None
diff --git a/website/teacher.py b/website/teacher.py
--- a/website/teacher.py
+++ b/website/teacher.py
@@ -155,7 +155,7 @@ def join_class (user, class_id, link):
     def leave_class (user, class_id, student_id):
 
         Class = DATABASE.get_class (class_id)
-        if not Class or Class ['teacher'] != user ['username']:
+        if not Class or Class ['teacher'] != user ['username'] or student_id != user ['username']:
             return 'No such class', 404
 
         DATABASE.remove_student_from_class (Class ['id'], student_id)


Test patch:
diff --git a/tests_e2e.py b/tests_e2e.py
--- a/tests_e2e.py
+++ b/tests_e2e.py
@@ -253,7 +253,9 @@ def test_invalid_signups(self):
             {'username': username, 'password': 'foo'},
             {'username': username, 'password': 'foobar'},
             {'username': username, 'password': 'foobar', 'email': 'me@something'},
-            {'username': username, 'password': 'foobar', 'email': 'me@something.com', 'prog_experience':[2]},
+            {'username': username, 'password': 'foobar', 'email': 'me@something.com', 'language': 123},
+            {'username': username, 'password': 'foobar', 'email': 'me@something.com', 'language': True},
+            {'username': username, 'password': 'foobar', 'email': 'me@something.com', 'prog_experience': [2]},
             {'username': username, 'password': 'foobar', 'email': 'me@something.com', 'prog_experience': 'foo'},
             {'username': username, 'password': 'foobar', 'email': 'me@something.com', 'experience_languages': 'python'}
         ]
@@ -466,6 +468,8 @@ def test_invalid_profile_modify(self):
             {'country': 'Netherlands'},
             {'gender': 0},
             {'gender': 'a'},
+            {'language': True},
+            {'language': 123},
             {'prog_experience': 1},
             {'prog_experience': 'foo'},
             {'prog_experience': True},