Skip to content

Login

Handles the backend for the login process.

login(first_name, last_name, attributes)

Login entry point.

Parameters:

Name Type Description Default
first_name str

First name entered by the user.

required
last_name str

Last name entered by the user.

required
attributes AppAttributes

The current app attributes.

required

Returns:

Type Description
(str, AppState | None)

A string containing the user hash on success or an error message on errror and the current app state on success or None on error.

Source code in evaluator/backend/login.py
def login(
    first_name: str, last_name: str, attributes: AppAttributes
) -> tuple[str, Optional[AppState]]:
    """Login entry point.

    Parameters
    ----------
    first_name : str
        First name entered by the user.
    last_name : str
        Last name entered by the user.
    attributes : AppAttributes
        The current app attributes.

    Returns
    -------
    (str, AppState | None)
        A string containing the user hash on success or an
        error message on errror and the current app state
        on success or None on error.
    """
    if not first_name and not last_name:
        return "Error: First and last name are required.", None
    elif not first_name:
        return "Error: First name is required.", None
    elif not last_name:
        return "Error: Last name is required.", None

    first_name = first_name.strip().lower()
    last_name = last_name.strip().lower()

    user_hash = _generate_user_hash(first_name, last_name)

    if _check_user_existence(user_hash, attributes):
        attributes["logger"].info(f"Found existing user for {last_name}, {first_name}")
        new_user = False
    else:
        new_user = True

    app_state = create_app_state(
        attributes=attributes, user_hash=user_hash, new_user=new_user
    )
    if new_user:
        app_state = create_new_user(
            app_state=app_state, first_name=first_name, last_name=last_name
        )

    log_state(app_state, "app")

    return user_hash, app_state

_check_user_existence(user_hash, attributes)

Checks if the user already exists or not.

user_hash : str The user's md5 hash. attributes : AppAttributes The current app state.

Returns:

Type Description
bool

True if the user exists, False otherwise.

Source code in evaluator/backend/login.py
def _check_user_existence(user_hash: str, attributes: AppAttributes) -> bool:
    """Checks if the user already exists or not.

    user_hash : str
        The user's md5 hash.
    attributes : AppAttributes
        The current app state.

    Returns
    -------
    bool
        True if the user exists, False otherwise.
    """
    if user_hash in attributes["users_data"]:
        return True
    else:
        return False

_generate_user_hash(first_name, last_name)

Generates the user's MD5 hash.

Parameters:

Name Type Description Default
first_name str

The user's first name.

required
last_name str

The user's last name.

required

Returns:

Type Description
str

The user hash.

Source code in evaluator/backend/login.py
def _generate_user_hash(first_name: str, last_name: str) -> str:
    """Generates the user's MD5 hash.

    Parameters
    ----------
    first_name : str
        The user's first name.
    last_name : str
        The user's last name.

    Returns
    -------
    str
        The user hash.
    """
    name_list = [first_name, last_name]
    sorted(name_list)
    name_str = "_".join(name_list)
    hash_hex = md5(name_str.encode("utf-8")).hexdigest()
    return hash_hex