def owned(): """Get objects for which the user is owner.""" res = db.session.query( query_helpers.get_myobjects_query( types=[object_class.__name__], contact_id=exp["ids"][0], is_creator=is_creator(), ).alias().c.id) res = res.all() if res: return object_class.id.in_([obj.id for obj in res]) return sa.sql.false()
def search_get_owner_query(self, query, types=None, contact_id=None): """Prepare the search query based on the contact_id to return my objects. This method is used only for dashboard and returns objects the user is the owner. """ if not contact_id: return query union_query = query_helpers.get_myobjects_query( types=types, contact_id=contact_id, is_creator=is_creator()) return query.join( union_query, and_(union_query.c.id == MysqlRecordProperty.key, union_query.c.type == MysqlRecordProperty.type), )
def search_get_owner_query(query, types=None, contact_id=None): """Prepare the search query based on the contact_id to return my objects. This method is used only for dashboard and returns objects the user is the owner. """ if not contact_id: return query union_query = query_helpers.get_myobjects_query( types=types, contact_id=contact_id, is_creator=is_creator() ) return query.join( union_query, and_( union_query.c.id == MysqlRecordProperty.key, union_query.c.type == MysqlRecordProperty.type), )
def owned(ids): """Get objects for which the user is owner. Note: only the first id from the list of ids is used. Args: ids: the ids of owners. Returns: sqlalchemy.sql.elements.BinaryExpression if an object of `object_class` is owned by one of the given users. """ res = db.session.query( query_helpers.get_myobjects_query( types=[object_class.__name__], contact_id=ids[0], is_creator=is_creator(), ).alias().c.id) res = res.all() if res: return object_class.id.in_([obj.id for obj in res]) return sa.sql.false()
def _my_work_count(self, **kwargs): """Get object counts for my work page.""" id_ = kwargs.get("id") if id_ != login.get_current_user_id(): raise Forbidden() with benchmark("Make response"): aliased = my_objects.get_myobjects_query( types=self.MY_WORK_OBJECTS.keys(), contact_id=login.get_current_user_id(), is_creator=login.is_creator(), ) all_ = db.session.query( aliased.c.type, aliased.c.id, ) all_ids = collections.defaultdict(set) for type_, id_ in all_: all_ids[type_].add(id_) response_object = self.MY_WORK_OBJECTS.copy() for type_, ids in all_ids.items(): model = models.get_model(type_) # pylint: disable=protected-access # We must move the type permissions query to a proper utility function # but we will not do that for a patch release permission_filter = builder.QueryHelper._get_type_query( model, "read") if permission_filter is not None: count = model.query.filter( model.id.in_(ids), permission_filter, ).count() else: count = model.query.filter(model.id.in_(ids)).count() response_object[type_] = count return self.json_success_response(response_object, )
def owned(exp, object_class, target_class, query): """Get objects for which the user is owner. Note: only the first id from the list of ids is used. Args: ids: the ids of owners. Returns: sqlalchemy.sql.elements.BinaryExpression if an object of `object_class` is owned by one of the given users. """ res = db.session.query( query_helpers.get_myobjects_query( types=[object_class.__name__], contact_id=exp['ids'][0], is_creator=is_creator(), ).alias().c.id ) res = res.all() if res: return object_class.id.in_([obj.id for obj in res]) return sqlalchemy.sql.false()