Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated settings to avoid concurrency updates #165

Merged
merged 4 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 7 additions & 19 deletions zeeguu/api/endpoints/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,42 +112,30 @@ def get_user_details():
def user_settings():
"""
set the native language of the user in session
:param language_code:
:return: OK for success
"""

data = flask.request.form
print(flask.request)
user = User.find_by_id(flask.g.user_id)

submitted_name = data.get("name", None)
if submitted_name:
user.name = submitted_name

submitted_native_language_code = data.get("native_language_code", None)
if not submitted_native_language_code:
submitted_native_language_code = data.get("native_language", None)

submitted_native_language_code = data.get("native_language", None)
if submitted_native_language_code:
user.set_native_language(submitted_native_language_code)

# deprecating the larned_language_code
submitted_learned_language_code = data.get("learned_language_code", None)
if not submitted_learned_language_code:
submitted_learned_language_code = data.get("learned_language", None)
cefr_level = data.get("cefr_level", None)
if cefr_level is None:
return "ERROR"

submitted_learned_language_code = data.get("learned_language", None)
if submitted_learned_language_code:
user.set_learned_language(
submitted_learned_language_code, zeeguu.core.model.db.session
submitted_learned_language_code, cefr_level, zeeguu.core.model.db.session
)

language_level = data.get("language_level_data", None)
if language_level:
submitted_learned_language_data = json.loads(language_level)
for language_level in submitted_learned_language_data:
user.set_learned_language_level(
language_level[0], language_level[1], zeeguu.core.model.db.session
)

submitted_email = data.get("email", None)
if submitted_email:
user.email = submitted_email
Expand Down
13 changes: 10 additions & 3 deletions zeeguu/core/model/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ def text_difficulty(self, text, language):
def set_native_language(self, code):
self.native_language = Language.find(code)

def set_learned_language(self, language_code, session=None):
def set_learned_language(
self, language_code: str, cefr_level: int = None, session=None
):
self.learned_language = Language.find(language_code)

from zeeguu.core.model import UserLanguage
Expand All @@ -191,16 +193,21 @@ def set_learned_language(self, language_code, session=None):
language = UserLanguage.find_or_create(session, self, self.learned_language)
language.reading_news = True
language.doing_exercises = True
if cefr_level:
language.cefr_level = cefr_level

if session:
session.add(language)


def set_learned_language_level(self, language_code: str, level: str, session=None):
def set_learned_language_level(
self, language_code: str, cefr_level: str, session=None
):
learned_language = Language.find_or_create(language_code)
from zeeguu.core.model import UserLanguage

language = UserLanguage.find_or_create(session, self, learned_language)
language.cefr_level = int(level)
language.cefr_level = int(cefr_level)
if session:
session.add(language)

Expand Down
17 changes: 14 additions & 3 deletions zeeguu/core/test/test_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,21 @@ def test_get_all_languages(self):
assert LanguageRule.get_or_create_language(lan)

def test_user_set_language(self):
language_should_be = LanguageRule().random
from zeeguu.core.model import UserLanguage

self.user.set_learned_language(language_should_be.code, db_session)
assert self.user.learned_language.id == language_should_be.id
language_should_be = LanguageRule().random
language_level_should_be = 2
self.user.set_learned_language(
language_should_be.code, language_level_should_be, db_session
)
db_session.commit()
user_language = UserLanguage.find_or_create(
db_session, self.user, language_should_be
)
assert (
self.user.learned_language.id == language_should_be.id
and user_language.cefr_level == language_level_should_be
)

def test_native_language(self):
language_should_be = LanguageRule().random
Expand Down
Loading