<commit_before>"""
byceps.services.terms.version_service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from typing import Optional, Sequence

from ...database import db
from ...typing import BrandID

from ..consent.transfer.models import SubjectID as ConsentSubjectID
from ..snippet.transfer.models import SnippetVersionID

from .models.version import CurrentVersionAssociation, Version
from .transfer.models import VersionID


def create_version(brand_id: BrandID, title: str,
                   snippet_version_id: SnippetVersionID,
                   consent_subject_id: ConsentSubjectID
                  ) -> Version:
    """Create a new version of the terms for that brand."""
    version = Version(brand_id, title, snippet_version_id, consent_subject_id)

    db.session.add(version)
    db.session.commit()

    return version


def find_version(version_id: VersionID) -> Optional[Version]:
    """Return the version with that id, or `None` if not found."""
    return Version.query.get(version_id)


def find_current_version_id(brand_id: BrandID) -> Optional[VersionID]:
    """Return the ID of the current version of the terms for that brand,
    or `None` if no current version is defined.
    """
    return db.session \
        .query(CurrentVersionAssociation.version_id) \
        .filter(CurrentVersionAssociation.brand_id == brand_id) \
        .scalar()


def find_current_version(brand_id: BrandID) -> Optional[Version]:
    """Return the current version of the terms for that brand, or `None`
    if none is defined.
    """
    return Version.query \
        .join(CurrentVersionAssociation) \
        .filter(CurrentVersionAssociation.brand_id == brand_id) \
        .one_or_none()


def get_current_version(brand_id: BrandID) -> Version:<commit_msg>Fetch current terms version for brand and actual entity separately<commit_after>"""
byceps.services.terms.version_service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from typing import Optional, Sequence

from ...database import db
from ...typing import BrandID

from ..brand import settings_service as brand_settings_service
from ..consent.transfer.models import SubjectID as ConsentSubjectID
from ..snippet.transfer.models import SnippetVersionID

from .models.version import CurrentVersionAssociation, Version
from .transfer.models import VersionID


def create_version(brand_id: BrandID, title: str,
                   snippet_version_id: SnippetVersionID,
                   consent_subject_id: ConsentSubjectID
                  ) -> Version:
    """Create a new version of the terms for that brand."""
    version = Version(brand_id, title, snippet_version_id, consent_subject_id)

    db.session.add(version)
    db.session.commit()

    return version


def find_version(version_id: VersionID) -> Optional[Version]:
    """Return the version with that id, or `None` if not found."""
    return Version.query.get(version_id)


def find_current_version_id(brand_id: BrandID) -> Optional[VersionID]:
    """Return the ID of the current version of the terms for that brand,
    or `None` if no current version is defined.
    """
    return db.session \
        .query(CurrentVersionAssociation.version_id) \
        .filter(CurrentVersionAssociation.brand_id == brand_id) \
        .scalar()


def find_current_version(brand_id: BrandID) -> Optional[Version]:
    """Return the current version of the terms for that brand, or `None`
    if none is defined.
    """
    current_version_id = find_current_version_id(brand_id)

    if current_version_id is None:
        return None

    return find_version(current_version_id)


def get_current_version(brand_id: BrandID) -> Version:
{'fraction_non_alphanumeric': 0.10700236034618411, 'fraction_numerical': 0.004196170993968004, 'mean_word_length': 3.679754601226994, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '118288', 'n_tokens_mistral': 1086, 'n_tokens_neox': 1031, 'n_words': 320}