def next_run_id(cls): session = Database().Session max_run_id = session.query(sql.func.max(cls.run_id)).scalar() if max_run_id: return max_run_id + 1 else: return 1
def get(self, id=None): if id: t = db_Result.get(id) else: session = Database().Session q = session.query(db_Result) if request.args.get('state') == 'open': q = q.filter(db_Result.finished_at == None) # q = q.join(db_Result) if request.args.get('task_id'): q = q.filter_by(task_id=request.args.get('task_id')) q = q.join(Organization).join(Node).join(Task, db_Result.task).join(Collaboration) if request.args.get('node_id'): q = q.filter(db.Node.id==request.args.get('node_id'))\ .filter(db.Collaboration.id==db.Node.collaboration_id) t = q.all() if request.args.get('include') == 'task': s = result_inc_schema else: s = result_schema return s.dump(t, many=not bool(id)).data, HTTPStatus.OK
def get(self, id=None): # obtain requisters organization if g.user: auth_org = g.user.organization elif g.node: auth_org = g.node.organization else: # g.container auth_org = Organization.get(g.container['organization_id']) if id: result = db_Result.get(id) if not result: return {'msg': f'Result id={id} not found!'}, \ HTTPStatus.NOT_FOUND if not self.r.v_glo.can(): c_orgs = result.task.collaboration.organizations if not (self.r.v_org.can() and auth_org in c_orgs): return {'msg': 'You lack the permission to do that!'}, \ HTTPStatus.UNAUTHORIZED else: session = Database().Session q = session.query(db_Result) if request.args.get('state') == 'open': q = q.filter(db_Result.finished_at == None) # q = q.join(db_Result) if request.args.get('task_id'): q = q.filter_by(task_id=request.args.get('task_id')) q = q.join(Organization).join(Node).join(Task, db_Result.task)\ .join(Collaboration) if request.args.get('node_id'): q = q.filter(db.Node.id == request.args.get('node_id'))\ .filter(db.Collaboration.id == db.Node.collaboration_id) result = q.all() # filter results based on permissions if not self.r.v_glo.can(): if self.r.v_org.can(): filtered_result = [] for res in result: if res.task.collaboration in auth_org.collaborations: filtered_result.append(res) result = filtered_result else: return {'msg': 'You lack the permission to do that!'}, \ HTTPStatus.UNAUTHORIZED if request.args.get('include') == 'task': s = result_inc_schema else: s = result_schema return s.dump(result, many=not id).data, HTTPStatus.OK
def get_by_(cls, name, scope, operation): session = Database().Session try: return session.query(cls).filter_by( name=name, operation=operation, scope=scope ).first() except NoResultFound: return None
def get_by_api_key(cls, api_key): """returns Node based on the provided API key. Returns None if no Node is associated with api_key. """ session = Database().Session try: return session.query(cls).filter_by(api_key=api_key).one() except NoResultFound: return None
def tasks(self, collaboration: Collaboration, organization: Organization): """A node belongs to a single collaboration. Therefore the node only executes task for the organization and collaboration is belongs to. Tasks can be assigned to many nodes but are only assigned to as single collaboration. """ # find node of the organization that is within the collaboration node = collaboration.get_node_from_organization(organization) session = Database().Session tasks = session.query(Task)\ .join(Collaboration)\ .join(Node)\ .filter_by(collabotation_id=collaboration.id)\ .filter(Node == node.id)\ .all() return tasks
def get_node(self, result: Result): """A node is assigned by an organization to an collaboration. Tasks, that is responsible for a set of Results, are assigned to these collaborations. A Result is therefore directly related to a single node by it's organization and collaboration. Organization can be directly derived from the Result while the collaboration can be found from the originating task. """ session = Database().Session node = session.query(Node)\ .join(Collaboration)\ .join(Task)\ .join(Organization)\ .join(Result)\ .filter_by(result_id=result.id)\ .filter(Task.organization_id == Node.organization_id)\ .filter(Task.collaboration_id == Node.collaboration_id)\ .one() return node
def exists(cls, field, value): session = Database().Session return session.query(exists().where(getattr(cls, field) == value))\ .scalar()
def username_exists(cls, username): session = Database().Session return session.query(exists().where(cls.username == username)).scalar()
def get_user_list(cls, filters=None): session = Database().Session return session.query(cls).all()
def get_by_email(cls, email): session = Database().Session return session.query(cls).filter_by(email=email).one()
def get_by_username(cls, username): session = Database().Session return session.query(cls).filter_by(username=username).one()
def exists(cls, organization_id, collaboration_id): session = Database().Session return session.query(cls).filter_by( organization_id=organization_id, collaboration_id=collaboration_id ).scalar()
def get_by_name(cls, name): session = Database().Session try: return session.query(cls).filter_by(name=name).first() except NoResultFound: return None