Ejemplo n.º 1
0
def _execute(project, source):
    """Executes document deletion.

    """
    # Format input params.
    project = unicode(project).strip().lower()
    if source is not None:
        source = unicode(source).strip().lower()
        if len(source) == 0:
            source = None

    # Build queries.
    qry1 = session.query(Document.id)
    qry1 = qry1.filter(Document.project == project)
    if source is not None:
        qry1 = qry1.filter(Document.source == source)

    qry2 = session.query(DocumentDRS)
    qry2 = qry2.filter(DocumentDRS.document_id.in_(qry1.subquery()))

    qry3 = session.query(DocumentExternalID)
    qry3 = qry3.filter(DocumentExternalID.document_id.in_(qry1.subquery()))

    qry4 = session.query(DocumentSubProject)
    qry4 = qry4.filter(DocumentSubProject.document_id.in_(qry1.subquery()))

    # Delete data.
    qry4.delete(synchronize_session=False)
    qry3.delete(synchronize_session=False)
    qry2.delete(synchronize_session=False)
    qry1.delete()
Ejemplo n.º 2
0
def _execute(project, source):
    """Executes document deletion.

    """
    # Format input params.
    project = unicode(project).strip().lower()
    if source is not None:
        source = unicode(source).strip().lower()
        if len(source) == 0:
            source = None

    # Build queries.
    qry1 = session.query(Document.id)
    qry1 = qry1.filter(Document.project == project)
    if source is not None:
        qry1 = qry1.filter(Document.source == source)

    qry2 = session.query(DocumentDRS)
    qry2 = qry2.filter(DocumentDRS.document_id.in_(qry1.subquery()))

    qry3 = session.query(DocumentExternalID)
    qry3 = qry3.filter(DocumentExternalID.document_id.in_(qry1.subquery()))

    qry4 = session.query(DocumentSubProject)
    qry4 = qry4.filter(DocumentSubProject.document_id.in_(qry1.subquery()))

    # Delete data.
    qry4.delete(synchronize_session=False)
    qry3.delete(synchronize_session=False)
    qry2.delete(synchronize_session=False)
    qry1.delete()
Ejemplo n.º 3
0
def get_document_by_name(
    project,
    typeof,
    name,
    institute=None,
    latest_only=True
    ):
    """Retrieves a single document by it's name.

    :param str project: Project code.
    :param str typeof: Document type.
    :param str name: Document name.
    :param str institute: Institute code.
    :param boolean latest_only: Project with which document is associated.

    :returns: First matching document.
    :rtype: db.models.Document

    """
    qry = session.query(Document)
    qry = text_filter(qry, Document.project, project)
    qry = text_filter(qry, Document.typeof, typeof)
    qry = text_filter(qry, Document.name, name)
    if institute:
        qry = text_filter(qry, Document.institute, institute)
    if latest_only == True:
        qry = qry.filter(Document.is_latest == True)

    return qry.first()
Ejemplo n.º 4
0
def get_sub_projects():
    """Returns list of indexed sub-projects.

    :returns: List of indexed sub-projects.
    :rtype: list

    """
    qry = session.query(DocumentSubProject.project, DocumentSubProject.sub_project)
    qry = qry.distinct()

    return qry.all()
Ejemplo n.º 5
0
def get_models():
    """Returns list of indexed models.

    :returns: List of indexed models.
    :rtype: list

    """
    qry = session.query(Document.project, Document.model)
    qry = qry.filter(Document.model != "None")
    qry = qry.filter(Document.model is not None)
    qry = qry.distinct()

    return qry.all()
Ejemplo n.º 6
0
def get_institutes():
    """Returns list of indexed institutes.

    :returns: List of indexed institutes.
    :rtype: list

    """
    qry = session.query(Document.project, Document.institute)
    qry = qry.filter(Document.institute != "None")
    qry = qry.filter(Document.institute is not None)
    qry = qry.distinct()

    return qry.all()
Ejemplo n.º 7
0
def get_experiments():
    """Returns list of indexed experiments.

    :returns: List of indexed experiments.
    :rtype: list

    """
    qry = session.query(Document.project, Document.experiment)
    qry = qry.filter(Document.experiment != "None")
    qry = qry.filter(Document.experiment is not None)
    qry = qry.distinct()

    return qry.all()
Ejemplo n.º 8
0
def get_document_types():
    """Returns list of indexed document types.

    :returns: List of indexed document types.
    :rtype: list

    """
    qry = session.query(Document.project, Document.typeof)
    qry = qry.filter(Document.typeof != "None")
    qry = qry.filter(Document.typeof is not None)
    qry = qry.distinct()

    return qry.all()
Ejemplo n.º 9
0
def delete_by_facet(etype, efilter):
    """Delete entity instance by id.

    :param class etype: A supported entity type.
    :param expression|func efilter: Filter to apply.

    """
    qry = session.query(etype)
    if inspect.isfunction(efilter):
        efilter(qry)
    else:
        qry = qry.filter(efilter)
    qry.delete()
Ejemplo n.º 10
0
def get_projects():
    """Returns list of indexed projects.

    :returns: List of indexed projects.
    :rtype: list

    """
    qry = session.query(Document.project)
    qry = qry.filter(Document.project != "None")
    qry = qry.filter(Document.project is not None)
    qry = qry.distinct()

    return sorted([i[0] for i in qry.all() if i[0]])
Ejemplo n.º 11
0
def get_document_by_drs_keys(
    project,
    key_01=None,
    key_02=None,
    key_03=None,
    key_04=None,
    key_05=None,
    key_06=None,
    key_07=None,
    key_08=None,
    latest_only = True
    ):
    """Retrieves a single document by it's drs keys.

    :param str project: Project code.
    :param str key_01: DRS key 1.
    :param str key_02: DRS key 2.
    :param str key_03: DRS key 3.
    :param str key_04: DRS key 4.
    :param str key_05: DRS key 5.
    :param str key_06: DRS key 6.
    :param str key_07: DRS key 7.
    :param str key_08: DRS key 8.
    :param boolean latest_only: Flag indicating whether only the latest document is to be returned.

    :returns: First matching document.
    :rtype: db.models.Document

    """
    qry = session.query(Document).join(DocumentDRS)
    qry = text_filter(qry, Document.project, project)
    if key_01:
        qry = text_filter(qry, DocumentDRS.key_01, key_01)
    if key_02:
        qry = text_filter(qry, DocumentDRS.key_02, key_02)
    if key_03:
        qry = text_filter(qry, DocumentDRS.key_03, key_03)
    if key_04:
        qry = text_filter(qry, DocumentDRS.key_04, key_04)
    if key_05:
        qry = text_filter(qry, DocumentDRS.key_05, key_05)
    if key_06:
        qry = text_filter(qry, DocumentDRS.key_06, key_06)
    if key_07:
        qry = text_filter(qry, DocumentDRS.key_07, key_07)
    if key_08:
        qry = text_filter(qry, DocumentDRS.key_08, key_08)
    if latest_only == True:
        qry = qry.filter(Document.is_latest == True)

    return qry.first()
Ejemplo n.º 12
0
def get_documents_by_external_id(project, external_id):
    """Retrieves a list of documents with a matching external ID.

    :param str project: Project code.
    :param str external_id: External ID to be resolved to a document.

    :returns: List of Document instances with matching external ID.
    :rtype: list

    """
    qry = session.query(Document).join(DocumentExternalID)
    qry = text_filter(qry, Document.project, project)
    qry = like_filter(qry, DocumentExternalID.external_id, external_id.upper())

    return qry.all()
Ejemplo n.º 13
0
def get_document_by_type(project, typeof, latest_only=True):
    """Retrieves documents by type.

    :param str project: Project code.
    :param str typeof: Document type.
    :param boolean latest_only: Flag indicating whether to return only the latest documents.

    :returns: Matching documents.
    :rtype: list

    """
    qry = session.query(Document)
    qry = text_filter(qry, Document.project, project)
    qry = text_filter(qry, Document.typeof, typeof)
    if latest_only == True:
        qry = qry.filter(Document.is_latest == True)

    return qry.all()
Ejemplo n.º 14
0
def get_document_type_count(project, typeof):
    """Returns count over a project's document type.

    :param str typeof: Document type.
    :param str project: Project code.

    :returns: List of counts over a project's document types.
    :rtype: list

    """
    qry = session.query(sa.func.count(Document.typeof))
    qry = text_filter(qry, Document.project, project)
    qry = text_filter(qry, Document.typeof, typeof)
    qry = qry.group_by(Document.typeof)

    counts = qry.all()

    return 0 if not len(counts) else counts[0][0]
Ejemplo n.º 15
0
def get_document(uid, version, project=None):
    """Returns a Document instance by it's project, UID & version.

    :param str uid: Document unique identifier.
    :param str version: Document version.
    :param str project: Project code.

    :returns: First matching document.
    :rtype: db.models.Document

    """
    qry = session.query(Document)
    if project:
        qry = text_filter(qry, Document.project, project)
    qry = qry.filter(Document.uid == unicode(uid))
    if version is None or version in constants.DOCUMENT_VERSIONS:
        qry = qry.order_by(Document.version.desc())
    else:
        qry = qry.filter(Document.version == int(version))

    return qry.all() if version == constants.DOCUMENT_VERSION_ALL else qry.first()
Ejemplo n.º 16
0
def get_by_facet(
    etype,
    efilter,
    order_by=None,
    get_iterable=False
    ):
    """Gets entity instance by facet.

    :param class etype: A supported entity type.
    :param expression efilter: Entity filter expression.
    :param expression order_by: Sort expression.
    :param bool get_iterable: Flag indicating whether to return an iterable or not.

    :returns: Entity or entity collection.
    :rtype: Sub-class of db.models.Entity

    """
    qry = session.query(etype)
    if efilter is not None:
        if inspect.isfunction(efilter):
            efilter(qry)
        else:
            qry = qry.filter(efilter)
    if order_by is not None:
        if inspect.isfunction(order_by):
            order_by(qry)
        else:
            qry = qry.order_by(order_by)

    # Return accordingly.
    # ... first
    if not get_iterable:
        return qry.first()
    # ... sorted collection
    elif order_by is None:
        return sort(etype, qry.all())
    # ... ordered collection
    else:
        return qry.all()
Ejemplo n.º 17
0
def get_document_summaries(
    project,
    typeof,
    version,
    sub_project=None,
    institute=None,
    model=None,
    experiment=None
    ):
    """Returns document summary information.

    :param str project: Project code.
    :param str typeof: Document type.
    :param str version: Document version (latest | all).
    :param str sub_project: Sub-project code.
    :param str institute: Institute code.
    :param str model: Model code.
    :param str experiment: Experiment code.

    :returns: List of matching documents.
    :rtype: db.models.Document

    """
    # Format params.
    if version:
        version = version.lower()
    typeof = typeof.upper()
    if model:
        model = model.upper()
    if experiment:
        experiment = experiment.upper()

    # Set query.
    qry = session.query(
        Document.experiment,
        Document.institute,
        Document.long_name,
        Document.model,
        Document.name,
        Document.canonical_name,
        Document.uid,
        Document.version,
        Document.alternative_name
        )

    # Set filters.
    qry = text_filter(qry, Document.project, project)
    qry = qry.filter(Document.canonical_name != u"")
    if typeof != constants.DOCUMENT_TYPE_ALL:
        qry = text_filter(qry, Document.typeof, typeof)
    if version == constants.DOCUMENT_VERSION_LATEST:
        qry = qry.filter(Document.is_latest == True)
    if experiment:
        qry = text_filter(qry, Document.experiment, experiment)
    if institute:
        qry = text_filter(qry, Document.institute, institute)
    if model:
        qry = text_filter(qry, Document.model, model)
    if sub_project:
        qry = like_filter(qry, Document.sub_projects, "<{}>".format(sub_project.lower()))

    # Apply query limit.
    try:
        load_all = constants.MAPPED_DOCUMENT_TYPES[typeof]['loadAll']
    except AttributeError:
        load_all = False
    if not load_all:
        qry = qry.limit(session.QUERY_LIMIT)

    return sort(Document, qry.all())