Exemplo n.º 1
0
 def get_node(cls, info: ResolveInfo, id):
     db = get_session(info)
     row = db.query(cls._meta.db_model)\
         .filter(and_(
             cls._meta.db_model.is_deleted != True,
             cls._meta.db_model.id == id,
         ))\
         .first()
     return cls._meta.model.parse_obj(versioned_row_to_dict(row, row.current))
Exemplo n.º 2
0
    def resolve_all_users(
        root,
        info: ResolveInfo,

        # Search parameters
        archived: Optional[bool] = None,
        
        # Paging parameters
        page: Optional[int] = None,
        per_page: Optional[int] = None,

        # Currently unused
        before: Optional[str] = None,
        after: Optional[str] = None,
        first: Optional[int] = None,
        last: Optional[int] = None,
    ):
        enforcer = get_enforcer_from_request(info.context['request'])
        current_user = get_current_user_from_request(info.context['request'])
        if current_user is None:
            raise HTTPException(401, "Unauthorized")

        db = get_session(info)
        pagination_dict = crud_get_users(
            item_to_dict=lambda user: add_ids(versioned_row_to_dict(user, user.current), user_id=user.id),

            enforcer=enforcer,
            db=db,
            current_user=current_user,

            archived=archived,

            page=page,
            per_page=per_page,
        )

        return UserConnection(
            page=pagination_dict.get('page', None),
            pageCount=pagination_dict.get('pageCount', None),
            edges=[
                UserConnection.Edge(
                    node=UserModel.parse_obj(r),
                    cursor=f"{pagination_dict.get('page', 1)}.{i}",
                )
                for i, r
                in enumerate(pagination_dict['users'])
            ],
            page_info=relay.PageInfo(
                has_next_page=pagination_dict.get('page', 1) < pagination_dict.get('pageCount', 1),
                has_previous_page=pagination_dict.get('page', 1) > pagination_dict.get('pageCount', 1),
                start_cursor="1.0",
                end_cursor=f"1.{len(pagination_dict['users'])}" if pagination_dict.get('pageCount', None) is None else f"{pagination_dict['pageCount']}.{len(pagination_dict['users'])}",
            ),
        )
Exemplo n.º 3
0
    def resolve_owner(root, info):
        enforcer = get_enforcer_from_request(info.context['request'])
        current_user = get_current_user_from_request(info.context['request'])
        if current_user is None:
            raise HTTPException(401, "Unauthorized")

        db = get_session(info)
        return UserModel.parse_obj(
            crud_get_user(
                item_to_dict=lambda user: add_ids(versioned_row_to_dict(user, user.current), user_id=user.id),
                enforcer=enforcer,
                db=db,
                current_user=current_user,
                user_id=root.created_by,
            ),
        )
Exemplo n.º 4
0
    def resolve_samples(
        root,
        info: ResolveInfo,

        # Paging parameters
        page: Optional[int] = None,
        per_page: Optional[int] = None,
    ):
        enforcer = get_enforcer_from_request(info.context['request'])
        current_user = get_current_user_from_request(info.context['request'])
        if current_user is None:
            raise HTTPException(401, "Unauthorized")

        db = get_session(info)
        pagination_dict = crud_get_run_samples(
            item_to_dict=lambda sample: versioned_row_to_dict(sample, sample.current),

            enforcer=enforcer,
            db=db,
            current_user=current_user,

            run_id=root.run_id,

            page=page,
            per_page=per_page,
        )

        return SampleConnection(
            page=pagination_dict.get('page', None),
            pageCount=pagination_dict.get('pageCount', None),
            edges=[
                SampleConnection.Edge(
                    node=SampleResult.parse_obj(r),
                    cursor=f"{pagination_dict.get('page', 1)}.{i}",
                )
                for i, r
                in enumerate(pagination_dict['samples'])
            ],
            page_info=relay.PageInfo(
                has_next_page=pagination_dict.get('page', 1) < pagination_dict.get('pageCount', 1),
                has_previous_page=pagination_dict.get('page', 1) > pagination_dict.get('pageCount', 1),
                start_cursor="1.0",
                end_cursor=f"1.{len(pagination_dict['samples'])}" if pagination_dict.get('pageCount', None) is None else f"{pagination_dict['pageCount']}.{len(pagination_dict['samples'])}",
            ),
        )
Exemplo n.º 5
0
    def resolve_protocol(root, info):
        current_user = get_current_user_from_request(info.context['request'])
        if current_user is None:
            raise HTTPException(401, "Unauthorized")

        # Calculate which top level fields to remove.
        top_level_ignore = {'id', 'protocol_id', 'created_by', 'created_on', 'updated_by', 'updated_on'}

        select_args = []
        top_level = set()
        for result in graphql_ast_flatten_field(info.field_asts[0], info.fragments, info.return_type, info.schema):
            result_parts = result.split('.')
            if len(result_parts) > 1 and result_parts[1] not in top_level_ignore:
                top_level.add(result_parts[1])

        jsonb_fields = [
            'id',
            'protocol_id',
            'created_by',
            'created_on',
            'updated_by',
            'updated_on',
        ]
        select_args = [
            Protocol.id.label('id'),
            Protocol.id.label('protocol_id'),
            Protocol.created_by.label('created_by'),
            Protocol.created_on.label('created_on'),
            ProtocolVersion.updated_by.label('updated_by'),
            ProtocolVersion.updated_on.label('updated_on'),
        ]
        for field in top_level:
            jsonb_fields.append(field)
            select_args.append(ProtocolVersion.data[field].label(field))

        db = get_session(info)
        row_version = db.query(*select_args)\
            .select_from(ProtocolVersion)\
            .join(Run, and_(
                Run.id == root.id,
                Run.protocol_version_id == ProtocolVersion.id,
            ))\
            .join(Protocol, Protocol.id == ProtocolVersion.protocol_id)\
            .first()
        return ProtocolModel.parse_obj(row_version._asdict())
Exemplo n.º 6
0
    def resolve_user(root, info: ResolveInfo, id: str, version_id: Optional[int]):
        enforcer = get_enforcer_from_request(info.context['request'])
        current_user = get_current_user_from_request(info.context['request'])
        if current_user is None:
            raise HTTPException(401, "Unauthorized")

        db = get_session(info)
        model_dict = crud_get_user(
            item_to_dict=lambda user: add_ids(versioned_row_to_dict(user, user.current), user_id=user.id),

            enforcer=enforcer,
            db=db,
            current_user=current_user,

            user_id=id,
            version_id=version_id,
        )
        return UserModel.parse_obj(model_dict)
Exemplo n.º 7
0
    def resolve_protocol(root, info: ResolveInfo, id: int, version_id: Optional[int]):
        enforcer = get_enforcer_from_request(info.context['request'])
        current_user = get_current_user_from_request(info.context['request'])
        if current_user is None:
            raise HTTPException(401, "Unauthorized")

        db = get_session(info)
        model_dict = crud_get_protocol(
            item_to_dict=lambda protocol: add_ids(versioned_row_to_dict(protocol, protocol.current), protocol_id=protocol.id),

            enforcer=enforcer,
            db=db,
            current_user=current_user,

            protocol_id=id,
            version_id=version_id,
        )
        return ProtocolModel.parse_obj(model_dict)
Exemplo n.º 8
0
    def resolve_sample(root, info: ResolveInfo, sample_id: str, plate_id: str, run_version_id: int, protocol_version_id: int, version_id: Optional[int]):
        enforcer = get_enforcer_from_request(info.context['request'])
        current_user = get_current_user_from_request(info.context['request'])
        if current_user is None:
            raise HTTPException(401, "Unauthorized")

        db = get_session(info)
        model_dict = crud_get_sample(
            item_to_dict=lambda sample: versioned_row_to_dict(sample, sample.current),

            enforcer=enforcer,
            db=db,
            current_user=current_user,

            sample_id=sample_id,
            plate_id=plate_id,
            run_version_id=run_version_id,
            protocol_version_id=protocol_version_id,
            version_id=version_id,
        )
        return SampleResult.parse_obj(model_dict)
Exemplo n.º 9
0
    def resolve_run(
        root,
        info: ResolveInfo,
        
        id: int,
        version_id: int,
    ):
        enforcer = get_enforcer_from_request(info.context['request'])
        current_user = get_current_user_from_request(info.context['request'])
        if current_user is None:
            raise HTTPException(401, "Unauthorized")

        db = get_session(info)
        model_dict = crud_get_run(
            item_to_dict=lambda run: add_ids(versioned_row_to_dict(run, run.current), run_id=run.id),

            enforcer=enforcer,
            db=db,
            current_user=current_user,

            run_id=id,
            version_id=version_id,
        )
        return RunModel.parse_obj(model_dict)