Skip to content

One

Requests like find one.

OneMixin

Requests like find one.

Source code in src/ramifice/commons/one.py
class OneMixin:
    """Requests like `find one`."""

    @classmethod
    async def find_one(
        cls: Any,
        filter: Any | None = None,
        lang_code: str = deepcopy(Translator.DEFAULT_LOCALE),
        *args: tuple,
        **kwargs: dict[str, Any],
    ) -> dict[str, Any] | None:
        """Get a single document from the database."""
        metadata = cls.META
        # Get collection for current model.
        collection: AsyncCollection = Config.MONGO_DATABASE[metadata["collection_name"]]
        # Correcting filter.
        if filter is not None:
            filter = correct_mongo_filter(cls, filter, lang_code)
        # Get document.
        mongo_doc = await collection.find_one(filter, *args, **kwargs)
        if mongo_doc is not None:
            mongo_doc = password_to_none(
                metadata["field_name_and_type"],
                mongo_doc,
            )
        return mongo_doc

    @classmethod
    async def find_one_to_model_dict(
        cls: Any,
        filter: Any | None = None,
        lang_code: str = deepcopy(Translator.DEFAULT_LOCALE),
        *args: tuple,
        **kwargs: dict[str, Any],
    ) -> dict[str, Any] | None:
        """Find a single document and convert to Model in dictionary format.

        Hint:
        - `lang_code` - Required for a text field with `is_multilingual=True`.
        """
        metadata = cls.META
        utc_timezone = deepcopy(Config.UTC_TIMEZONE)
        # Get collection for current model.
        collection: AsyncCollection = Config.MONGO_DATABASE[metadata["collection_name"]]
        # Correcting filter.
        if filter is not None:
            filter = correct_mongo_filter(cls, filter, lang_code)
        # Get document.
        model_dict = None
        mongo_doc = await collection.find_one(filter, *args, **kwargs)

        if mongo_doc is not None:
            model_dict = mongo_doc_to_model_dict(
                cls,
                mongo_doc,
                lang_code,
                utc_timezone,
            )
        return model_dict

    @classmethod
    async def find_one_to_instance_model(
        cls: Any,
        filter: Any | None = None,
        lang_code: str = deepcopy(Translator.DEFAULT_LOCALE),
        *args: tuple,
        **kwargs: dict[str, Any],
    ) -> Any | None:
        """Find a single document and convert it to a Model instance.

        Hint:
        - `lang_code` - Required for a text field with `is_multilingual=True`.
        """
        metadata = cls.META
        # Get collection for current model.
        collection: AsyncCollection = Config.MONGO_DATABASE[metadata["collection_name"]]
        # Correcting filter.
        if filter is not None:
            filter = correct_mongo_filter(cls, filter, lang_code)
        # Get document.
        mongo_doc = await collection.find_one(filter, *args, **kwargs)
        instance_model = None
        if mongo_doc is not None:
            # Convert document to Model instance.
            instance_model = cls.from_mongo_doc(mongo_doc, lang_code)
        return instance_model

    @classmethod
    async def find_one_to_json(
        cls: Any,
        filter: Any | None = None,
        lang_code: str = deepcopy(Translator.DEFAULT_LOCALE),
        *args: tuple,
        **kwargs: dict[str, Any],
    ) -> str | None:
        """Find a single document and convert it to a JSON string.

        Hint:
        - `lang_code` - Required for a text field with `is_multilingual=True`.
        """
        metadata = cls.META
        # Get collection for current model.
        collection: AsyncCollection = Config.MONGO_DATABASE[metadata["collection_name"]]
        # Correcting filter.
        if filter is not None:
            filter = correct_mongo_filter(cls, filter, lang_code)
        # Get document.
        json_str: str | None = None
        mongo_doc = await collection.find_one(filter, *args, **kwargs)
        if mongo_doc is not None:
            # Convert document to Model instance.
            instance_model = cls.from_mongo_doc(mongo_doc, lang_code)
            json_str = instance_model.to_json()
        return json_str

    @classmethod
    async def delete_one(
        cls: Any,
        filter: Any,
        collation: Any | None = None,
        hint: Any | None = None,
        session: Any | None = None,
        let: Any | None = None,
        comment: Any | None = None,
        lang_code: str = deepcopy(Translator.DEFAULT_LOCALE),
    ) -> DeleteResult:
        """Delete a single document matching the filter.

        Hint:
        - `lang_code` - Required for a text field with `is_multilingual=True`.
        """
        metadata = cls.META
        # Raises a panic if the Model cannot be removed.
        if not metadata["is_delete_doc"]:
            msg = (
                f"Model: `{metadata['full_model_name']}` > "
                + "META param: `is_delete_doc` (False) => "
                + "Documents of this Model cannot be removed from the database!"
            )
            logger.error(msg)
            raise ForbiddenDeleteDocError(msg)
        # Get collection for current model.
        collection: AsyncCollection = Config.MONGO_DATABASE[metadata["collection_name"]]
        # Correcting filter.
        if filter is not None:
            filter = correct_mongo_filter(cls, filter, lang_code)
        # Get document.
        result: DeleteResult = await collection.delete_one(
            filter=filter,
            collation=collation,
            hint=hint,
            session=session,
            let=let,
            comment=comment,
        )
        return result

    @classmethod
    async def find_one_and_delete(
        cls: Any,
        filter: Any,
        projection: Any | None = None,
        sort: Any | None = None,
        hint: Any | None = None,
        session: Any | None = None,
        let: Any | None = None,
        comment: Any | None = None,
        lang_code: str = deepcopy(Translator.DEFAULT_LOCALE),
        **kwargs: dict[str, Any],
    ) -> Any | None:
        """Finds a single document and deletes it, returning the document.

        Hint:
        - `lang_code` - Required for a text field with `is_multilingual=True`.
        """
        metadata = cls.META
        # Raises a panic if the Model cannot be removed.
        if not metadata["is_delete_doc"]:
            msg = (
                f"Model: `{metadata['full_model_name']}` > "
                + "META param: `is_delete_doc` (False) => "
                + "Documents of this Model cannot be removed from the database!"
            )
            logger.error(msg)
            raise ForbiddenDeleteDocError(msg)
        # Get collection for current model.
        collection: AsyncCollection = Config.MONGO_DATABASE[metadata["collection_name"]]
        # Correcting filter.
        if filter is not None:
            filter = correct_mongo_filter(cls, filter, lang_code)
        # Get document.
        mongo_doc: dict[str, Any] | None = await collection.find_one_and_delete(
            filter=filter,
            projection=projection,
            sort=sort,
            hint=hint,
            session=session,
            let=let,
            comment=comment,
            **kwargs,
        )
        instance_model = None
        if mongo_doc is not None:
            # Convert document to Model instance.
            instance_model = cls.from_mongo_doc(mongo_doc, lang_code)
        return instance_model

delete_one(filter, collation=None, hint=None, session=None, let=None, comment=None, lang_code=deepcopy(Translator.DEFAULT_LOCALE)) async classmethod

Delete a single document matching the filter.

Hint: - lang_code - Required for a text field with is_multilingual=True.

Source code in src/ramifice/commons/one.py
@classmethod
async def delete_one(
    cls: Any,
    filter: Any,
    collation: Any | None = None,
    hint: Any | None = None,
    session: Any | None = None,
    let: Any | None = None,
    comment: Any | None = None,
    lang_code: str = deepcopy(Translator.DEFAULT_LOCALE),
) -> DeleteResult:
    """Delete a single document matching the filter.

    Hint:
    - `lang_code` - Required for a text field with `is_multilingual=True`.
    """
    metadata = cls.META
    # Raises a panic if the Model cannot be removed.
    if not metadata["is_delete_doc"]:
        msg = (
            f"Model: `{metadata['full_model_name']}` > "
            + "META param: `is_delete_doc` (False) => "
            + "Documents of this Model cannot be removed from the database!"
        )
        logger.error(msg)
        raise ForbiddenDeleteDocError(msg)
    # Get collection for current model.
    collection: AsyncCollection = Config.MONGO_DATABASE[metadata["collection_name"]]
    # Correcting filter.
    if filter is not None:
        filter = correct_mongo_filter(cls, filter, lang_code)
    # Get document.
    result: DeleteResult = await collection.delete_one(
        filter=filter,
        collation=collation,
        hint=hint,
        session=session,
        let=let,
        comment=comment,
    )
    return result

find_one(filter=None, lang_code=deepcopy(Translator.DEFAULT_LOCALE), *args, **kwargs) async classmethod

Get a single document from the database.

Source code in src/ramifice/commons/one.py
@classmethod
async def find_one(
    cls: Any,
    filter: Any | None = None,
    lang_code: str = deepcopy(Translator.DEFAULT_LOCALE),
    *args: tuple,
    **kwargs: dict[str, Any],
) -> dict[str, Any] | None:
    """Get a single document from the database."""
    metadata = cls.META
    # Get collection for current model.
    collection: AsyncCollection = Config.MONGO_DATABASE[metadata["collection_name"]]
    # Correcting filter.
    if filter is not None:
        filter = correct_mongo_filter(cls, filter, lang_code)
    # Get document.
    mongo_doc = await collection.find_one(filter, *args, **kwargs)
    if mongo_doc is not None:
        mongo_doc = password_to_none(
            metadata["field_name_and_type"],
            mongo_doc,
        )
    return mongo_doc

find_one_and_delete(filter, projection=None, sort=None, hint=None, session=None, let=None, comment=None, lang_code=deepcopy(Translator.DEFAULT_LOCALE), **kwargs) async classmethod

Finds a single document and deletes it, returning the document.

Hint: - lang_code - Required for a text field with is_multilingual=True.

Source code in src/ramifice/commons/one.py
@classmethod
async def find_one_and_delete(
    cls: Any,
    filter: Any,
    projection: Any | None = None,
    sort: Any | None = None,
    hint: Any | None = None,
    session: Any | None = None,
    let: Any | None = None,
    comment: Any | None = None,
    lang_code: str = deepcopy(Translator.DEFAULT_LOCALE),
    **kwargs: dict[str, Any],
) -> Any | None:
    """Finds a single document and deletes it, returning the document.

    Hint:
    - `lang_code` - Required for a text field with `is_multilingual=True`.
    """
    metadata = cls.META
    # Raises a panic if the Model cannot be removed.
    if not metadata["is_delete_doc"]:
        msg = (
            f"Model: `{metadata['full_model_name']}` > "
            + "META param: `is_delete_doc` (False) => "
            + "Documents of this Model cannot be removed from the database!"
        )
        logger.error(msg)
        raise ForbiddenDeleteDocError(msg)
    # Get collection for current model.
    collection: AsyncCollection = Config.MONGO_DATABASE[metadata["collection_name"]]
    # Correcting filter.
    if filter is not None:
        filter = correct_mongo_filter(cls, filter, lang_code)
    # Get document.
    mongo_doc: dict[str, Any] | None = await collection.find_one_and_delete(
        filter=filter,
        projection=projection,
        sort=sort,
        hint=hint,
        session=session,
        let=let,
        comment=comment,
        **kwargs,
    )
    instance_model = None
    if mongo_doc is not None:
        # Convert document to Model instance.
        instance_model = cls.from_mongo_doc(mongo_doc, lang_code)
    return instance_model

find_one_to_instance_model(filter=None, lang_code=deepcopy(Translator.DEFAULT_LOCALE), *args, **kwargs) async classmethod

Find a single document and convert it to a Model instance.

Hint: - lang_code - Required for a text field with is_multilingual=True.

Source code in src/ramifice/commons/one.py
@classmethod
async def find_one_to_instance_model(
    cls: Any,
    filter: Any | None = None,
    lang_code: str = deepcopy(Translator.DEFAULT_LOCALE),
    *args: tuple,
    **kwargs: dict[str, Any],
) -> Any | None:
    """Find a single document and convert it to a Model instance.

    Hint:
    - `lang_code` - Required for a text field with `is_multilingual=True`.
    """
    metadata = cls.META
    # Get collection for current model.
    collection: AsyncCollection = Config.MONGO_DATABASE[metadata["collection_name"]]
    # Correcting filter.
    if filter is not None:
        filter = correct_mongo_filter(cls, filter, lang_code)
    # Get document.
    mongo_doc = await collection.find_one(filter, *args, **kwargs)
    instance_model = None
    if mongo_doc is not None:
        # Convert document to Model instance.
        instance_model = cls.from_mongo_doc(mongo_doc, lang_code)
    return instance_model

find_one_to_json(filter=None, lang_code=deepcopy(Translator.DEFAULT_LOCALE), *args, **kwargs) async classmethod

Find a single document and convert it to a JSON string.

Hint: - lang_code - Required for a text field with is_multilingual=True.

Source code in src/ramifice/commons/one.py
@classmethod
async def find_one_to_json(
    cls: Any,
    filter: Any | None = None,
    lang_code: str = deepcopy(Translator.DEFAULT_LOCALE),
    *args: tuple,
    **kwargs: dict[str, Any],
) -> str | None:
    """Find a single document and convert it to a JSON string.

    Hint:
    - `lang_code` - Required for a text field with `is_multilingual=True`.
    """
    metadata = cls.META
    # Get collection for current model.
    collection: AsyncCollection = Config.MONGO_DATABASE[metadata["collection_name"]]
    # Correcting filter.
    if filter is not None:
        filter = correct_mongo_filter(cls, filter, lang_code)
    # Get document.
    json_str: str | None = None
    mongo_doc = await collection.find_one(filter, *args, **kwargs)
    if mongo_doc is not None:
        # Convert document to Model instance.
        instance_model = cls.from_mongo_doc(mongo_doc, lang_code)
        json_str = instance_model.to_json()
    return json_str

find_one_to_model_dict(filter=None, lang_code=deepcopy(Translator.DEFAULT_LOCALE), *args, **kwargs) async classmethod

Find a single document and convert to Model in dictionary format.

Hint: - lang_code - Required for a text field with is_multilingual=True.

Source code in src/ramifice/commons/one.py
@classmethod
async def find_one_to_model_dict(
    cls: Any,
    filter: Any | None = None,
    lang_code: str = deepcopy(Translator.DEFAULT_LOCALE),
    *args: tuple,
    **kwargs: dict[str, Any],
) -> dict[str, Any] | None:
    """Find a single document and convert to Model in dictionary format.

    Hint:
    - `lang_code` - Required for a text field with `is_multilingual=True`.
    """
    metadata = cls.META
    utc_timezone = deepcopy(Config.UTC_TIMEZONE)
    # Get collection for current model.
    collection: AsyncCollection = Config.MONGO_DATABASE[metadata["collection_name"]]
    # Correcting filter.
    if filter is not None:
        filter = correct_mongo_filter(cls, filter, lang_code)
    # Get document.
    model_dict = None
    mongo_doc = await collection.find_one(filter, *args, **kwargs)

    if mongo_doc is not None:
        model_dict = mongo_doc_to_model_dict(
            cls,
            mongo_doc,
            lang_code,
            utc_timezone,
        )
    return model_dict