<commit_before>"""
byceps.blueprints.user_admin.service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from typing import Any, Dict, Iterator, Tuple

from ...database import db
from ...services.newsletter import service as newsletter_service
from ...services.shop.order import service as order_service
from ...services.terms import service as terms_service
from ...services.user import event_service
from ...services.user.models.detail import UserDetail
from ...services.user.models.event import UserEvent, UserEventData
from ...services.user.models.user import User, UserTuple
from ...services.user import service as user_service
from ...services.user_avatar import service as avatar_service
from ...typing import UserID

from .models import UserStateFilter


def get_users_paginated(page, per_page, *, search_term=None, state_filter=None):
    """Return the users to show on the specified page, optionally
    filtered by search term or 'enabled' flag.
    """
    query = User.query \
        .options(db.joinedload('detail')) \
        .order_by(User.created_at.desc())

    query = _filter_by_state(query, state_filter)

    if search_term:
        query = _filter_by_search_term(query, search_term)

    return query.paginate(page, per_page)


def _filter_by_search_term(query, search_term):
    ilike_pattern = '%{}%'.format(search_term)

    return query \
        .join(UserDetail) \
        .filter(
            db.or_(
                User.email_address.ilike(ilike_pattern),
                User.screen_name.ilike(ilike_pattern),
                UserDetail.first_names.ilike(ilike_pattern),
                UserDetail.last_name.ilike(ilike_pattern)
            )
        )


def _filter_by_state(query, state_filter):
    if state_filter == UserStateFilter.enabled:
        return query.filter_by(enabled=True)
    elif state_filter == UserStateFilter.disabled:
        return query.filter_by(enabled=False)
    elif state_filter == UserStateFilter.suspended:
        return query.filter_by(suspended=True)
    elif state_filter == UserStateFilter.deleted:
        return query.filter_by(deleted=True)
    else:
        return query


def get_events(user_id: UserID) -> Iterator[UserEventData]:
    events = event_service.get_events_for_user(user_id)
    events.insert(0, _fake_user_creation_event(user_id))
    events.extend(_fake_avatar_update_events(user_id))<commit_msg>Make user filters by flag more exclusive<commit_after>"""
byceps.blueprints.user_admin.service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

from typing import Any, Dict, Iterator, Tuple

from ...database import db
from ...services.newsletter import service as newsletter_service
from ...services.shop.order import service as order_service
from ...services.terms import service as terms_service
from ...services.user import event_service
from ...services.user.models.detail import UserDetail
from ...services.user.models.event import UserEvent, UserEventData
from ...services.user.models.user import User, UserTuple
from ...services.user import service as user_service
from ...services.user_avatar import service as avatar_service
from ...typing import UserID

from .models import UserStateFilter


def get_users_paginated(page, per_page, *, search_term=None, state_filter=None):
    """Return the users to show on the specified page, optionally
    filtered by search term or 'enabled' flag.
    """
    query = User.query \
        .options(db.joinedload('detail')) \
        .order_by(User.created_at.desc())

    query = _filter_by_state(query, state_filter)

    if search_term:
        query = _filter_by_search_term(query, search_term)

    return query.paginate(page, per_page)


def _filter_by_search_term(query, search_term):
    ilike_pattern = '%{}%'.format(search_term)

    return query \
        .join(UserDetail) \
        .filter(
            db.or_(
                User.email_address.ilike(ilike_pattern),
                User.screen_name.ilike(ilike_pattern),
                UserDetail.first_names.ilike(ilike_pattern),
                UserDetail.last_name.ilike(ilike_pattern)
            )
        )


def _filter_by_state(query, state_filter):
    if state_filter == UserStateFilter.enabled:
        return query \
            .filter_by(enabled=True) \
            .filter_by(suspended=False) \
            .filter_by(deleted=False)
    elif state_filter == UserStateFilter.disabled:
        return query \
            .filter_by(enabled=False) \
            .filter_by(suspended=False) \
            .filter_by(deleted=False)
    elif state_filter == UserStateFilter.suspended:
        return query \
            .filter_by(suspended=True) \
            .filter_by(deleted=False)
    elif state_filter == UserStateFilter.deleted:
        return query \
            .filter_by(deleted=True)
    else:
        return query


def get_events(user_id: UserID) -> Iterator[UserEventData]:
    events = event_service.get_events_for_user(user_id)
    events.insert(0, _fake_user_creation_event(user_id))
    events.extend(_fake_avatar_update_events(user_id))
{'fraction_non_alphanumeric': 0.10842448666282863, 'fraction_numerical': 0.0034542314335060447, 'mean_word_length': 3.28266228430567, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '3385249', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1501, 'n_words': 344}