Example #1
0
def create_account(
    email: str,
    password: str,
    birthdate: date,
    has_allowed_recommendations: bool = False,
    is_email_validated: bool = False,
    send_activation_mail: bool = True,
) -> User:
    if find_user_by_email(email):
        raise exceptions.UserAlreadyExistsException()

    user = User(
        email=format_email(email),
        dateOfBirth=datetime.combine(birthdate, datetime.min.time()),
        isEmailValidated=is_email_validated,
        departementCode="007",
        publicName=VOID_PUBLIC_NAME,  # Required because model validation requires 3+ chars
        hasSeenTutorials=False,
        firstName=VOID_FIRST_NAME,
        hasAllowedRecommendations=has_allowed_recommendations,
    )

    age = user.calculate_age()
    if not age or age < constants.ACCOUNT_CREATION_MINIMUM_AGE:
        raise exceptions.UnderAgeUserException()

    user.setPassword(password)
    repository.save(user)

    if not is_email_validated and send_activation_mail:
        request_email_confirmation(user)
    return user
Example #2
0
def create_account(
    email: str,
    password: str,
    birthdate: date,
    has_allowed_recommendations: bool = False,
    is_email_validated: bool = False,
    send_activation_mail: bool = True,
) -> User:
    if find_user_by_email(email):
        raise exceptions.UserAlreadyExistsException()

    user = User(
        email=format_email(email),
        dateOfBirth=birthdate,
        isEmailValidated=is_email_validated,
        departementCode="007",
        publicName="   ",  # Required because model validation requires 3+ chars
        hasSeenTutorials=False,
        firstName="",
        hasAllowedRecommendations=has_allowed_recommendations,
    )
    user.setPassword(password)
    repository.save(user)

    if not is_email_validated and send_activation_mail:
        request_email_confirmation(user)
    return user
Example #3
0
def signin(
        body: authentication.SigninRequest) -> authentication.SigninResponse:
    try:
        users_repo.get_user_with_credentials(body.identifier, body.password)
    except users_exceptions.UnvalidatedAccount as exc:
        raise ApiErrors({
            "code": "EMAIL_NOT_VALIDATED",
            "general": ["L'email n'a pas été validé."]
        }) from exc
    except users_exceptions.CredentialsException as exc:
        raise ApiErrors({"general":
                         ["Identifiant ou Mot de passe incorrect"]}) from exc

    user_email = format_email(body.identifier)

    return authentication.SigninResponse(
        access_token=create_access_token(identity=user_email),
        refresh_token=create_refresh_token(identity=user_email),
    )
Example #4
0
def find_user_by_email(email: str) -> User:
    return User.query.filter(
        func.lower(User.email) == format_email(email)).first()