Example #1
0
def query_projects(*,
                   user: User=None,
                   genre: Genre=None,
                   type_: ProjectType=None,
                   title: str=None,
                   body: str=None,
                   followable: bool=None)\
        -> List[Project]:
    q = session.query(Project)
    if user:
        q = q.filter_by(user_id=user.user_id)
    if genre:
        q = q.filter_by(genre=genre)
    if type_:
        q = q.filter_by(type=type_)
    if title:
        title_like = format_like(title)
        q = q.filter(Project.title.ilike(title_like))
    if body:
        body_like = format_like(body)
        q = q.filter(
            db.or_(Project.pitch.ilike(body_like),
                   Project.description.ilike(body_like),
                   Project.title.ilike(body_like)))
    if followable is not None:
        if followable:
            # noinspection PyComparisonWithNone,PyPep8
            q = q.filter(Project.follow_role_id != None)
        else:
            q = q.filter_by(follow_role_id=None)
    return q.all()
Example #2
0
def search_quotes(search_term: str=None, user: Union[User, List[User]]=None) -> List[Quote]:
    """
    Fulltext search for quotes.
    :param search_term: The substring to search for.
    :param user: optional user to filter by
    """

    if not user and not search_term:
        raise ValueError("Must specify at least 1 search criterion")

    if user:
        user_list = [user] if isinstance(user, User) else user  # type: List[User]
    else:
        user_list = []

    query = session.query(Quote)
    if user_list:
        # noinspection PyUnresolvedReferences
        query = query.filter(Quote.author_id.in_(u.user_id for u in user_list))

    if search_term:
        search_term_like = db.format_like(search_term)
        # noinspection PyUnresolvedReferences
        query = query.filter(Quote.message.ilike(search_term_like, escape='\\'))

    results = query.order_by(Quote.timestamp).all()
    try:
        results[0]
    except IndexError:
        raise orm.exc.NoResultFound
    logger.info("search_quotes: Found {:d} results for {!r}".format(len(results), query))
    return results
Example #3
0
def search_users(search_term: str) -> List[User]:
    """
    Search for users.

    :param search_term: The substring to search for - should already be sanitised!
    :return:
    """
    search_term_like = format_like(search_term)
    # noinspection PyUnresolvedReferences
    results = session.query(User).outerjoin(UserAlias) \
        .filter(db.or_(User.name.ilike(search_term_like, escape='\\'),
                UserAlias.name.ilike(search_term_like, escape='\\'))) \
        .order_by(User.name) \
        .all()
    logger.info("search_users: Found {:d} results for {!r}".format(
        len(results), search_term))
    return results