Esempio n. 1
0
def for_relations(q, Rel):
    # Source = aliased(Entity)
    # Target = aliased(Entity)
    # q = q.join(Source, Rel.source)
    # q = q.join(Target, Rel.target)

    q = q.filter(Rel.project_id.in_(permissions().get("reader")))

    project = single_arg("project")
    if project:
        Proj = aliased(Project)
        q = q.join(Proj, Rel.project)
        q = q.filter(Proj.slug == project)

    q = property_filters(Relation, q, Rel)

    if "source" in request.args:
        q = q.filter(Rel.source_id == single_arg("source"))

    if "target" in request.args:
        q = q.filter(Rel.target_id == single_arg("target"))

    if "entity" in request.args:
        id = single_arg("entity")
        q = q.filter(or_(Rel.target_id == id, Rel.source_id == id))

    schemata = request.args.getlist("schema")
    if len(schemata):
        alias = aliased(Schema)
        q = q.join(alias, Rel.schema)
        q = q.filter(alias.name.in_(schemata))

    return q
Esempio n. 2
0
def for_entities(q, Ent):
    """ Get all entities the current user has access to. Accepts project and
    additional filter parameters. """
    # NOTE: I'm passing in the query and entity alias so that this
    # function can be re-used from the facetting code to constrain
    # the results of the facet sub-query.
    q = q.filter(Ent.same_as == None)  # noqa
    q = q.filter(Ent.project_id.in_(permissions().get("reader")))

    if "project" in request.args:
        Proj = aliased(Project)
        q = q.join(Proj, Ent.project)
        q = q.filter(Proj.slug == single_arg("project"))

    q = property_filters(Entity, q, Ent)

    if "q" in request.args and single_arg("q"):
        EntProp = aliased(Property)
        q_text = "%%%s%%" % single_arg("q")
        q = q.join(EntProp)
        q = q.filter(EntProp.name == "name")
        q = q.filter(EntProp.value_string.ilike(q_text))

    schemata = request.args.getlist("schema")
    if len(schemata):
        alias = aliased(Schema)
        q = q.join(alias, Ent.schema)
        q = q.filter(alias.name.in_(schemata))

    return q
Esempio n. 3
0
def for_relations(q, Rel):
    #Source = aliased(Entity)
    #Target = aliased(Entity)
    #q = q.join(Source, Rel.source)
    #q = q.join(Target, Rel.target)

    q = q.filter(Rel.project_id.in_(permissions().get('reader')))

    project = single_arg('project')
    if project:
        Proj = aliased(Project)
        q = q.join(Proj, Rel.project)
        q = q.filter(Proj.slug == project)

    q = property_filters(Relation, q, Rel)

    if 'source' in request.args:
        q = q.filter(Rel.source_id == single_arg('source'))

    if 'target' in request.args:
        q = q.filter(Rel.target_id == single_arg('target'))

    if 'entity' in request.args:
        id = single_arg('entity')
        q = q.filter(or_(Rel.target_id == id, Rel.source_id == id))

    schemata = request.args.getlist('schema')
    if len(schemata):
        alias = aliased(Schema)
        q = q.join(alias, Rel.schema)
        q = q.filter(alias.name.in_(schemata))

    return q
Esempio n. 4
0
def for_relations(q, Rel):
    Proj = aliased(Project)
    Perm = aliased(Permission)
    q = q.join(Proj, Rel.project)
    q = q.outerjoin(Perm, Proj.permissions)

    # TODO: Entity status checks
    q = q.filter(or_(Proj.private == False,
                 and_(Perm.reader == True, Perm.account == request.account)))

    project = single_arg('project')
    if project:
        q = q.filter(Proj.slug == project)

    q = property_filters(Relation, q)

    if 'source' in request.args:
        q = q.filter(Rel.source_id == single_arg('source'))

    if 'target' in request.args:
        q = q.filter(Rel.target_id == single_arg('target'))

    if 'schema' in request.args:
        schemata = request.args.get('schema').split(',')
        alias = aliased(Schema)
        q = q.join(alias, Rel.schema)
        q = q.filter(alias.name.in_(schemata))

    return q
Esempio n. 5
0
def for_entities(q, Ent):
    """ Get all entities the current user has access to. Accepts project and
    additional filter parameters. """
    # NOTE: I'm passing in the query and entity alias so that this
    # function can be re-used from the facetting code to constrain
    # the results of the facet sub-query.
    q = q.filter(Ent.same_as == None)  # noqa
    q = q.filter(Ent.project_id.in_(permissions().get('reader')))

    if 'project' in request.args:
        Proj = aliased(Project)
        q = q.join(Proj, Ent.project)
        q = q.filter(Proj.slug == single_arg('project'))

    q = property_filters(Entity, q, Ent)

    if 'q' in request.args and single_arg('q'):
        EntProp = aliased(Property)
        q_text = '%%%s%%' % single_arg('q')
        q = q.join(EntProp)
        q = q.filter(EntProp.name == 'name')
        q = q.filter(EntProp.value_string.ilike(q_text))

    schemata = request.args.getlist('schema')
    if len(schemata):
        alias = aliased(Schema)
        q = q.join(alias, Ent.schema)
        q = q.filter(alias.name.in_(schemata))

    return q
Esempio n. 6
0
def for_relations(q, Rel):
    Proj = aliased(Project)
    Perm = aliased(Permission)
    Source = aliased(Entity)
    Target = aliased(Entity)
    q = q.join(Proj, Rel.project)
    q = q.join(Source, Rel.source)
    q = q.join(Target, Rel.target)
    q = q.outerjoin(Perm, Proj.permissions)

    q = q.filter(or_(Proj.private == False,
        and_(Perm.reader == True, Perm.account == request.account)))
    q = q.filter(or_(
        and_(
            Proj.private == False,
            Source.status >= PUBLISHED_THRESHOLD,
            Target.status >= PUBLISHED_THRESHOLD,
        ),
        and_(
            Perm.reader == True,
            Source.status >= PUBLISHED_THRESHOLD,
            Target.status >= PUBLISHED_THRESHOLD,
            Perm.account == request.account
        ),
        and_(
            Perm.editor == True,
            Perm.account == request.account
        )
    ))

    project = single_arg('project')
    if project:
        q = q.filter(Proj.slug == project)

    q = property_filters(Relation, q)

    if 'source' in request.args:
        q = q.filter(Rel.source_id == single_arg('source'))

    if 'target' in request.args:
        q = q.filter(Rel.target_id == single_arg('target'))

    if 'schema' in request.args:
        schemata = request.args.get('schema').split(',')
        alias = aliased(Schema)
        q = q.join(alias, Rel.schema)
        q = q.filter(alias.name.in_(schemata))

    return q
Esempio n. 7
0
def for_entities(q, Ent):
    """ Get all entities the current user has access to. Accepts project and
    additional filter parameters. """
    # NOTE: I'm passing in the query and entity alias so that this
    # function can be re-used from the facetting code to constrain
    # the results of the facet sub-query.
    Proj = aliased(Project)
    Perm = aliased(Permission)
    q = q.join(Proj, Ent.project).outerjoin(Perm, Proj.permissions)
    q = q.filter(Ent.same_as == None)

    q = q.filter(or_(
        and_(
            Proj.private == False,
            Ent.status >= PUBLISHED_THRESHOLD,
        ),
        and_(
            Perm.reader == True,
            Ent.status >= PUBLISHED_THRESHOLD,
            Perm.account == request.account
        ),
        and_(
            Perm.editor == True,
            Perm.account == request.account
        )
    ))

    if 'project' in request.args:
        q = q.filter(Proj.slug == single_arg('project'))

    q = property_filters(Entity, q)

    if 'q' in request.args and single_arg('q'):
        EntProp = aliased(EntityProperty)
        q_text = '%%%s%%' % single_arg('q')
        q = q.join(EntProp)
        q = q.filter(EntProp.name == 'name')
        q = q.filter(EntProp.value_string.ilike(q_text))

    for schema in request.args.getlist('schema'):
        if not len(schema.strip()):
            continue
        alias = aliased(Schema)
        q = q.join(alias, Ent.schemata)
        q = q.filter(alias.name.in_(schema.split(',')))

    return q
Esempio n. 8
0
def for_relations(q, Rel):
    Proj = aliased(Project)
    Perm = aliased(Permission)
    Source = aliased(Entity)
    Target = aliased(Entity)
    q = q.join(Proj, Rel.project)
    q = q.join(Source, Rel.source)
    q = q.join(Target, Rel.target)
    q = q.outerjoin(Perm, Proj.permissions)

    q = q.filter(
        or_(Proj.private == False,
            and_(Perm.reader == True, Perm.account == request.account)))
    q = q.filter(
        or_(
            and_(
                Proj.private == False,
                Source.status >= PUBLISHED_THRESHOLD,
                Target.status >= PUBLISHED_THRESHOLD,
            ),
            and_(Perm.reader == True, Source.status >= PUBLISHED_THRESHOLD,
                 Target.status >= PUBLISHED_THRESHOLD,
                 Perm.account == request.account),
            and_(Perm.editor == True, Perm.account == request.account)))

    project = single_arg('project')
    if project:
        q = q.filter(Proj.slug == project)

    q = property_filters(Relation, q)

    if 'source' in request.args:
        q = q.filter(Rel.source_id == single_arg('source'))

    if 'target' in request.args:
        q = q.filter(Rel.target_id == single_arg('target'))

    if 'schema' in request.args:
        schemata = request.args.get('schema').split(',')
        alias = aliased(Schema)
        q = q.join(alias, Rel.schema)
        q = q.filter(alias.name.in_(schemata))

    return q
Esempio n. 9
0
def for_entities(q, Ent):
    """ Get all entities the current user has access to. Accepts project and
    additional filter parameters. """
    # NOTE: I'm passing in the query and entity alias so that this
    # function can be re-used from the facetting code to constrain
    # the results of the facet sub-query.
    Proj = aliased(Project)
    Perm = aliased(Permission)
    q = q.join(Proj, Ent.project).outerjoin(Perm, Proj.permissions)
    q = q.filter(Ent.same_as == None)

    q = q.filter(
        or_(
            and_(
                Proj.private == False,
                Ent.status >= PUBLISHED_THRESHOLD,
            ),
            and_(Perm.reader == True, Ent.status >= PUBLISHED_THRESHOLD,
                 Perm.account == request.account),
            and_(Perm.editor == True, Perm.account == request.account)))

    if 'project' in request.args:
        q = q.filter(Proj.slug == single_arg('project'))

    q = property_filters(Entity, q)

    if 'q' in request.args and single_arg('q'):
        EntProp = aliased(Property)
        q_text = '%%%s%%' % single_arg('q')
        q = q.join(EntProp)
        q = q.filter(EntProp.name == 'name')
        q = q.filter(EntProp.value_string.ilike(q_text))

    for schema in request.args.getlist('schema'):
        if not len(schema.strip()):
            continue
        alias = aliased(Schema)
        q = q.join(alias, Ent.schemata)
        q = q.filter(alias.name.in_(schema.split(',')))

    return q
Esempio n. 10
0
def property_filters(cls, q, alias):
    """ Parse the query arguments and apply any specified property
    filters to the given query ``q``. The property-holding object
    (a relation or entity) is given as ``cls``. """
    for key in request.args.keys():
        if not key.startswith(PROPERTY):
            continue
        prop = key[len(PROPERTY) :]

        only_active = True
        if prop.startswith(ALIASES):
            prop = prop[len(ALIASES) :]
            only_active = False

        value = single_arg(key)
        q = cls._filter_property(q, prop, value, only_active=only_active, alias=alias)
    return q
Esempio n. 11
0
def property_filters(cls, q):
    """ Parse the query arguments and apply any specified property
    filters to the given query ``q``. The property-holding object
    (a relation or entity) is given as ``cls``. """
    for key in request.args.keys():
        if not key.startswith(PROPERTY):
            continue
        prop = key[len(PROPERTY):]

        only_active = True
        if prop.startswith(ALIASES):
            prop = prop[len(ALIASES):]
            only_active = False

        attributes = Attribute.all_named(prop)
        value = single_arg(key)
        q = cls._filter_property(q, attributes, value, only_active=only_active)
    return q
Esempio n. 12
0
 def test_single_arg__allow_empty_duplicates(self):
     with self.app.test_request_context('/?a=b&a='):
         self.assertEqual(single_arg('a'), 'b')
Esempio n. 13
0
 def test_single_arg__bad_request(self):
     with self.app.test_request_context('/?a=b&a=c'):
         with self.assertRaises(BadRequest):
             single_arg('a')
Esempio n. 14
0
 def test_single_arg(self):
     with self.app.test_request_context('/?a=b'):
         self.assertEqual(single_arg('a'), 'b')
Esempio n. 15
0
 def test_single_arg__allow_empty_duplicates(self):
     with self.app.test_request_context('/?a=b&a='):
         self.assertEqual(single_arg('a'), 'b')
Esempio n. 16
0
 def test_single_arg__bad_request(self):
     with self.app.test_request_context('/?a=b&a=c'):
         with self.assertRaises(BadRequest):
             single_arg('a')
Esempio n. 17
0
 def test_single_arg(self):
     with self.app.test_request_context('/?a=b'):
         self.assertEqual(single_arg('a'), 'b')