def revision_get_documents(revision_id=None, include_history=True, unique_only=True, session=None, **filters): """Return the documents that match filters for the specified `revision_id`. :param revision_id: The ID corresponding to the ``Revision`` object. If the ID is ``None``, then retrieve the latest revision, if one exists. :param include_history: Return all documents for revision history prior and up to current revision, if ``True``. Default is ``True``. :param unique_only: Return only unique documents if ``True. Default is ``True``. :param filters: Dictionary attributes (including nested) used to filter out revision documents. :param session: Database session object. :param filters: Key-value pairs used for filtering out revision documents. :returns: All revision documents for ``revision_id`` that match the ``filters``, including document revision history if applicable. :raises: RevisionNotFound if the revision was not found. """ session = session or get_session() revision_documents = [] try: if revision_id: revision = session.query(models.Revision)\ .filter_by(id=revision_id)\ .one() else: # If no revision_id is specified, grab the latest one. revision = session.query(models.Revision)\ .order_by(models.Revision.created_at.desc())\ .first() revision_documents = (revision.to_dict()['documents'] if revision else []) if include_history and revision: older_revisions = session.query(models.Revision)\ .filter(models.Revision.created_at < revision.created_at)\ .order_by(models.Revision.created_at)\ .all() # Include documents from older revisions in response body. for older_revision in older_revisions: revision_documents.extend( older_revision.to_dict()['documents']) except sa_orm.exc.NoResultFound: raise errors.RevisionNotFound(revision=revision_id) revision_documents = _update_revision_history(revision_documents) filtered_documents = _filter_revision_documents(revision_documents, unique_only, **filters) return filtered_documents
def revision_get(revision_id=None, session=None): """Return the specified `revision_id`. :param revision_id: The ID corresponding to the ``Revision`` object. :param session: Database session object. :returns: Dictionary representation of retrieved revision. :raises RevisionNotFound: if the revision was not found. """ session = session or get_session() try: revision = session.query(models.Revision)\ .filter_by(id=revision_id)\ .one()\ .to_dict() except sa_orm.exc.NoResultFound: raise errors.RevisionNotFound(revision_id=revision_id) revision['documents'] = _update_revision_history(revision['documents']) return revision
def revision_get_latest(session=None): """Return the latest revision. :param session: Database session object. :returns: Dictionary representation of latest revision. :raises RevisionNotFound: if the latest revision was not found. """ session = session or get_session() latest_revision = session.query(models.Revision)\ .order_by(models.Revision.created_at.desc())\ .first() if not latest_revision: raise errors.RevisionNotFound(revision='latest') latest_revision = latest_revision.to_dict() latest_revision['documents'] = _update_revision_history( latest_revision['documents']) return latest_revision