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
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
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
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
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
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
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
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
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
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
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
def test_single_arg__allow_empty_duplicates(self): with self.app.test_request_context('/?a=b&a='): self.assertEqual(single_arg('a'), 'b')
def test_single_arg__bad_request(self): with self.app.test_request_context('/?a=b&a=c'): with self.assertRaises(BadRequest): single_arg('a')
def test_single_arg(self): with self.app.test_request_context('/?a=b'): self.assertEqual(single_arg('a'), 'b')