def model_query(model, session=None, project_only=False): """Query helper. :param model: base model to query :param project_only: if present and current context is user-type, then restrict query to match the tenant_id from current context. """ session = session or get_session() query = session.query(model) if project_only and 'tenant_id' not in model.__table__.columns: # model not having tenant_id as attribute LOG.debug("Query not filtered per tenant") return query if project_only: try: ctx = context.current() except RuntimeError: # Context is not available return query if context.is_user_context(ctx): query = query.filter_by(tenant_id=ctx.tenant_id) return query
def test_is_user_context_with_empty_context(self): ctx = context.ClimateContext() self.assertEqual(False, context.is_user_context(ctx)) self.assertEqual(False, context.is_user_context(None))
def test_is_user_context(self): ctx = context.ClimateContext(user_id="user", tenant_id="tenant") self.assertEqual(True, context.is_user_context(ctx)) ctx = ctx.elevated() self.assertEqual(False, context.is_user_context(ctx))