<commit_before>"""
byceps.services.user.creation_service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2020 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from datetime import datetime
from typing import Optional, Tuple

from flask import current_app

from ...database import db
from ...events.user import UserAccountCreated
from ...typing import UserID

from ..authentication.password import service as password_service
from ..consent import consent_service
from ..consent.transfer.models import Consent
from ..newsletter import command_service as newsletter_command_service
from ..newsletter.transfer.models import Subscription as NewsletterSubscription
from ..site.transfer.models import SiteID

from . import email_address_verification_service
from . import event_service
from .models.detail import UserDetail as DbUserDetail
from .models.user import User as DbUser
from . import service as user_service
from .transfer.models import User


class UserCreationFailed(Exception):
    pass


def create_user(
    screen_name: str,
    email_address: str,
    password: str,
    first_names: Optional[str],
    last_name: Optional[str],
    site_id: SiteID,
    *,
    terms_consent: Optional[Consent] = None,
    privacy_policy_consent: Optional[Consent] = None,
    newsletter_subscription: Optional[NewsletterSubscription] = None,
) -> Tuple[User, UserAccountCreated]:
    """Create a user account and related records."""
    # user with details, password, and roles
    user, event = create_basic_user(
        screen_name,
        email_address,
        password,
        first_names=first_names,
        last_name=last_name,
    )

    # consent to terms of service
    if terms_consent:
        terms_consent = consent_service.build_consent(
            user.id, terms_consent.subject_id, terms_consent.expressed_at
        )
        db.session.add(terms_consent)

    # consent to privacy policy
    if privacy_policy_consent:
        privacy_policy_consent = consent_service.build_consent(
            user.id,
            privacy_policy_consent.subject_id,
            privacy_policy_consent.expressed_at,
        )
        db.session.add(privacy_policy_consent)

    db.session.commit()

    # newsletter subscription (optional)
    if newsletter_subscription:
        newsletter_command_service.subscribe(
            user.id,
            newsletter_subscription.list_id,
            newsletter_subscription.expressed_at,
        )

    request_email_address_confirmation(user, email_address, site_id)

    return user, event


def create_basic_user(
    screen_name: str,
    email_address: Optional[str],
    password: str,
    *,
    first_names: Optional[str] = None,
    last_name: Optional[str] = None,
    creator_id: Optional[UserID] = None,
) -> Tuple[User, UserAccountCreated]:
    # user with details
    user, event = _create_user(
        screen_name,
        email_address,
        first_names=first_names,
        last_name=last_name,
        creator_id=creator_id,
    )

    # password
    password_service.create_password_hash(user.id, password)

    return user, event


def _create_user(
    screen_name: str,
    email_address: Optional[str],
    *,
    first_names: Optional[str] = None,
    last_name: Optional[str] = None,
    creator_id: Optional[UserID] = None,
) -> Tuple[User, UserAccountCreated]:
    created_at = datetime.utcnow()

    user = build_user(created_at, screen_name, email_address)

    user.detail.first_names = first_names
    user.detail.last_name = last_name

    db.session.add(user)

    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.error('User creation failed: %s', e)
        db.session.rollback()
        raise UserCreationFailed()

    # Create event in separate step as user ID is not available earlier.
    event_data = {}
    if creator_id is not None:
        event_data['initiator_id'] = str(creator_id)
    event_service.create_event(
        'user-created', user.id, event_data, occurred_at=created_at
    )

    user_dto = user_service._db_entity_to_user(user)

    event = UserAccountCreated(
        occurred_at=user.created_at, user_id=user.id, initiator_id=creator_id
    )

    return user_dto, event


def build_user(
    created_at: datetime, screen_name: str, email_address: Optional[str]
) -> DbUser:
    normalized_screen_name = _normalize_screen_name(screen_name)

    if email_address is not None:
        normalized_email_address = _normalize_email_address(email_address)
    else:
        normalized_email_address = None

    user = DbUser(created_at, normalized_screen_name, normalized_email_address)<commit_msg>Allow internal creation of a user without a screen name<commit_after>"""
byceps.services.user.creation_service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2020 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from datetime import datetime
from typing import Optional, Tuple

from flask import current_app

from ...database import db
from ...events.user import UserAccountCreated
from ...typing import UserID

from ..authentication.password import service as password_service
from ..consent import consent_service
from ..consent.transfer.models import Consent
from ..newsletter import command_service as newsletter_command_service
from ..newsletter.transfer.models import Subscription as NewsletterSubscription
from ..site.transfer.models import SiteID

from . import email_address_verification_service
from . import event_service
from .models.detail import UserDetail as DbUserDetail
from .models.user import User as DbUser
from . import service as user_service
from .transfer.models import User


class UserCreationFailed(Exception):
    pass


def create_user(
    screen_name: str,
    email_address: str,
    password: str,
    first_names: Optional[str],
    last_name: Optional[str],
    site_id: SiteID,
    *,
    terms_consent: Optional[Consent] = None,
    privacy_policy_consent: Optional[Consent] = None,
    newsletter_subscription: Optional[NewsletterSubscription] = None,
) -> Tuple[User, UserAccountCreated]:
    """Create a user account and related records."""
    # user with details, password, and roles
    user, event = create_basic_user(
        screen_name,
        email_address,
        password,
        first_names=first_names,
        last_name=last_name,
    )

    # consent to terms of service
    if terms_consent:
        terms_consent = consent_service.build_consent(
            user.id, terms_consent.subject_id, terms_consent.expressed_at
        )
        db.session.add(terms_consent)

    # consent to privacy policy
    if privacy_policy_consent:
        privacy_policy_consent = consent_service.build_consent(
            user.id,
            privacy_policy_consent.subject_id,
            privacy_policy_consent.expressed_at,
        )
        db.session.add(privacy_policy_consent)

    db.session.commit()

    # newsletter subscription (optional)
    if newsletter_subscription:
        newsletter_command_service.subscribe(
            user.id,
            newsletter_subscription.list_id,
            newsletter_subscription.expressed_at,
        )

    request_email_address_confirmation(user, email_address, site_id)

    return user, event


def create_basic_user(
    screen_name: str,
    email_address: Optional[str],
    password: str,
    *,
    first_names: Optional[str] = None,
    last_name: Optional[str] = None,
    creator_id: Optional[UserID] = None,
) -> Tuple[User, UserAccountCreated]:
    # user with details
    user, event = _create_user(
        screen_name,
        email_address,
        first_names=first_names,
        last_name=last_name,
        creator_id=creator_id,
    )

    # password
    password_service.create_password_hash(user.id, password)

    return user, event


def _create_user(
    screen_name: Optional[str],
    email_address: Optional[str],
    *,
    first_names: Optional[str] = None,
    last_name: Optional[str] = None,
    creator_id: Optional[UserID] = None,
) -> Tuple[User, UserAccountCreated]:
    created_at = datetime.utcnow()

    user = build_user(created_at, screen_name, email_address)

    user.detail.first_names = first_names
    user.detail.last_name = last_name

    db.session.add(user)

    try:
        db.session.commit()
    except Exception as e:
        current_app.logger.error('User creation failed: %s', e)
        db.session.rollback()
        raise UserCreationFailed()

    # Create event in separate step as user ID is not available earlier.
    event_data = {}
    if creator_id is not None:
        event_data['initiator_id'] = str(creator_id)
    event_service.create_event(
        'user-created', user.id, event_data, occurred_at=created_at
    )

    user_dto = user_service._db_entity_to_user(user)

    event = UserAccountCreated(
        occurred_at=user.created_at, user_id=user.id, initiator_id=creator_id
    )

    return user_dto, event


def build_user(
    created_at: datetime,
    screen_name: Optional[str],
    email_address: Optional[str],
) -> DbUser:
    if screen_name is not None:
        normalized_screen_name = _normalize_screen_name(screen_name)
    else:
        normalized_screen_name = None

    if email_address is not None:
        normalized_email_address = _normalize_email_address(email_address)
    else:
        normalized_email_address = None

    user = DbUser(created_at, normalized_screen_name, normalized_email_address)
{'fraction_non_alphanumeric': 0.08604871875988611, 'fraction_numerical': 0.00168722977960561, 'mean_word_length': 3.3968474733426053, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2752328', 'n_tokens_mistral': 2837, 'n_tokens_neox': 2776, 'n_words': 660}