Python sqlalchemy.asc() Examples

The following are 30 code examples of sqlalchemy.asc(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sqlalchemy , or try the search function .
Example #1
Source File: instance_database.py    From maubot with GNU Affero General Public License v3.0 6 votes vote down vote up
def get_table(request: web.Request) -> web.Response:
    instance_id = request.match_info.get("id", "")
    instance = PluginInstance.get(instance_id, None)
    if not instance:
        return resp.instance_not_found
    elif not instance.inst_db:
        return resp.plugin_has_no_database
    tables = instance.get_db_tables()
    try:
        table = tables[request.match_info.get("table", "")]
    except KeyError:
        return resp.table_not_found
    try:
        order = [tuple(order.split(":")) for order in request.query.getall("order")]
        order = [(asc if sort.lower() == "asc" else desc)(table.columns[column])
                 if sort else table.columns[column]
                 for column, sort in order]
    except KeyError:
        order = []
    limit = int(request.query.get("limit", 100))
    return execute_query(instance, table.select().order_by(*order).limit(limit)) 
Example #2
Source File: vfolder.py    From backend.ai-manager with GNU Lesser General Public License v3.0 6 votes vote down vote up
def load_slice(cls, context, limit, offset, *,
                         domain_name=None, group_id=None, user_id=None,
                         order_key=None, order_asc=None):
        from .user import users
        async with context['dbpool'].acquire() as conn:
            if order_key is None:
                _ordering = vfolders.c.created_at
            else:
                _order_func = sa.asc if order_asc else sa.desc
                _ordering = _order_func(getattr(vfolders.c, order_key))
            j = sa.join(vfolders, users, vfolders.c.user == users.c.uuid)
            query = (
                sa.select([vfolders])
                .select_from(j)
                .order_by(_ordering)
                .limit(limit)
                .offset(offset)
            )
            if domain_name is not None:
                query = query.where(users.c.domain_name == domain_name)
            if group_id is not None:
                query = query.where(vfolders.c.group == group_id)
            if user_id is not None:
                query = query.where(vfolders.c.user == user_id)
            return [cls.from_row(context, r) async for r in conn.execute(query)] 
Example #3
Source File: __init__.py    From ACE with Apache License 2.0 6 votes vote down vote up
def execute(self):
        from saq.database import Message
        attempted_ids = [] # list of Message.id values we want to delete
        for message in saq.db.query(Message).order_by(Message.insert_date.asc()):
            try:
                dispatch(message)
                attempted_ids.append(message.id)
            except Exception as e:
                logging.error(f"unable to dispatch {message}: {e}")

            if control_function is not None and control_function():
                break

        if attempted_ids:
            saq.db.execute(Message.__table__.delete().where(Message.id.in_(attempted_ids)))
            saq.db.commit()

#
# global mapping of notification systems to the handlers that process them 
Example #4
Source File: __init__.py    From ACE with Apache License 2.0 6 votes vote down vote up
def dispatch_messages(control_function=None):
    """Dispatches all messages currently in queue.
       An optional control_function can return True to break out of the loop prematurely."""
    from saq.database import Message
    attempted_ids = [] # list of Message.id values we want to delete
    for message in saq.db.query(Message).order_by(Message.insert_date.asc()):
        try:
            dispatch(message)
            attempted_ids.append(message.id)
        except Exception as e:
            logging.error(f"unable to dispatch {message}: {e}")

        if control_function is not None and control_function():
            break

    if attempted_ids:
        saq.db.execute(Message.__table__.delete().where(Message.id.in_(attempted_ids)))
        saq.db.commit() 
Example #5
Source File: pagination.py    From octavia with Apache License 2.0 6 votes vote down vote up
def __init__(self, params, sort_dir=constants.DEFAULT_SORT_DIR):
        """Pagination Helper takes params and a default sort direction

        :param params: Contains the following:
                       limit: maximum number of items to return
                       marker: the last item of the previous page; we return
                               the next results after this value.
                       sort: array of attr by which results should be sorted
        :param sort_dir: default direction to sort (asc, desc)
        """
        self.marker = params.get('marker')
        self.sort_dir = self._validate_sort_dir(sort_dir)
        self.limit = self._parse_limit(params)
        self.sort_keys = self._parse_sort_keys(params)
        self.params = params
        self.filters = None
        self.page_reverse = params.get('page_reverse', 'False') 
Example #6
Source File: fuel.py    From biweeklybudget with GNU Affero General Public License v3.0 6 votes vote down vote up
def get(self):
        resdata = []
        prices = db_session.query(
            FuelFill
        ).filter(
            FuelFill.cost_per_gallon.__ne__(None)
        ).order_by(asc(FuelFill.date))
        for point in prices.all():
            ds = point.date.strftime('%Y-%m-%d')
            resdata.append({
                'date': ds,
                'price': float(point.cost_per_gallon)
            })
        res = {
            'data': resdata
        }
        return jsonify(res) 
Example #7
Source File: get.py    From daf-recipes with GNU General Public License v3.0 6 votes vote down vote up
def _unpick_search(sort, allowed_fields=None, total=None):
    ''' This is a helper function that takes a sort string
    eg 'name asc, last_modified desc' and returns a list of
    split field order eg [('name', 'asc'), ('last_modified', 'desc')]
    allowed_fields can limit which field names are ok.
    total controls how many sorts can be specifed '''
    sorts = []
    split_sort = sort.split(',')
    for part in split_sort:
        split_part = part.strip().split()
        field = split_part[0]
        if len(split_part) > 1:
            order = split_part[1].lower()
        else:
            order = 'asc'
        if allowed_fields:
            if field not in allowed_fields:
                raise ValidationError('Cannot sort by field `%s`' % field)
        if order not in ['asc', 'desc']:
            raise ValidationError('Invalid sort direction `%s`' % order)
        sorts.append((field, order))
    if total and len(sorts) > total:
        raise ValidationError(
            'Too many sort criteria provided only %s allowed' % total)
    return sorts 
Example #8
Source File: taskreschedule.py    From airflow with Apache License 2.0 6 votes vote down vote up
def query_for_task_instance(task_instance, descending=False, session=None):
        """
        Returns query for task reschedules for a given the task instance.

        :param session: the database session object
        :type session: sqlalchemy.orm.session.Session
        :param task_instance: the task instance to find task reschedules for
        :type task_instance: airflow.models.TaskInstance
        :param descending: If True then records are returned in descending order
        :type descending: bool
        """
        TR = TaskReschedule
        qry = (
            session
            .query(TR)
            .filter(TR.dag_id == task_instance.dag_id,
                    TR.task_id == task_instance.task_id,
                    TR.execution_date == task_instance.execution_date,
                    TR.try_number == task_instance.try_number)
        )
        if descending:
            return qry.order_by(desc(TR.id))
        else:
            return qry.order_by(asc(TR.id)) 
Example #9
Source File: blocks.py    From grin-pool with Apache License 2.0 6 votes vote down vote up
def get_by_time(cls, ts, range=None):
        if range == None:
            # XXX TODO: Test this
            return database.db.getSession().query(Blocks).filter(Blocks.timestamp <= ts).first()
        else:
            ts_start = ts-range
            ts_end = ts
            return list(database.db.getSession().query(Blocks).filter(and_(Blocks.timestamp >= ts_start, Blocks.timestamp <= ts_end)).order_by(asc(Blocks.height)))



# def main():
#     PROCESS = "GrinPoolBaseModelBlockTest"
#     from grinlib import lib
#     config = lib.get_config()
#     logger = lib.get_logger(PROCESS)
#     logger.error("test")
#     database = lib.get_db()
# 
# 
# if __name__ == "__main__":
#     main() 
Example #10
Source File: columns.py    From sqlakeyset with The Unlicense 6 votes vote down vote up
def __init__(self, x):
        if isinstance(x, str):
            x = column(x)
        if _get_order_direction(x) is None:
            x = asc(x)
        self.uo = x
        _warn_if_nullable(self.comparable_value)
        self.full_name = str(self.element)
        try:
            table_name, name = self.full_name.split('.', 1)
        except ValueError:
            table_name = None
            name = self.full_name

        self.table_name = table_name
        self.name = name 
Example #11
Source File: test_internals.py    From sqlakeyset with The Unlicense 6 votes vote down vote up
def test_oc():
    a = asc('a')
    b = desc('a')
    c = asc('b')
    n = nullslast(desc('a'))

    a = OC(a)
    b = OC(b)
    c = OC(c)
    n = OC(n)

    assert str(a) == str(OC('a'))
    assert a.is_ascending
    assert not b.is_ascending
    assert not n.reversed.reversed.is_ascending
    assert n.reversed.is_ascending
    assert not n.is_ascending # make sure reversed doesn't modify in-place
    assert str(a.element) == str(b.element) == str(n.element)
    assert str(a) == str(b.reversed)
    assert str(n.reversed.reversed) == str(n)

    assert a.name == 'a'
    assert n.name == 'a'
    assert n.quoted_full_name == 'a'
    assert repr(n) == '<OC: a DESC NULLS LAST>' 
Example #12
Source File: test_internals.py    From sqlakeyset with The Unlicense 6 votes vote down vote up
def test_order_manipulation():
    is_asc = lambda c: _get_order_direction(c) == asc_op
    flip = _reverse_order_direction
    scrub = _remove_order_direction
    base = column('a')
    l = base.label('test')
    a = asc(base)
    d = desc(base)
    assert is_asc(a)
    assert not is_asc(d)
    equal_pairs = [
        (scrub(a), base),
        (scrub(d), base),
        (scrub(asc(l)), scrub(a.label('test'))),
        (flip(a), d),
        (flip(d), a),
    ]
    for lhs, rhs in equal_pairs:
        assert str(lhs) == str(rhs) 
Example #13
Source File: test_utils.py    From oslo.db with Apache License 2.0 6 votes vote down vote up
def test_paginate_query_no_pagination(self):
        self.query.order_by.return_value = self.query
        self.mock_asc.side_effect = ['asc']
        self.mock_desc.side_effect = ['desc']

        utils.paginate_query(self.query, self.model, 5,
                             ['user_id', 'project_id'],
                             sort_dirs=['asc', 'desc'])

        self.mock_asc.assert_called_once_with(self.model.user_id)
        self.mock_desc.assert_called_once_with(self.model.project_id)
        self.query.order_by.assert_has_calls([
            mock.call('asc'),
            mock.call('desc'),
        ])
        self.query.limit.assert_called_once_with(5) 
Example #14
Source File: storage_common.py    From armchair-expert with MIT License 5 votes vote down vote up
def all_training_data(self, limit: int = None, order_by: str = None, order='desc') -> List[Tuple[bytes]]:
        query = self._session.query(self._table_type.text)
        if order_by and order == 'desc':
            query = query.order_by(desc(order_by))
        elif order_by and order == 'asc':
            query = query.order_by(asc(order_by))
        if limit:
            query = query.limit(limit)
        return query.all() 
Example #15
Source File: agent.py    From backend.ai-manager with GNU Lesser General Public License v3.0 5 votes vote down vote up
def load_slice(
        cls, context, limit, offset, *,
        scaling_group=None,
        status=None,
        order_key=None,
        order_asc=True,
    ) -> Sequence[Agent]:
        async with context['dbpool'].acquire() as conn:
            # TODO: optimization for pagination using subquery, join
            if order_key is None:
                _ordering = agents.c.id
            else:
                _order_func = sa.asc if order_asc else sa.desc
                _ordering = _order_func(getattr(agents.c, order_key))
            query = (
                sa.select([agents])
                .select_from(agents)
                .order_by(_ordering)
                .limit(limit)
                .offset(offset)
            )
            if scaling_group is not None:
                query = query.where(agents.c.scaling_group == scaling_group)
            if status is not None:
                status = AgentStatus[status]
                query = query.where(agents.c.status == status)
            return [
                cls.from_row(context, row) async for row in conn.execute(query)
            ] 
Example #16
Source File: kernel.py    From backend.ai-manager with GNU Lesser General Public License v3.0 5 votes vote down vote up
def load_slice(cls, context, limit, offset, session_id, *,
                         role=None,
                         domain_name=None, group_id=None, access_key=None,
                         order_key=None, order_asc=None):
        async with context['dbpool'].acquire() as conn:
            if order_key is None:
                _ordering = DEFAULT_SESSION_ORDERING
            else:
                _order_func = sa.asc if order_asc else sa.desc
                _ordering = [_order_func(getattr(kernels.c, order_key))]
            j = (
                kernels
                .join(groups, groups.c.id == kernels.c.group_id)
                .join(users, users.c.uuid == kernels.c.user_uuid)
            )
            query = (
                sa.select([kernels, groups.c.name, users.c.email])
                .select_from(j)
                # TODO: use "owner session ID" when we implement multi-container session
                .where(kernels.c.id == session_id)
                .order_by(*_ordering)
                .limit(limit)
                .offset(offset)
            )
            if role is not None:
                query = query.where(kernels.c.role == role)
            if domain_name is not None:
                query = query.where(kernels.c.domain_name == domain_name)
            if group_id is not None:
                query = query.where(kernels.c.group_id == group_id)
            if access_key is not None:
                query = query.where(kernels.c.access_key == access_key)
            return [cls.from_row(context, r) async for r in conn.execute(query)] 
Example #17
Source File: kernel.py    From backend.ai-manager with GNU Lesser General Public License v3.0 5 votes vote down vote up
def load_slice(cls, context, limit, offset, *,
                         domain_name=None, group_id=None, access_key=None,
                         status=None,
                         order_key=None, order_asc=None):
        if isinstance(status, str):
            status_list = [KernelStatus[s] for s in status.split(',')]
        elif isinstance(status, KernelStatus):
            status_list = [status]
        async with context['dbpool'].acquire() as conn:
            if order_key is None:
                _ordering = DEFAULT_SESSION_ORDERING
            else:
                _order_func = sa.asc if order_asc else sa.desc
                _ordering = [_order_func(getattr(kernels.c, order_key))]
            j = (
                kernels
                .join(groups, groups.c.id == kernels.c.group_id)
                .join(users, users.c.uuid == kernels.c.user_uuid)
            )
            query = (
                sa.select([kernels, groups.c.name, users.c.email])
                .select_from(j)
                .where(kernels.c.role == 'master')
                .order_by(*_ordering)
                .limit(limit)
                .offset(offset)
            )
            if domain_name is not None:
                query = query.where(kernels.c.domain_name == domain_name)
            if group_id is not None:
                query = query.where(kernels.c.group_id == group_id)
            if access_key is not None:
                query = query.where(kernels.c.access_key == access_key)
            if status is not None:
                query = query.where(kernels.c.status.in_(status_list))
            return [cls.from_row(context, r) async for r in conn.execute(query)] 
Example #18
Source File: keypair.py    From backend.ai-manager with GNU Lesser General Public License v3.0 5 votes vote down vote up
def load_slice(
        cls, context, limit, offset, *,
        domain_name=None,
        email=None,
        is_active=None,
        order_key=None,
        order_asc=True,
    ) -> Sequence[KeyPair]:
        from .user import users
        async with context['dbpool'].acquire() as conn:
            if order_key is None:
                _ordering = sa.desc(keypairs.c.created_at)
            else:
                _order_func = sa.asc if order_asc else sa.desc
                _ordering = _order_func(getattr(keypairs.c, order_key))
            j = sa.join(keypairs, users, keypairs.c.user == users.c.uuid)
            query = (
                sa.select([keypairs])
                .select_from(j)
                .order_by(_ordering)
                .limit(limit)
                .offset(offset)
            )
            if domain_name is not None:
                query = query.where(users.c.domain_name == domain_name)
            if email is not None:
                query = query.where(keypairs.c.user_id == email)
            if is_active is not None:
                query = query.where(keypairs.c.is_active == is_active)
            return [
                cls.from_row(context, row) async for row in conn.execute(query)
            ] 
Example #19
Source File: query.py    From pagure with GNU General Public License v2.0 5 votes vote down vote up
def get_user_activity_day(session, user, date, tz="UTC"):
    """ Return the activity of the specified user on the specified date.
    'offset' is intended to be a timezone offset from UTC, in minutes:
    you can discover the offset for a timezone and pass that, so this
    will return activity that occurred on the specified date in the
    desired timezone. Note, offset should be the amount of minutes
    that should be added to the UTC time to produce the local time -
    so for timezones behind UTC the number should be negative, and
    for timezones ahead of UTC the number should be positive. This is
    the opposite of what Javascript getTimezoneOffset() does, so you
    have to invert any value you get from that.
    """
    dt = datetime.datetime.strptime(date, "%Y-%m-%d")
    # if the offset is *negative* some of the events we want may be
    # on the next day in UTC terms. if the offset is *positive* some
    # of the events we want may be on the previous day in UTC terms.
    # 'dt' will be at 00:00, so we subtract 1 day for prevday but add
    # 2 days for nextday. e.g. 2018-02-15 00:00 - prevday will be
    # 2018-02-14 00:00, nextday will be 2018-02-17 00:00. We'll get
    # all events that occurred on 2018-02-14, 2018-02-15 or 2018-02-16
    # in UTC time.
    prevday = dt - datetime.timedelta(days=1)
    nextday = dt + datetime.timedelta(days=2)
    query = (
        session.query(model.PagureLog)
        .filter(model.PagureLog.date_created.between(prevday, nextday))
        .filter(model.PagureLog.user_id == user.id)
        .order_by(model.PagureLog.id.asc())
    )
    events = query.all()
    # Now we filter down to the events that *really* occurred on the
    # date we were asked for with the offset applied, and return
    return [ev for ev in events if ev.date_tz(tz) == dt.date()] 
Example #20
Source File: manager.py    From eventsourcing with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_notification_records(
        self,
        start: Optional[int] = None,
        stop: Optional[int] = None,
        *args: Any,
        **kwargs: Any
    ) -> Iterable:
        try:
            query = self.orm_query()
            query = self.filter_for_application_name(query)
            query = self.filter_for_pipeline_id(query)

            if self.notification_id_name:
                query = query.order_by(asc(self.notification_id_name))
                # NB '+1' because record IDs start from 1.
                notification_id_col = getattr(
                    self.record_class, self.notification_id_name
                )
                if start is not None:
                    query = query.filter(notification_id_col >= start + 1)
                if stop is not None:
                    query = query.filter(notification_id_col < stop + 1)
            # Todo: Should some tables with an ID not be ordered by ID?
            # Todo: Which order do other tables have?
            return list(query.all())
        except sqlalchemy.exc.OperationalError as e:
            raise OperationalError(e)
        finally:
            self.session.close() 
Example #21
Source File: elements.py    From jbox with MIT License 5 votes vote down vote up
def _create_nullsfirst(cls, column):
        """Produce the ``NULLS FIRST`` modifier for an ``ORDER BY`` expression.

        :func:`.nullsfirst` is intended to modify the expression produced
        by :func:`.asc` or :func:`.desc`, and indicates how NULL values
        should be handled when they are encountered during ordering::


            from sqlalchemy import desc, nullsfirst

            stmt = select([users_table]).\\
                        order_by(nullsfirst(desc(users_table.c.name)))

        The SQL expression from the above would resemble::

            SELECT id, name FROM user ORDER BY name DESC NULLS FIRST

        Like :func:`.asc` and :func:`.desc`, :func:`.nullsfirst` is typically
        invoked from the column expression itself using
        :meth:`.ColumnElement.nullsfirst`, rather than as its standalone
        function version, as in::

            stmt = (select([users_table]).
                    order_by(users_table.c.name.desc().nullsfirst())
                    )

        .. seealso::

            :func:`.asc`

            :func:`.desc`

            :func:`.nullslast`

            :meth:`.Select.order_by`

        """
        return UnaryExpression(
            _literal_as_label_reference(column),
            modifier=operators.nullsfirst_op,
            wraps_column_expression=False) 
Example #22
Source File: elements.py    From jbox with MIT License 5 votes vote down vote up
def _create_nullslast(cls, column):
        """Produce the ``NULLS LAST`` modifier for an ``ORDER BY`` expression.

        :func:`.nullslast` is intended to modify the expression produced
        by :func:`.asc` or :func:`.desc`, and indicates how NULL values
        should be handled when they are encountered during ordering::


            from sqlalchemy import desc, nullslast

            stmt = select([users_table]).\\
                        order_by(nullslast(desc(users_table.c.name)))

        The SQL expression from the above would resemble::

            SELECT id, name FROM user ORDER BY name DESC NULLS LAST

        Like :func:`.asc` and :func:`.desc`, :func:`.nullslast` is typically
        invoked from the column expression itself using
        :meth:`.ColumnElement.nullslast`, rather than as its standalone
        function version, as in::

            stmt = select([users_table]).\\
                        order_by(users_table.c.name.desc().nullslast())

        .. seealso::

            :func:`.asc`

            :func:`.desc`

            :func:`.nullsfirst`

            :meth:`.Select.order_by`

        """
        return UnaryExpression(
            _literal_as_label_reference(column),
            modifier=operators.nullslast_op,
            wraps_column_expression=False) 
Example #23
Source File: elements.py    From jbox with MIT License 5 votes vote down vote up
def _create_desc(cls, column):
        """Produce a descending ``ORDER BY`` clause element.

        e.g.::

            from sqlalchemy import desc

            stmt = select([users_table]).order_by(desc(users_table.c.name))

        will produce SQL as::

            SELECT id, name FROM user ORDER BY name DESC

        The :func:`.desc` function is a standalone version of the
        :meth:`.ColumnElement.desc` method available on all SQL expressions,
        e.g.::


            stmt = select([users_table]).order_by(users_table.c.name.desc())

        :param column: A :class:`.ColumnElement` (e.g. scalar SQL expression)
         with which to apply the :func:`.desc` operation.

        .. seealso::

            :func:`.asc`

            :func:`.nullsfirst`

            :func:`.nullslast`

            :meth:`.Select.order_by`

        """
        return UnaryExpression(
            _literal_as_label_reference(column),
            modifier=operators.desc_op,
            wraps_column_expression=False) 
Example #24
Source File: elements.py    From jbox with MIT License 5 votes vote down vote up
def _create_asc(cls, column):
        """Produce an ascending ``ORDER BY`` clause element.

        e.g.::

            from sqlalchemy import asc
            stmt = select([users_table]).order_by(asc(users_table.c.name))

        will produce SQL as::

            SELECT id, name FROM user ORDER BY name ASC

        The :func:`.asc` function is a standalone version of the
        :meth:`.ColumnElement.asc` method available on all SQL expressions,
        e.g.::


            stmt = select([users_table]).order_by(users_table.c.name.asc())

        :param column: A :class:`.ColumnElement` (e.g. scalar SQL expression)
         with which to apply the :func:`.asc` operation.

        .. seealso::

            :func:`.desc`

            :func:`.nullsfirst`

            :func:`.nullslast`

            :meth:`.Select.order_by`

        """
        return UnaryExpression(
            _literal_as_label_reference(column),
            modifier=operators.asc_op,
            wraps_column_expression=False) 
Example #25
Source File: identity.py    From rucio with Apache License 2.0 5 votes vote down vote up
def get_default_account(identity, type, oldest_if_none=False, session=None):
    """
    Retrieves the default account mapped to an identity.

    :param identity: The identity key name. For example, x509DN, or a username.
    :param type: The type of the authentication (x509, gss, userpass, saml, oidc).
    :param oldest_if_none: If True and no default account it found the oldes known
                           account of that identity will be chosen, if False and
                           no default account is found, exception will be raised.
    :param session: The database session to use.
    :returns: The default account name, None otherwise.
    """

    tmp = session.query(models.IdentityAccountAssociation).filter_by(identity=identity,
                                                                     identity_type=type,
                                                                     is_default=True).first()
    if tmp is None:
        if oldest_if_none:
            tmp = session.query(models.IdentityAccountAssociation)\
                         .filter_by(identity=identity, identity_type=type)\
                         .order_by(asc(models.IdentityAccountAssociation.created_at)).first()
            if tmp is None:
                raise exception.IdentityError('There is no account for identity (%s, %s)' % (identity, type))
        else:
            raise exception.IdentityError('There is no default account for identity (%s, %s)' % (identity, type))

    return tmp.account 
Example #26
Source File: sa.py    From aiohttp_admin with Apache License 2.0 5 votes vote down vote up
def list(self, request):
        await require(request, Permissions.view)
        columns_names = list(self._table.c.keys())
        q = validate_query(request.query, columns_names)
        paging = calc_pagination(q, self._primary_key)

        filters = q.get('_filters')
        async with self.pool.acquire() as conn:
            if filters:
                query = create_filter(self.table, filters)
            else:
                query = self.table.select()
            count = await conn.scalar(
                sa.select([sa.func.count()])
                .select_from(query.alias('foo')))

            sort_dir = sa.asc if paging.sort_dir == ASC else sa.desc
            cursor = await conn.execute(
                query
                .offset(paging.offset)
                .limit(paging.limit)
                .order_by(sort_dir(paging.sort_field)))

            recs = await cursor.fetchall()

            entities = list(map(dict, recs))

        headers = {'X-Total-Count': str(count)}
        return json_response(entities, headers=headers) 
Example #27
Source File: utils.py    From designate with Apache License 2.0 5 votes vote down vote up
def sort_query(query, table, sort_keys, sort_dir=None, sort_dirs=None):

    if 'id' not in sort_keys:
        # TODO(justinsb): If this ever gives a false-positive, check
        # the actual primary key, rather than assuming its id
        LOG.warning('Id not in sort_keys; is sort_keys unique?')

    assert(not (sort_dir and sort_dirs))

    # Default the sort direction to ascending
    if sort_dirs is None and sort_dir is None:
        sort_dir = 'asc'

    # Ensure a per-column sort direction
    if sort_dirs is None:
        sort_dirs = [sort_dir for _sort_key in sort_keys]

    assert(len(sort_dirs) == len(sort_keys))

    for current_sort_key, current_sort_dir in \
            six.moves.zip(sort_keys, sort_dirs):
        try:
            sort_dir_func = {
                'asc': sqlalchemy.asc,
                'desc': sqlalchemy.desc,
            }[current_sort_dir]
        except KeyError:
            raise ValueError(_("Unknown sort direction, "
                               "must be 'desc' or 'asc'"))
        try:
            sort_key_attr = getattr(table.c, current_sort_key)
        except AttributeError:
            raise utils.InvalidSortKey()
        query = query.order_by(sort_dir_func(sort_key_attr))

    return query, sort_dirs 
Example #28
Source File: sqlaudit.py    From privacyidea with GNU Affero General Public License v3.0 5 votes vote down vote up
def search(self, search_dict, page_size=15, page=1, sortorder="asc",
               timelimit=None):
        """
        This function returns the audit log as a Pagination object.

        :param timelimit: Only audit entries newer than this timedelta will
            be searched
        :type timelimit: timedelta
        """
        page = int(page)
        page_size = int(page_size)
        paging_object = Paginate()
        paging_object.page = page
        paging_object.total = self.get_total(search_dict, timelimit=timelimit)
        if page > 1:
            paging_object.prev = page - 1
        if paging_object.total > (page_size * page):
            paging_object.next = page + 1

        auditIter = self.search_query(search_dict, page_size=page_size,
                                      page=page, sortorder=sortorder,
                                      timelimit=timelimit)
        while True:
            try:
                le = next(auditIter)
                # Fill the list
                paging_object.auditdata.append(self.audit_entry_to_dict(le))
            except StopIteration as _e:
                log.debug("Interation stopped.")
                break
            except UnicodeDecodeError as _e:
                # Unfortunately if one of the audit entries fails, the whole
                # iteration stops and we return an empty paging_object.
                # TODO: Check if we can return the other entries in the auditIter
                #  or some meaningful error for the user.
                log.warning('Could not read audit log entry! '
                            'Possible database encoding mismatch.')
                log.debug("{0!s}".format(traceback.format_exc()))

        return paging_object 
Example #29
Source File: sqlaudit.py    From privacyidea with GNU Affero General Public License v3.0 5 votes vote down vote up
def search_query(self, search_dict, page_size=15, page=1, sortorder="asc",
                     sortname="number", timelimit=None):
        """
        This function returns the audit log as an iterator on the result

        :param timelimit: Only audit entries newer than this timedelta will
            be searched
        :type timelimit: timedelta
        """
        logentries = None
        try:
            limit = int(page_size)
            offset = (int(page) - 1) * limit
            
            # create filter condition
            filter_condition = self._create_filter(search_dict,
                                                   timelimit=timelimit)

            if sortorder == "desc":
                logentries = self.session.query(LogEntry).filter(
                    filter_condition).order_by(
                    desc(self._get_logentry_attribute("number"))).limit(
                    limit).offset(offset)
            else:
                logentries = self.session.query(LogEntry).filter(
                    filter_condition).order_by(
                    asc(self._get_logentry_attribute("number"))).limit(
                    limit).offset(offset)
                                         
        except Exception as exx:  # pragma: no cover
            log.error("exception {0!r}".format(exx))
            log.debug("{0!s}".format(traceback.format_exc()))
            self.session.rollback()
        finally:
            self.session.close()

        if logentries is None:
            return iter([])
        else:
            return iter(logentries) 
Example #30
Source File: test_biweeklypayperiod.py    From biweeklybudget with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_scheduled_transactions_per_period(self):
        res = self.cls._scheduled_transactions_per_period()
        frv = self.mock_sess.query.return_value.filter.return_value
        assert res == frv.order_by.return_value
        assert len(self.mock_sess.mock_calls) == 3
        assert self.mock_sess.mock_calls[0] == call.query(ScheduledTransaction)
        kall = self.mock_sess.mock_calls[1]
        assert kall[0] == 'query().filter'
        expected = ScheduledTransaction.schedule_type.__eq__('per period')
        assert binexp_to_dict(expected) == binexp_to_dict(kall[1][0])
        kall = self.mock_sess.mock_calls[2]
        assert kall[0] == 'query().filter().order_by'
        assert str(kall[1][0]) == str(asc(ScheduledTransaction.num_per_period))
        assert str(kall[1][1]) == str(asc(ScheduledTransaction.amount))