Ejemplo n.º 1
0
 def test_insert(self):
     task = Task(name="unit_task")
     result = Result(task=task,
                     organization=Organization.get()[0],
                     input="something")
     result.save()
     self.assertEqual(result, result)
Ejemplo n.º 2
0
    def patch(self, id):
        """Update a Result."""
        result = db_Result.get(id)
        if not result:
            return {'msg': f'Result id={id} not found!'}, HTTPStatus.NOT_FOUND

        data = request.get_json()

        if result.organization_id != g.node.organization_id:
            log.warn(
                f"{g.node.name} tries to update a result that does not belong "
                f"to him. ({result.organization_id}/{g.node.organization_id})")
            return {"msg": "This is not your result to PATCH!"}, \
                HTTPStatus.UNAUTHORIZED

        if result.finished_at is not None:
            return {"msg": "Cannot update an already finished result!"}, \
                HTTPStatus.BAD_REQUEST

        # notify collaboration nodes/users that the task has an update
        self.socketio.emit("status_update", {'result_id': id},
                           namespace='/tasks', room='collaboration_'+\
                           str(result.task.collaboration.id))

        result.started_at = parse_datetime(data.get("started_at"),
                                           result.started_at)
        result.finished_at = parse_datetime(data.get("finished_at"))
        result.result = data.get("result")
        result.log = data.get("log")
        result.save()

        return result_schema.dump(result, many=False).data, HTTPStatus.OK
Ejemplo n.º 3
0
    def patch(self, id):
        """Update a Result."""
        data = request.get_json()
        result = db_Result.get(id)

        if result.organization_id != g.node.organization_id:
            log.warn(
                f"{g.node.name} tries to update a result that does not belong "
                f"to him. ({result.organization_id}/{g.node.organization_id})"
            )
            return {"msg": "This is not your result to PATCH!"}, HTTPStatus.UNAUTHORIZED

        if result.finished_at is not None:
            return {"msg": "Cannot update an already finished result!"}, HTTPStatus.BAD_REQUEST


        # notify collaboration nodes/users that the task has an update
        socketio.emit(
            "status_update",
            {'result_id': id},
            room='collaboration_'+str(result.task.collaboration.id),
            namespace='/tasks',
        )

        url = url_for('result_with_id', id=id)
        # log.debug(f'result [{url}] was updated.')

        result.started_at = parse_datetime(data.get("started_at"), result.started_at)
        result.finished_at = parse_datetime(data.get("finished_at"))
        result.result = data.get("result")
        result.log = data.get("log")

        result.save()

        return result
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
 def test_read(self):
     for result in Result.get():
         self.assertIsInstance(result, Result)
         self.assertIsNone(result.result)
         self.assertIsInstance(result.assigned_at, datetime.datetime)
         self.assertIsNone(result.started_at)
         self.assertIsNone(result.finished_at)
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
 def test_relations(self):
     result = Result.get()[0]
     self.assertIsInstance(result.organization, Organization)
     for user in result.organization.users:
         self.assertIsInstance(user, User)
     self.assertIsInstance(result.task, Task)
Ejemplo n.º 8
0
 def test_methods(self):
     for result in Result.get():
         self.assertFalse(result.complete)