Skip to content

Utils

Utils - A set of auxiliary methods for a commons module.

correct_mongo_filter(cls_model, filter, lang_code)

Correcting filter of request.

Corrects TextField fields that require localization of translation.

Source code in src/ramifice/commons/utils.py
def correct_mongo_filter(cls_model: Any, filter: Any, lang_code: str) -> Any:
    """Correcting filter of request.

    Corrects `TextField` fields that require localization of translation.
    """
    filter_json: str = json_util.dumps(filter)
    filter_json = (
        cls_model.META["regex_mongo_filter"]
        .sub(rf'\g<field>.{lang_code}":', filter_json)
        .replace(
            '":.',
            ".",
        )
    )
    return json_util.loads(filter_json)

mongo_doc_to_model_dict(cls_model, mongo_doc, lang_code, utc_timezone)

Convert Mongo document to Model document.

Special changes
  • _id to id (str)
  • password to None
  • date to str
  • datetime to str
Source code in src/ramifice/commons/utils.py
def mongo_doc_to_model_dict(
    cls_model: Any,
    mongo_doc: dict[str, Any],
    lang_code: str,
    utc_timezone,
) -> dict[str, Any]:
    """Convert Mongo document to Model document.

    Special changes:
        - `_id to id (str)`
        - `password to None`
        - `date to str`
        - `datetime to str`
    """
    descriptor_fields = cls_model.META["all_descriptor_fields"]
    instance_model: Any = cls_model(lang_code)
    model_dict: dict[str, Any] = {}

    for f_name in descriptor_fields:
        mongo_value = mongo_doc.get(f_name)

        f__core = getattr(instance_model, f"{f_name}__core")
        field_type = f__core.field_type

        if mongo_value is None or f__core.is_ignore:
            model_dict[f_name] = None
            continue

        if field_type == "TextField":
            model_dict[f_name] = mongo_value.get(lang_code, "- -") if isinstance(mongo_value, dict) else mongo_value
        elif "Date" in field_type:
            if "Time" in field_type:
                model_dict[f_name] = format_datetime(
                    datetime=mongo_value,
                    format="medium",
                    tzinfo=utc_timezone,
                    locale=lang_code,
                )
            else:
                model_dict[f_name] = format_date(
                    date=mongo_value.date(),
                    format="medium",
                    locale=lang_code,
                )
        elif field_type == "IDField":
            model_dict["id"] = str(mongo_value)
        elif field_type == "PasswordField":
            model_dict[f_name] = None
        else:
            model_dict[f_name] = mongo_value

    return model_dict

password_to_none(field_name_and_type, mongo_doc)

In the Mongo document, set all passwords to None.

Source code in src/ramifice/commons/utils.py
def password_to_none(
    field_name_and_type: dict[str, str],
    mongo_doc: dict[str, Any],
) -> dict[str, Any]:
    """In the Mongo document, set all passwords to None."""
    for f_name, t_name in field_name_and_type.items():
        if t_name == "PasswordField":
            mongo_doc[f_name] = None
    return mongo_doc