From 4af966eff4eb2015b7887013f942a67c87cbd02a Mon Sep 17 00:00:00 2001 From: Tiago Ribeiro Date: Thu, 25 Apr 2024 13:32:43 +0200 Subject: [PATCH] Fix user creation (#137) * Fix user creation --- zeeguu/api/endpoints/accounts.py | 10 +-- .../user_account_creation.py | 34 +++------ zeeguu/core/model/user.py | 74 +++++++++---------- 3 files changed, 49 insertions(+), 69 deletions(-) diff --git a/zeeguu/api/endpoints/accounts.py b/zeeguu/api/endpoints/accounts.py index 73820b50..6068d353 100644 --- a/zeeguu/api/endpoints/accounts.py +++ b/zeeguu/api/endpoints/accounts.py @@ -17,9 +17,7 @@ @cross_domain def add_user(email): """ - Creates user, then returns a session for that user - """ password = request.form.get("password") @@ -61,9 +59,7 @@ def add_user(email): @cross_domain def add_basic_user(email): """ - Creates user, then returns a session for that user - """ from ...core.account_management.user_account_creation import create_basic_account @@ -74,11 +70,7 @@ def add_basic_user(email): try: new_user = create_basic_account( - db_session, - username, - password, - invite_code, - email + db_session, username, password, invite_code, email ) new_session = Session.for_user(new_user) db_session.add(new_session) diff --git a/zeeguu/core/account_management/user_account_creation.py b/zeeguu/core/account_management/user_account_creation.py index 4b3ad311..2e37eb1e 100644 --- a/zeeguu/core/account_management/user_account_creation.py +++ b/zeeguu/core/account_management/user_account_creation.py @@ -10,7 +10,7 @@ def valid_invite_code(invite_code): return True if zeeguu.core.app.config.get( - "INVITATION_CODES" + "INVITATION_CODES" ) and invite_code in zeeguu.core.app.config.get("INVITATION_CODES"): return True @@ -22,14 +22,14 @@ def valid_invite_code(invite_code): # TODO: delete after the new onboarding of Iga is done def create_account( - db_session, - username, - password, - invite_code, - email, - learned_language_code, - native_language_code, - learned_cefr_level, + db_session, + username, + password, + invite_code, + email, + learned_language_code, + native_language_code, + learned_cefr_level, ): cohort_name = "" if password is None or len(password) < 4: @@ -63,7 +63,7 @@ def create_account( ) db_session.add(new_user) - + new_user.create_default_user_preference() learned_language = UserLanguage.find_or_create( db_session, new_user, learned_language ) @@ -94,13 +94,7 @@ def create_account( raise Exception("Could not create the account") -def create_basic_account( - db_session, - username, - password, - invite_code, - email -): +def create_basic_account(db_session, username, password, invite_code, email): cohort_name = "" if password is None or len(password) < 4: raise Exception("Password should be at least 4 characters long") @@ -119,11 +113,7 @@ def create_basic_account( try: new_user = User( - email, - username, - password, - invitation_code=invite_code, - cohort=cohort + email, username, password, invitation_code=invite_code, cohort=cohort ) db_session.add(new_user) diff --git a/zeeguu/core/model/user.py b/zeeguu/core/model/user.py index 45f95496..ddb38a49 100644 --- a/zeeguu/core/model/user.py +++ b/zeeguu/core/model/user.py @@ -55,15 +55,15 @@ class User(db.Model): is_dev = Column(Boolean) def __init__( - self, - email, - name, - password, - learned_language=None, - native_language=None, - invitation_code=None, - cohort=None, - is_dev=0, + self, + email, + name, + password, + learned_language=None, + native_language=None, + invitation_code=None, + cohort=None, + is_dev=0, ): self.email = email self.name = name @@ -74,11 +74,9 @@ def __init__( self.cohort = cohort self.is_dev = is_dev - self.create_default_user_preference() - @classmethod def create_anonymous( - cls, uuid, password, learned_language_code=None, native_language_code=None + cls, uuid, password, learned_language_code=None, native_language_code=None ): """ @@ -153,8 +151,8 @@ def preferred_difficulty_estimator(self): # Must have this import here to avoid circular dependency preference = ( - UserPreference.get_difficulty_estimator(self) - or "FleschKincaidDifficultyEstimator" + UserPreference.get_difficulty_estimator(self) + or "FleschKincaidDifficultyEstimator" ) log(f"Difficulty estimator for user {self.id}: {preference}") return preference @@ -314,10 +312,10 @@ def update_password(self, password: str): self.password_salt = salt_bytes.hex() def all_reading_sessions( - self, - after_date=datetime.datetime(1970, 1, 1), - before_date=datetime.date.today() + datetime.timedelta(days=1), - language_id=None, + self, + after_date=datetime.datetime(1970, 1, 1), + before_date=datetime.date.today() + datetime.timedelta(days=1), + language_id=None, ): from zeeguu.core.model.user_reading_session import UserReadingSession from zeeguu.core.model.article import Article @@ -340,10 +338,10 @@ def all_reading_sessions( return all_sessions def all_bookmarks( - self, - after_date=datetime.datetime(1970, 1, 1), - before_date=datetime.date.today() + datetime.timedelta(days=1), - language_id=None, + self, + after_date=datetime.datetime(1970, 1, 1), + before_date=datetime.date.today() + datetime.timedelta(days=1), + language_id=None, ): from zeeguu.core.model import Bookmark, UserWord @@ -449,7 +447,7 @@ def _to_serializable(self, tuple_list, key_name, *args): return result def reading_sessions_by_day( - self, after_date=datetime.datetime(2010, 1, 1), max=42, language_id=None + self, after_date=datetime.datetime(2010, 1, 1), max=42, language_id=None ): """ :param after_date: The date from which the reading sessions will be queried @@ -468,8 +466,8 @@ def reading_sessions_by_day( if len(sorted_date_reading_sessions_tuples) > max: sorted_date_reading_sessions_tuples = sorted_date_reading_sessions_tuples[ - :max - ] + :max + ] result = self._to_serializable( sorted_date_reading_sessions_tuples, key_name="reading_sessions" @@ -499,12 +497,12 @@ def extract_day_from_date(bookmark): return bookmarks_by_date, sorted_dates def bookmarks_by_day( - self, - with_context, - after_date=datetime.datetime(2010, 1, 1), - max=42, - with_title=False, - language_id=None, + self, + with_context, + after_date=datetime.datetime(2010, 1, 1), + max=42, + with_title=False, + language_id=None, ): bookmarks = self.all_bookmarks(after_date, language_id=language_id) @@ -522,12 +520,12 @@ def bookmarks_by_day( return result def bookmarks_for_article( - self, - article_id, - with_context, - with_title=False, - good_for_study=False, - json=True, + self, + article_id, + with_context, + with_title=False, + good_for_study=False, + json=True, ): from zeeguu.core.model import Bookmark, Text @@ -712,7 +710,7 @@ def authorize(cls, email, password): try: user = cls.find(email) if user.password == password_hash( - password, bytes.fromhex(user.password_salt) + password, bytes.fromhex(user.password_salt) ): return user except sqlalchemy.orm.exc.NoResultFound: