~homeworkprod/byceps

fc2b0a811d246da866f3ad0c09933d1620413088 — Jochen Kupperschmidt a month ago f693a44
Split user search term into separate words and clauses
1 files changed, 15 insertions(+), 9 deletions(-)

M byceps/blueprints/admin/user/service.py
M byceps/blueprints/admin/user/service.py => byceps/blueprints/admin/user/service.py +15 -9
@@ 8,6 8,7 @@ byceps.blueprints.admin.user.service

from collections import defaultdict
from datetime import datetime, timedelta
from itertools import chain
from operator import attrgetter
from typing import Any, Dict, Iterator, List, Optional, Sequence, Set, Tuple



@@ 85,18 86,23 @@ def _filter_by_state(query, state_filter):


def _filter_by_search_term(query, search_term):
    ilike_pattern = f'%{search_term}%'
    terms = search_term.split(' ')
    clauses = chain.from_iterable(map(_generate_search_clauses_for_term, terms))

    return query \
        .join(DbUserDetail) \
        .filter(
            db.or_(
                DbUser.email_address.ilike(ilike_pattern),
                DbUser.screen_name.ilike(ilike_pattern),
                DbUserDetail.first_names.ilike(ilike_pattern),
                DbUserDetail.last_name.ilike(ilike_pattern)
            )
        )
        .filter(db.or_(*clauses))


def _generate_search_clauses_for_term(search_term: str) -> List:
    ilike_pattern = f'%{search_term}%'

    return [
        DbUser.email_address.ilike(ilike_pattern),
        DbUser.screen_name.ilike(ilike_pattern),
        DbUserDetail.first_names.ilike(ilike_pattern),
        DbUserDetail.last_name.ilike(ilike_pattern),
    ]


def get_users_created_since(