Skip to content

Commit

Permalink
Fix user creation (#137)
Browse files Browse the repository at this point in the history
* Fix user creation
  • Loading branch information
tfnribeiro authored Apr 25, 2024
1 parent 52080e0 commit 4af966e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 69 deletions.
10 changes: 1 addition & 9 deletions zeeguu/api/endpoints/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
@cross_domain
def add_user(email):
"""
Creates user, then returns a session for that user
"""

password = request.form.get("password")
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
34 changes: 12 additions & 22 deletions zeeguu/core/account_management/user_account_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand Down
74 changes: 36 additions & 38 deletions zeeguu/core/model/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
):
"""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 4af966e

Please sign in to comment.