Did I find the right examples for you? yes no

All Samples(3)  |  Call(2)  |  Derive(0)  |  Import(1)
Given parsed_query and max_timestamp, find the resulting ItemVersion ids,
in the right order.

        def query_itemversions(session, model, parsed_query, max_timestamp=None):
    """Given parsed_query and max_timestamp, find the resulting ItemVersion ids,
    in the right order."""

    from sqlalchemy.sql import select, and_, or_, not_, func

    # find view ordering
    view_orderings = (session.query(ViewOrdering)
            .filter_by(norm_query=unicode(parsed_query))
            .order_by(ViewOrdering.timestamp.desc())
            .limit(1)).all()

    if view_orderings:
        have_view_ordering = len(view_orderings[0].entries) != 0
    else:
        have_view_ordering = False

    # compose base set: the current versions of all items
    from_obj = get_current_itemversions_join(model, max_timestamp)

    if have_view_ordering:
        # if we have an ordering, we must also join the viewordering_entries table
        # so we can order by weight

        vo_entries = (
                select([model.viewordering_entries])
                .where(model.viewordering_entries.c.viewordering_id
                    ==view_orderings[0].id)).alias("vo_entries")

        from_obj = from_obj.outerjoin(vo_entries,
                model.itemversions.c.item_id==vo_entries.c.item_id)

    from synoptic.datamodel import SQLifyQueryVisitor
    visitor = SQLifyQueryVisitor(session)

    conditions = [
            model.itemversions.c.contents != None,
            parsed_query.visit(visitor)
            ]

    if visitor.use_hide_until:
        from time import time
        conditions.append(
                or_(ItemVersion.hide_until < time(),
                    ItemVersion.hide_until == None))

    where = and_(*conditions)

    result = select([model.itemversions], from_obj=[from_obj]).where(where)

    if visitor.sort_by_date:
        result = result.order_by(ItemVersion.start_date)
    elif have_view_ordering:
        # add the ordering clause
        result = result.order_by(vo_entries.c.weight)
    else:
        result = result.order_by(model.itemversions.c.timestamp.desc())

    return result.group_by(model.itemversions.c.item_id)
        


src/s/y/synoptic-2013.1/synoptic/__init__.py   synoptic(Download)
from synoptic.datamodel import \
        Item, ItemVersion, Tag, ViewOrdering, ViewOrderingEntry, \
        store_itemversion,  \
        get_current_itemversions_join, \
        query_itemversions
                .query(ItemVersion)
                .from_statement(query_itemversions(
                    session, model, parsed_query, max_timestamp)))
 
    def get_json_items(self, session, model, parsed_query, max_timestamp):
 
        if parsed_query is not None:
            itemversions_q = query_itemversions(session, model,
                    parsed_query, max_timestamp)
        else: