示例#1
0
    def mutate(
        args: Dict,
        info: graphql.execution.base.ResolveInfo,
        user_study: InputUserStudy,
    ):
        """ Upserts a `ModelUserStudy` record based on the `user_study` input.

        Returns
            MutationUserStudyUpsert: The result of the mutation.
        """

        # Cleanup the Auth0 user ID.
        auth0_user_id = clean_auth0_user_id(auth0_user_id=str(
            user_study.auth0_user_id), )

        # Retrieve the NCT ID of the defined study.
        nct_id = str(user_study.nct_id)

        # Check that the requesting user is authorized to upsert the user with
        # the given Auth0 ID.
        check_auth(info=info, auth0_user_id=auth0_user_id)

        # Retrieve the session out of the context as the `get_query` method
        # automatically selects the model.
        session = info.context.get("session")  # type: sqlalchemy.orm.Session

        # Retrieve the `ModelUser` record object.
        user = get_user(session=session, auth0_user_id=auth0_user_id)

        # Raise an exception if the requested user could not be found.
        if not user:
            msg = "User with Auth0 ID '{}' could not be found."
            msg_fmt = msg.format(auth0_user_id)
            raise graphql.GraphQLError(message=msg_fmt)

        # Retrieve the `ModelStudy` record object.
        study = get_study(session=session, nct_id=nct_id)

        # Raise an exception if the requested study could not be found.
        if not study:
            msg = "Study with NCT ID '{}' could not be found."
            msg_fmt = msg.format(nct_id)
            raise graphql.GraphQLError(message=msg_fmt)

        # Upsert the `ModelUserStudy` record.
        statement = insert(ModelUserStudy,
                           values={
                               "user_id": user.user_id,
                               "study_id": study.study_id,
                           }).on_conflict_do_nothing()  # type: Insert

        # Execute the upsert.
        result = session.execute(statement)  # type: ResultProxy

        # Retrieve the newly upserted `ModelUser` record object.
        obj = get_user(session=session, auth0_user_id=auth0_user_id)

        session.commit()

        return MutationUserStudyUpsert(user=obj)
示例#2
0
    def mutate(
        args: Dict,
        info: graphql.execution.base.ResolveInfo,
        auth0_user_id: str,
    ):
        """ Deletes a `ModelUser` record based on the `user` input.

        Returns
            MutationUserDelete: The result of the mutation.
        """

        # Cleanup the Auth0 user ID.
        auth0_user_id = clean_auth0_user_id(auth0_user_id=str(auth0_user_id))

        # Check that the requesting user is authorized to upsert the user with
        # the given Auth0 ID.
        check_auth(info=info, auth0_user_id=auth0_user_id)

        # Retrieve the session out of the context as the `get_query` method
        # automatically selects the model.
        session = info.context.get("session")  # type: sqlalchemy.orm.Session

        # Retrieve the `ModelUser` record object.
        query = session.query(ModelUser)
        query = query.filter(ModelUser.auth0_user_id == auth0_user_id)
        obj = query.one_or_none()  # type: ModelUser

        # Raise an exception if the requested user could not be found.
        if not obj:
            msg = "User with Auth0 ID '{}' could not be found."
            msg_fmt = msg.format(auth0_user_id)
            raise graphql.GraphQLError(message=msg_fmt)

        # Delete the `ModelSearch` records related to this user as well as all
        # `ModelUserSearch` and `ModelSearchDescriptor` records related to
        # those searches.
        delete_user_searches(session=session, user_id=obj.user_id)

        # Delete all `ModelUserStudy` records related to this user.
        query = session.query(ModelUserStudy)
        query = query.filter(ModelUserStudy.user_id == obj.user_id)
        query.delete()

        # Delete all `ModelUserCitation` records related to this user.
        query = session.query(ModelUserCitation)
        query = query.filter(ModelUserCitation.user_id == obj.user_id)
        query.delete()

        # Delete the `ModelUser` record.
        session.delete(obj)
        session.commit()

        return MutationUserDelete(user=obj)
示例#3
0
    def resolve_by_search_uuid(
        args: dict,
        info: graphene.ResolveInfo,
        auth0_user_id: str,
        search_uuid: str,
    ) -> ModelSearch:
        """ Retrieves a `ModelUser` record object through its Auth0 IDs.

        Args:
            args (dict): The resolver arguments.
            info (graphene.ResolveInfo): The resolver info.
            auth0_user_id (str): The Auth0 ID of the user to which the search
                belongs.
            search_uuid (uuid.UUID): The search UUID for which the
                `ModelSearch` record object will be retrieved.

        Returns:
             ModelSearch: The retrieved `ModelSearch` record object or `None`
                if no matches were found.
        """

        # Cleanup the Auth0 user ID.
        auth0_user_id = clean_auth0_user_id(auth0_user_id=str(auth0_user_id))

        # Check that the requesting user is authorized to retrieve the user with
        # the given Auth0 ID.
        check_auth(info=info, auth0_user_id=auth0_user_id)

        # Retrieve the query on `ModelSearch`.
        query = TypeSearch.get_query(
            info=info,
        )  # type: sqlalchemy.orm.query.Query

        # Filter to the `ModelSearch` record matching the `search_uuid`.
        query = query.filter(ModelSearch.search_uuid == search_uuid)

        # Limit query to fields requested in the GraphQL query adding
        # `load_only` and `joinedload` options as required.
        query = apply_requested_fields(
            info=info,
            query=query,
            orm_class=ModelSearch,
        )

        obj = query.one_or_none()

        # Raise an exception if the requested search could not be found.
        if not obj:
            msg = "Search with UUID '{}' could not be found."
            msg_fmt = msg.format(search_uuid)
            raise graphql.GraphQLError(message=msg_fmt)

        return obj
示例#4
0
    def mutate(
        args: Dict,
        info: graphql.execution.base.ResolveInfo,
        auth0_user_id: str,
        search_uuid: uuid.UUID,
    ):
        """ Deletes a `ModelSearch` record and all its `ModelSearchDescriptor`
            records.

        Returns
            MutationSearchUpsert: The result of the mutation.
        """

        # Cleanup the Auth0 user ID.
        auth0_user_id = clean_auth0_user_id(auth0_user_id=str(auth0_user_id))

        # Check that the requesting user is authorized to make changes to the
        # user with the given Auth0 ID.
        check_auth(info=info, auth0_user_id=auth0_user_id)

        # Retrieve the session out of the context as the `get_query` method
        # automatically selects the model.
        session = info.context.get("session")  # type: sqlalchemy.orm.Session

        # Retrieve the `ModelUser` record object.
        user = get_user(session=session, auth0_user_id=auth0_user_id)

        # Raise an exception if the requested user could not be found.
        if not user:
            raise graphql.GraphQLError(
                message=f"User with Auth0 ID '{auth0_user_id}' could not be "
                f"found.")

        # Retrieve the `ModelSearch` record object.
        search = get_search(session=session, search_uuid=search_uuid)

        # Raise an exception if the requested search could not be found.
        if not search:
            raise graphql.GraphQLError(
                message=f"Search with UUID '{search_uuid}' under user with "
                f"Auth0 ID '{auth0_user_id}' could not be found.")

        # Delete the `ModelSearch` record and all its related `ModelUserSearch`
        # and `ModelSearchDescriptor` records.
        delete_search(session=session, search_id=search.search_id)

        session.commit()

        return MutationSearchDelete(search=search)
示例#5
0
    def mutate(
        args: Dict,
        info: graphql.execution.base.ResolveInfo,
        user: InputUser,
    ):
        """ Upserts a `ModelUser` record based on the `user` input.

        Returns
            MutationUserUpsert: The result of the mutation.
        """

        # Cleanup the Auth0 user ID.
        auth0_user_id = clean_auth0_user_id(auth0_user_id=str(
            user.auth0_user_id), )

        # Check that the requesting user is authorized to upsert the user with
        # the given Auth0 ID.
        check_auth(info=info, auth0_user_id=auth0_user_id)

        # Retrieve the session out of the context as the `get_query` method
        # automatically selects the model.
        session = info.context.get("session")  # type: sqlalchemy.orm.Session

        # Upsert the `ModelUser` record.
        statement = insert(ModelUser,
                           values={
                               "auth0_user_id": user.auth0_user_id,
                               "email": user.email,
                           }).on_conflict_do_nothing()  # type: Insert

        # Execute the upsert.
        result = session.execute(statement)  # type: ResultProxy

        # Retrieve the newly upserted `ModelUser` record object.
        obj = get_user(session=session, auth0_user_id=auth0_user_id)

        session.commit()

        return MutationUserUpsert(user=obj)
示例#6
0
    def mutate(
        args: Dict,
        info: graphql.execution.base.ResolveInfo,
        user_citation: InputUserCitation,
    ):
        """ Deletes a `ModelUserCitation` record based on the `user_citation`
            input.

        Returns
            MutationUserCitationDelete: The result of the mutation.
        """

        # Cleanup the Auth0 user ID.
        auth0_user_id = clean_auth0_user_id(auth0_user_id=str(
            user_citation.auth0_user_id), )

        # Retrieve the PubMed ID of the defined study.
        pmid = int(str(user_citation.pmid))

        # Check that the requesting user is authorized to upsert the user with
        # the given Auth0 ID.
        check_auth(info=info, auth0_user_id=auth0_user_id)

        # Retrieve the session out of the context as the `get_query` method
        # automatically selects the model.
        session = info.context.get("session")  # type: sqlalchemy.orm.Session

        # Retrieve the `ModelUser` record object.
        user = get_user(session=session, auth0_user_id=auth0_user_id)

        # Raise an exception if the requested user could not be found.
        if not user:
            msg = "User with Auth0 ID '{}' could not be found."
            msg_fmt = msg.format(auth0_user_id)
            raise graphql.GraphQLError(message=msg_fmt)

        # Retrieve the `ModelCitation` record object.
        citation = get_citation(session=session, pmid=pmid)

        # Raise an exception if the requested citation could not be found.
        if not citation:
            msg = "Citation with PubMed ID '{}' could not be found."
            msg_fmt = msg.format(pmid)
            raise graphql.GraphQLError(message=msg_fmt)

        # Retrieve the `ModelUserCitation` record object.
        query = session.query(ModelUserCitation)
        query = query.filter(ModelUserCitation.user_id == user.user_id)
        query = query.filter(
            ModelUserCitation.citation_id == citation.citation_id, )
        user_citation = query.one_or_none()

        # Raise an exception if the requested user-citation could not be found.
        if not user_citation:
            msg = ("User-citation for user with Auth0 ID '{} and PubMed "
                   "ID '{}' could not be found.")
            msg_fmt = msg.format(auth0_user_id, pmid)
            raise graphql.GraphQLError(message=msg_fmt)

        # Delete the `ModelUserCitation` record.
        session.delete(user_citation)

        # Retrieve the newly upserted `ModelUser` record object.
        obj = get_user(session=session, auth0_user_id=auth0_user_id)

        session.commit()

        return MutationUserCitationDelete(user=obj)
示例#7
0
    def mutate(
        args: Dict,
        info: graphql.execution.base.ResolveInfo,
        user_study: InputUserStudy,
    ):
        """ Deletes a `ModelUserStudy` record based on the `user_study` input.

        Returns
            MutationUserStudyDelete: The result of the mutation.
        """

        # Cleanup the Auth0 user ID.
        auth0_user_id = clean_auth0_user_id(auth0_user_id=str(
            user_study.auth0_user_id), )

        # Retrieve the NCT ID of the defined study.
        nct_id = str(user_study.nct_id)

        # Check that the requesting user is authorized to upsert the user with
        # the given Auth0 ID.
        check_auth(info=info, auth0_user_id=auth0_user_id)

        # Retrieve the session out of the context as the `get_query` method
        # automatically selects the model.
        session = info.context.get("session")  # type: sqlalchemy.orm.Session

        # Retrieve the `ModelUser` record object.
        user = get_user(session=session, auth0_user_id=auth0_user_id)

        # Raise an exception if the requested user could not be found.
        if not user:
            msg = "User with Auth0 ID '{}' could not be found."
            msg_fmt = msg.format(auth0_user_id)
            raise graphql.GraphQLError(message=msg_fmt)

        # Retrieve the `ModelStudy` record object.
        study = get_study(session=session, nct_id=nct_id)

        # Raise an exception if the requested study could not be found.
        if not study:
            msg = "Study with NCT ID '{}' could not be found."
            msg_fmt = msg.format(nct_id)
            raise graphql.GraphQLError(message=msg_fmt)

        # Retrieve the `ModelUserStudy` record object.
        query = session.query(ModelUserStudy)
        query = query.filter(ModelUserStudy.user_id == user.user_id)
        query = query.filter(ModelUserStudy.study_id == study.study_id)
        user_study = query.one_or_none()

        # Raise an exception if the requested user-study could not be found.
        if not user_study:
            msg = ("User-study for user with Auth0 ID '{} and NCT ID '{}' "
                   "could not be found.")
            msg_fmt = msg.format(auth0_user_id, nct_id)
            raise graphql.GraphQLError(message=msg_fmt)

        # Delete the `ModelUserStudy` record.
        session.delete(user_study)

        # Retrieve the newly upserted `ModelUser` record object.
        obj = get_user(session=session, auth0_user_id=auth0_user_id)

        session.commit()

        return MutationUserStudyDelete(user=obj)
示例#8
0
    def mutate(
        args: Dict,
        info: graphql.execution.base.ResolveInfo,
        auth0_user_id: str,
        search: InputSearch,
        mesh_descriptor_ids: List[int],
    ):
        """ Upserts a `ModelSearch` record based on the `search` input.

        Returns
            MutationSearchUpsert: The result of the mutation.
        """

        # Cleanup the Auth0 user ID.
        auth0_user_id = clean_auth0_user_id(auth0_user_id=str(auth0_user_id))

        # Check that the requesting user is authorized to make changes to the
        # user with the given Auth0 ID.
        check_auth(info=info, auth0_user_id=auth0_user_id)

        # Retrieve the session out of the context as the `get_query` method
        # automatically selects the model.
        session = info.context.get("session")  # type: sqlalchemy.orm.Session

        # Retrieve the `ModelUser` record object.
        user = get_user(session=session, auth0_user_id=auth0_user_id)

        # Raise an exception if the requested user could not be found.
        if not user:
            raise graphql.GraphQLError(
                message=f"User with Auth0 ID '{auth0_user_id}' could not be "
                f"found.")

        # Upsert the `ModelSearch` record.
        statement = insert(
            ModelSearch,
            values={
                "search_uuid":
                search.search_uuid,
                "title":
                search.title,
                "gender":
                EnumGender.get_member(
                    value=str(search.gender)) if search.gender else None,
                "year_beg":
                search.year_beg,
                "year_end":
                search.year_end,
                "age_beg":
                search.age_beg,
                "age_end":
                search.age_end,
            }).on_conflict_do_nothing()  # type: Insert
        # Execute the upsert.
        session.execute(statement)  # type: ResultProxy

        # Retrieve the newly upserted `ModelSearch` record object.
        search = get_search(session=session, search_uuid=search.search_uuid)

        # Upsert a `ModelUserSearch` record.
        statement = insert(ModelUserSearch,
                           values={
                               "user_id": user.user_id,
                               "search_id": search.search_id,
                           }).on_conflict_do_nothing()  # type: Insert
        # Execute the upsert.
        session.execute(statement)  # type: ResultProxy

        # Delete all existing `ModelSearchDescriptor` records for the given
        # search so they can be replaced with new ones.
        delete_search_descriptors(session=session, search_id=search.search_id)

        # Upsert a `ModelSearchDescriptor` record for each of the defined
        # descriptors.
        for mesh_descriptor_id in mesh_descriptor_ids:
            statement = insert(ModelSearchDescriptor,
                               values={
                                   "search_id": search.search_id,
                                   "descriptor_id": mesh_descriptor_id,
                               }).on_conflict_do_nothing()  # type: Insert
            # Execute the upsert.
            session.execute(statement)  # type: ResultProxy

        session.commit()

        return MutationSearchUpsert(search=search)