def list_for_user_get_page(self, user_id, marker, limit, session=None): if not session: session = get_session() user = api.USER.get(user_id) if hasattr(api.USER, 'uid_to_id'): backend_user_id = api.USER.uid_to_id(user_id) else: backend_user_id = user_id ura = aliased(models.UserRoleAssociation) tenant = aliased(models.Tenant) q1 = session.query(tenant).join((ura, ura.tenant_id == tenant.id)).\ filter(ura.user_id == backend_user_id) if 'tenant_id' in user: if hasattr(api.TENANT, 'uid_to_id'): backend_tenant_id = api.TENANT.uid_to_id(user.tenant_id) else: backend_tenant_id = user.tenant_id q2 = session.query(tenant).filter(tenant.id == backend_tenant_id) q3 = q1.union(q2) else: q3 = q1 if marker: results = q3.filter("tenant.id>:marker").params( marker='%s' % marker).order_by( tenant.id.desc()).limit(int(limit)).all() else: results = q3.order_by(tenant.id.desc()).limit(int(limit)).all() return TenantAPI.to_model_list(results)
def list_for_user_get_page(self, user_id, marker, limit, session=None): if not session: session = get_session() user = api.USER.get(user_id) if hasattr(api.USER, 'uid_to_id'): backend_user_id = api.USER.uid_to_id(user_id) else: backend_user_id = user_id ura = aliased(models.UserRoleAssociation) tenant = aliased(models.Tenant) q1 = session.query(tenant).join((ura, ura.tenant_id == tenant.id)).\ filter(ura.user_id == backend_user_id) if 'tenant_id' in user: if hasattr(api.TENANT, 'uid_to_id'): backend_tenant_id = api.TENANT.uid_to_id(user.tenant_id) else: backend_tenant_id = user.tenant_id q2 = session.query(tenant).filter(tenant.id == backend_tenant_id) q3 = q1.union(q2) else: q3 = q1 if marker: results = q3.filter("tenant.id>:marker").params(\ marker='%s' % marker).order_by(\ tenant.id.desc()).limit(limit).all() else: results = q3.order_by(tenant.id.desc()).limit(limit).all() return TenantAPI.to_model_list(results)
def list_for_user_get_page_markers(self, user_id, marker, limit, session=None): if not session: session = get_session() user = api.USER.get(user_id) if hasattr(api.USER, 'uid_to_id'): backend_user_id = api.USER.uid_to_id(user_id) else: backend_user_id = user_id ura = aliased(models.UserRoleAssociation) tenant = aliased(models.Tenant) q1 = session.query(tenant).join((ura, ura.tenant_id == tenant.id)).\ filter(ura.user_id == backend_user_id) if 'tenant_id' in user: if hasattr(api.TENANT, 'uid_to_id'): backend_tenant_id = api.TENANT.uid_to_id(user.tenant_id) else: backend_tenant_id = user.tenant_id q2 = session.query(tenant).filter(tenant.id == backend_tenant_id) q3 = q1.union(q2) else: q3 = q1 first = q3.order_by(tenant.id).first() last = q3.order_by(tenant.id.desc()).first() if first is None: return (None, None) if marker is None: marker = first.id next_page = q3.filter(tenant.id > marker).order_by(tenant.id).limit( int(limit)).all() prev_page = q3.filter(tenant.id > marker).order_by( tenant.id.desc()).limit(int(limit)).all() if len(next_page) == 0: next_page = last else: for t in next_page: next_page = t if len(prev_page) == 0: prev_page = first else: for t in prev_page: prev_page = t if prev_page.id == marker: prev_page = None else: prev_page = prev_page.id if next_page.id == last.id: next_page = None else: next_page = next_page.id return (prev_page, next_page)
def user_groups_get_all(self, user_id, session=None): if not session: session = get_session() uga = aliased(models.UserGroupAssociation) group = aliased(models.Group) return session.query(group, uga).\ join((uga, uga.group_id == group.id)).\ filter(uga.user_id == user_id).order_by( group.id).all()
def list_for_user_get_page_markers(self, user_id, marker, limit, session=None): if not session: session = get_session() user = api.USER.get(user_id) if hasattr(api.USER, 'uid_to_id'): backend_user_id = api.USER.uid_to_id(user_id) else: backend_user_id = user_id ura = aliased(models.UserRoleAssociation) tenant = aliased(models.Tenant) q1 = session.query(tenant).join((ura, ura.tenant_id == tenant.id)).\ filter(ura.user_id == backend_user_id) if 'tenant_id' in user: if hasattr(api.TENANT, 'uid_to_id'): backend_tenant_id = api.TENANT.uid_to_id(user.tenant_id) else: backend_tenant_id = user.tenant_id q2 = session.query(tenant).filter(tenant.id == backend_tenant_id) q3 = q1.union(q2) else: q3 = q1 first = q3.order_by(tenant.id).first() last = q3.order_by(tenant.id.desc()).first() if first is None: return (None, None) if marker is None: marker = first.id next_page = q3.filter(tenant.id > marker).order_by( tenant.id).limit(int(limit)).all() prev_page = q3.filter(tenant.id > marker).order_by( tenant.id.desc()).limit(int(limit)).all() if len(next_page) == 0: next_page = last else: for t in next_page: next_page = t if len(prev_page) == 0: prev_page = first else: for t in prev_page: prev_page = t if prev_page.id == marker: prev_page = None else: prev_page = prev_page.id if next_page.id == last.id: next_page = None else: next_page = next_page.id return (prev_page, next_page)
def get_all_endpoints(self, tenant_id, session=None): if not session: session = get_session() ep = aliased(models.Endpoints) endpointTemplates = aliased(models.EndpointTemplates) q1 = session.query(endpointTemplates).join((ep, ep.endpoint_template_id == endpointTemplates.id)).\ filter(ep.tenant_id == tenant_id) q2 = session.query(endpointTemplates).\ filter(endpointTemplates.is_global == 1) q3 = q1.union(q2) return q3.all()
def get_by_user_get_page_markers(self, user_id, marker, limit, session=None): if not session: session = get_session() uga = aliased(models.UserGroupAssociation) group = aliased(models.Group) first, _firstassoc = session.query(group, uga).\ join((uga, uga.group_id == group.id)).\ filter(uga.user_id == user_id).\ order_by(group.id).first() last, _lastassoc = session.query(group, uga).\ join((uga, uga.group_id == group.id)).\ filter(uga.user_id == user_id).\ order_by(group.id.desc()).first() if first is None: return (None, None) if marker is None: marker = first.id next = session.query(group, uga).join( (uga, uga.group_id == group.id)).\ filter(uga.user_id == user_id).\ filter("id>=:marker").params( marker='%s' % marker).order_by( group.id).limit(int(limit)).all() prev = session.query(group, uga).join( (uga, uga.group_id == group.id)).\ filter(uga.user_id == user_id).\ filter("id < :marker").params( marker='%s' % marker).order_by( group.id).limit(int(limit) + 1).all() next_len = len(next) prev_len = len(prev) if next_len == 0: next = last else: for t, _a in next: next = t if prev_len == 0: prev = first else: for t, _a in prev: prev = t if first.id == marker: prev = None else: prev = prev.id if marker == last.id: next = None else: next = next.id return (prev, next)
def get_all_endpoints(self, tenant_id, session=None): if not session: session = get_session() endpointTemplates = aliased(models.EndpointTemplates) q = session.query(endpointTemplates).\ filter(endpointTemplates.is_global == 1) if tenant_id: ep = aliased(models.Endpoints) q1 = session.query(endpointTemplates).join((ep, ep.endpoint_template_id == endpointTemplates.id)).\ filter(ep.tenant_id == tenant_id) q = q.union(q1) return q.all()
def get_all_endpoints(self, tenant_id, session=None): if not session: session = get_session() endpointTemplates = aliased(models.EndpointTemplates) q = session.query(endpointTemplates).filter(endpointTemplates.is_global == 1) if tenant_id: ep = aliased(models.Endpoints) q1 = ( session.query(endpointTemplates) .join((ep, ep.endpoint_template_id == endpointTemplates.id)) .filter(ep.tenant_id == tenant_id) ) q = q.union(q1) return q.all()
def tenants_for_user_get_page(self, user, marker, limit, session=None): if not session: session = get_session() ura = aliased(models.UserRoleAssociation) tenant = aliased(models.Tenant) q1 = session.query(tenant).join((ura, ura.tenant_id == tenant.id)).\ filter(ura.user_id == user.id) q2 = session.query(tenant).filter(tenant.id == user.tenant_id) q3 = q1.union(q2) if marker: return q3.filter("tenant.id>:marker").params(\ marker='%s' % marker).order_by(\ tenant.id.desc()).limit(limit).all() else: return q3.order_by(tenant.id.desc()).limit(limit).all()
def users_get_by_tenant_get_page(self, tenant_id, marker, limit, session=None): # This is broken. If a user has more than one role per project # shit hits the fan because we're limiting the wrong model. # Also the user lookup is nasty and potentially injectiable. if not session: session = get_session() user = aliased(models.User) if marker: users = session.query(user).\ filter_by(tenant_id = tenant_id).\ filter("id > :marker").params(\ marker='%s' % marker).order_by(user.id).\ limit(limit).\ all() else: users = session.query(user).\ filter_by(tenant_id = tenant_id).\ order_by(user.id).\ limit(limit).\ all() for usr in users: usr.tenant_roles = set() for role in usr.roles: if role.tenant_id == tenant_id: usr.tenant_roles.add(role.role_id) return users
def users_get_by_tenant_get_page(self, tenant_id, role_id, marker, limit, session=None): # This is broken. If a user has more than one role per project # shit hits the fan because we're limiting the wrong model. # Also the user lookup is nasty and potentially injectiable. if not session: session = get_session() if hasattr(api.TENANT, "uid_to_id"): tenant_id = api.TENANT.uid_to_id(tenant_id) user = aliased(models.UserRoleAssociation) query = session.query(user).filter("tenant_id = :tenant_id").params(tenant_id="%s" % tenant_id) if role_id: query = query.filter(user.role_id == role_id) if marker: rv = query.filter("id>=:marker").params(marker="%s" % marker).order_by("id").limit(int(limit)).all() else: rv = query.order_by("id").limit(int(limit)).all() user_ids = set([str(assoc.user_id) for assoc in rv]) users = session.query(models.User).filter("id in ('%s')" % "','".join(user_ids)).all() for usr in users: usr.tenant_roles = set() for role in usr.roles: if role.tenant_id == tenant_id: usr.tenant_roles.add(role.role_id) return UserAPI.to_model_list(users)
def users_get_by_tenant_get_page(self, tenant_id, marker, limit, session=None): # This is broken. If a user has more than one role per project # shit hits the fan because we're limiting the wrong model. # Also the user lookup is nasty and potentially injectiable. if not session: session = get_session() user = aliased(models.UserRoleAssociation) if marker: rv = session.query(user).\ filter("tenant_id = :tenant_id").\ params(tenant_id='%s' % tenant_id).\ filter("id>=:marker").\ params(marker='%s' % marker).\ order_by("id").\ limit(limit).\ all() else: rv = session.query(user).\ filter("tenant_id = :tenant_id").\ params(tenant_id='%s' % tenant_id).\ order_by("id").\ limit(limit).\ all() user_ids = set([assoc.user_id for assoc in rv]) users = session.query(models.User).\ filter("id in ('%s')" % "','".join(user_ids)).\ all() for usr in users: usr.tenant_roles = set() for role in usr.roles: if role.tenant_id == tenant_id: usr.tenant_roles.add(role.role_id) return users
def users_get_by_tenant_get_page(self, tenant_id, marker, limit, session=None): # This is broken. If a user has more than one role per project # shit hits the fan because we're limiting the wrong model. # Also the user lookup is nasty and potentially injectiable. if not session: session = get_session() user = aliased(models.UserRoleAssociation) if marker: rv = session.query(user).\ filter("tenant_id = :tenant_id").\ params(tenant_id='%s' % tenant_id).\ filter("id>=:marker").\ params(marker='%s' % marker).\ order_by("id").\ limit(limit).\ all() else: rv = session.query(user).\ filter("tenant_id = :tenant_id").\ params(tenant_id='%s' % tenant_id).\ order_by("id").\ limit(limit).\ all() user_ids = set([str(assoc.user_id) for assoc in rv]) users = session.query(models.User).\ filter("id in ('%s')" % "','".join(user_ids)).\ all() for usr in users: usr.tenant_roles = set() for role in usr.roles: if role.tenant_id == tenant_id: usr.tenant_roles.add(role.role_id) return users
def users_tenant_group_get_page(self, group_id, marker, limit, session=None): if not session: session = get_session() uga = aliased(models.UserGroupAssociation) user = aliased(models.User) if marker: return session.query(user, uga).join(\ (uga, uga.user_id == user.id)).\ filter(uga.group_id == group_id).\ filter("id>=:marker").params(\ marker='%s' % marker).order_by(\ user.id).limit(limit).all() else: return session.query(user, uga).\ join((uga, uga.user_id == user.id)).\ filter(uga.group_id == group_id).order_by(\ user.id).limit(limit).all()
def get_all_endpoints(self, tenant_id, session=None): if not session: session = get_session() if hasattr(api.TENANT, 'uid_to_id'): tenant_id = self.uid_to_id(tenant_id) endpoint_templates = aliased(models.EndpointTemplates) q = session.query(endpoint_templates).\ filter(endpoint_templates.is_global == True) if tenant_id: ep = aliased(models.Endpoints) q1 = session.query(endpoint_templates).join((ep, ep.endpoint_template_id == endpoint_templates.id)).\ filter(ep.tenant_id == tenant_id) q = q.union(q1) return q.all()
def get_by_user_get_page(self, user_id, marker, limit, session=None): if not session: session = get_session() uga = aliased(models.UserGroupAssociation) group = aliased(models.Group) if marker: return session.query(group, uga).join(\ (uga, uga.group_id == group.id)).\ filter(uga.user_id == user_id).\ filter("id>=:marker").params( marker='%s' % marker).order_by( group.id).limit(limit).all() else: return session.query(group, uga).\ join((uga, uga.group_id == group.id)).\ filter(uga.user_id == user_id).order_by( group.id).limit(limit).all()
def tenants_for_user_get_page_markers(self, user, marker, limit, session=None): if not session: session = get_session() ura = aliased(models.UserRoleAssociation) tenant = aliased(models.Tenant) q1 = session.query(tenant).join((ura, ura.tenant_id == tenant.id)).\ filter(ura.user_id == user.id) q2 = session.query(tenant).filter(tenant.id == user.tenant_id) q3 = q1.union(q2) first = q3.order_by(\ tenant.id).first() last = q3.order_by(\ tenant.id.desc()).first() if first is None: return (None, None) if marker is None: marker = first.id next = q3.filter(tenant.id > marker).order_by(\ tenant.id).limit(limit).all() prev = q3.filter(tenant.id > marker).order_by(\ tenant.id.desc()).limit(int(limit)).all() if len(next) == 0: next = last else: for t in next: next = t if len(prev) == 0: prev = first else: for t in prev: prev = t if prev.id == marker: prev = None else: prev = prev.id if next.id == last.id: next = None else: next = next.id return (prev, next)
def tenants_for_user_get_page_markers(self, user, marker, limit, session=None): if not session: session = get_session() ura = aliased(models.UserRoleAssociation) tenant = aliased(models.Tenant) q1 = session.query(tenant).join((ura, ura.tenant_id == tenant.id)).\ filter(ura.user_id == user.id) q2 = session.query(tenant).filter(tenant.id == user.tenant_id) q3 = q1.union(q2) first = q3.order_by(\ tenant.id).first() last = q3.order_by(\ tenant.id.desc()).first() if first is None: return (None, None) if marker is None: marker = first.id next_page = q3.filter(tenant.id > marker).order_by(\ tenant.id).limit(limit).all() prev_page = q3.filter(tenant.id > marker).order_by(\ tenant.id.desc()).limit(int(limit)).all() if len(next_page) == 0: next_page = last else: for t in next_page: next_page = t if len(prev_page) == 0: prev_page = first else: for t in prev_page: prev_page = t if prev_page.id == marker: prev_page = None else: prev_page = prev_page.id if next_page.id == last.id: next_page = None else: next_page = next_page.id return (prev_page, next_page)
def users_tenant_group_get_page_markers(self, group_id, marker, limit, session=None): if not session: session = get_session() uga = aliased(models.UserGroupAssociation) user = aliased(models.User) first = session.query(models.User).order_by(\ models.User.id).first() last = session.query(models.User).order_by(\ models.User.id.desc()).first() if first is None: return (None, None) if marker is None: marker = first.id next = session.query(user).join( (uga, uga.user_id == user.id)).\ filter(uga.group_id == group_id).\ filter("id > :marker").params(\ marker='%s' % marker).order_by(\ user.id).limit(limit).all() prev = session.query(user).join(\ (uga, uga.user_id == user.id)).\ filter(uga.group_id == group_id).\ filter("id < :marker").params(\ marker='%s' % marker).order_by(\ user.id.desc()).limit(int(limit)).all() if len(next) == 0: next = last else: for t in next: next = t if len(prev) == 0: prev = first else: for t in prev: prev = t if prev.id == marker: prev = None else: prev = prev.id if next.id == last.id: next = None else: next = next.id return (prev, next)
def endpoint_get_by_tenant_get_page_markers(self, tenant_id, marker, limit, session=None): if not session: session = get_session() if hasattr(api.TENANT, 'uid_to_id'): tenant_id = api.TENANT.uid_to_id(tenant_id) tba = aliased(models.Endpoints) first = session.query(tba).\ filter(tba.tenant_id == tenant_id).\ order_by(tba.id).first() last = session.query(tba).\ filter(tba.tenant_id == tenant_id).\ order_by(tba.id.desc()).first() if first is None: return (None, None) if marker is None: marker = first.id next_page = session.query(tba).\ filter(tba.tenant_id == tenant_id).\ filter("id>=:marker").params( marker='%s' % marker).order_by( tba.id).limit(int(limit)).all() prev_page = session.query(tba).\ filter(tba.tenant_id == tenant_id).\ filter("id < :marker").params( marker='%s' % marker).order_by( tba.id).limit(int(limit) + 1).all() next_len = len(next_page) prev_len = len(prev_page) if next_len == 0: next_page = last else: for t in next_page: next_page = t if prev_len == 0: prev_page = first else: for t in prev_page: prev_page = t if first.id == marker: prev_page = None else: prev_page = prev_page.id if marker == last.id: next_page = None else: next_page = next_page.id return (prev_page, next_page)
def users_get_page(self, marker, limit, session=None): if not session: session = get_session() user = aliased(models.User) if marker: return session.query(user).\ filter("id>=:marker").params( marker='%s' % marker).order_by( "id").limit(limit).all() else: return session.query(user).\ order_by("id").limit(limit).all()
def users_get_by_tenant_get_page_markers(self, tenant_id, \ role_id, marker, limit, session=None): if not session: session = get_session() if hasattr(api.TENANT, 'uid_to_id'): tenant_id = api.TENANT.uid_to_id(tenant_id) user = aliased(models.UserRoleAssociation) query = session.query(user).\ filter(user.tenant_id == tenant_id) if role_id: query = query.filter( user.role_id == role_id) first = query.\ order_by(user.id).first() last = query.\ order_by(user.id.desc()).first() if first is None: return (None, None) if marker is None: marker = first.id next_page = query.\ filter("id > :marker").params( marker='%s' % marker).order_by(user.id).\ limit(int(limit)).all() prev_page = query.\ filter("id < :marker").params( marker='%s' % marker).order_by( user.id.desc()).limit(int(limit)).all() next_len = len(next_page) prev_len = len(prev_page) if next_len == 0: next_page = last else: for t in next_page: next_page = t if prev_len == 0: prev_page = first else: for t in prev_page: prev_page = t if first.id == marker: prev_page = None else: prev_page = prev_page.id if marker == last.id: next_page = None else: next_page = next_page.id return (prev_page, next_page)
def users_get_by_tenant_get_page_markers(self, tenant_id, \ role_id, marker, limit, session=None): if not session: session = get_session() if hasattr(api.TENANT, 'uid_to_id'): tenant_id = api.TENANT.uid_to_id(tenant_id) user = aliased(models.UserRoleAssociation) query = session.query(user).\ filter(user.tenant_id == tenant_id) if role_id: query = query.filter(user.role_id == role_id) first = query.\ order_by(user.id).first() last = query.\ order_by(user.id.desc()).first() if first is None: return (None, None) if marker is None: marker = first.id next_page = query.\ filter("id > :marker").params(\ marker='%s' % marker).order_by(user.id).\ limit(int(limit)).all() prev_page = query.\ filter("id < :marker").params( marker='%s' % marker).order_by( user.id.desc()).limit(int(limit)).all() next_len = len(next_page) prev_len = len(prev_page) if next_len == 0: next_page = last else: for t in next_page: next_page = t if prev_len == 0: prev_page = first else: for t in prev_page: prev_page = t if first.id == marker: prev_page = None else: prev_page = prev_page.id if marker == last.id: next_page = None else: next_page = next_page.id return (prev_page, next_page)
def endpoint_get_by_tenant_get_page_markers(self, tenant_id, marker, limit, session=None): if not session: session = get_session() tba = aliased(models.Endpoints) first = session.query(tba).filter(tba.tenant_id == tenant_id).order_by(tba.id).first() last = session.query(tba).filter(tba.tenant_id == tenant_id).order_by(tba.id.desc()).first() if first is None: return (None, None) if marker is None: marker = first.id next = ( session.query(tba) .filter(tba.tenant_id == tenant_id) .filter("id>=:marker") .params(marker="%s" % marker) .order_by(tba.id) .limit(int(limit)) .all() ) prev = ( session.query(tba) .filter(tba.tenant_id == tenant_id) .filter("id < :marker") .params(marker="%s" % marker) .order_by(tba.id) .limit(int(limit) + 1) .all() ) next_len = len(next) prev_len = len(prev) if next_len == 0: next = last else: for t in next: next = t if prev_len == 0: prev = first else: for t in prev: prev = t if first.id == marker: prev = None else: prev = prev.id if marker == last.id: next = None else: next = next.id return (prev, next)
def users_get_page_markers(self, marker, limit, session=None): if not session: session = get_session() user = aliased(models.User) first = session.query(user).order_by(user.id).first() last = session.query(user).order_by(user.id.desc()).first() if first is None: return (None, None) if marker is None: marker = first.id next_page = ( session.query(user) .filter("id > :marker") .params(marker="%s" % marker) .order_by(user.id) .limit(int(limit)) .all() ) prev_page = ( session.query(user) .filter("id < :marker") .params(marker="%s" % marker) .order_by(user.id.desc()) .limit(int(limit)) .all() ) next_len = len(next_page) prev_len = len(prev_page) if next_len == 0: next_page = last else: for t in next_page: next_page = t if prev_len == 0: prev_page = first else: for t in prev_page: prev_page = t if first.id == marker: prev_page = None else: prev_page = prev_page.id if marker == last.id: next_page = None else: next_page = next_page.id return (prev_page, next_page)
def users_get_by_tenant_get_page_markers(self, tenant_id, marker, limit, \ session=None): if not session: session = get_session() user = aliased(models.UserRoleAssociation) first = session.query(user).\ filter(user.tenant_id == tenant_id).\ order_by(user.id).first() last = session.query(user).\ filter(user.tenant_id == tenant_id).\ order_by(user.id.desc()).first() if first is None: return (None, None) if marker is None: marker = first.id next = session.query(user).\ filter(user.tenant_id == tenant_id).\ filter("id > :marker").params(\ marker='%s' % marker).order_by(user.id).\ limit(int(limit)).all() prev = session.query(user).\ filter(user.tenant_id == tenant_id).\ filter("id < :marker").params( marker='%s' % marker).order_by( user.id.desc()).limit(int(limit)).all() next_len = len(next) prev_len = len(prev) if next_len == 0: next = last else: for t in next: next = t if prev_len == 0: prev = first else: for t in prev: prev = t if first.id == marker: prev = None else: prev = prev.id if marker == last.id: next = None else: next = next.id return (prev, next)
def users_get_page(self, marker, limit, session=None): if not session: session = get_session() user = aliased(models.User) if marker: results = ( session.query(user) .filter("id>=:marker") .params(marker="%s" % marker) .order_by("id") .limit(int(limit)) .all() ) else: results = session.query(user).order_by("id").limit(int(limit)).all() return UserAPI.to_model_list(results)
def users_get_page_markers(self, marker, limit, session=None): if not session: session = get_session() user = aliased(models.User) first = session.query(user).\ order_by(user.id).first() last = session.query(user).\ order_by(user.id.desc()).first() if first is None: return (None, None) if marker is None: marker = first.id next_page = session.query(user).\ filter("id > :marker").params(\ marker='%s' % marker).order_by(user.id).\ limit(int(limit)).all() prev_page = session.query(user).\ filter("id < :marker").params( marker='%s' % marker).order_by( user.id.desc()).limit(int(limit)).all() next_len = len(next_page) prev_len = len(prev_page) if next_len == 0: next_page = last else: for t in next_page: next_page = t if prev_len == 0: prev_page = first else: for t in prev_page: prev_page = t if first.id == marker: prev_page = None else: prev_page = prev_page.id if marker == last.id: next_page = None else: next_page = next_page.id return (prev_page, next_page)