Exemple #1
0
def add_property_fields(new_model: Type["Model"], attrs: Dict) -> None:  # noqa: CCR001
    """
    Checks class namespace for properties or functions with __property_field__.
    If attribute have __property_field__ it was decorated with @property_field.

    Functions like this are exposed in dict() (therefore also fastapi result).
    Names of property fields are cached for quicker access / extraction.

    :param new_model: newly constructed model
    :type new_model: Model class
    :param attrs:
    :type attrs: Dict[str, str]
    """
    props = set()
    for var_name, value in attrs.items():
        if isinstance(value, property):
            value = value.fget
        field_config = getattr(value, "__property_field__", None)
        if field_config:
            props.add(var_name)

    if meta_field_not_set(model=new_model, field_name="property_fields"):
        new_model.Meta.property_fields = props
    else:
        new_model.Meta.property_fields = new_model.Meta.property_fields.union(props)
Exemple #2
0
def register_signals(new_model: Type["Model"]) -> None:  # noqa: CCR001
    """
    Registers on model's SignalEmmiter and sets pre defined signals.
    Predefined signals are (pre/post) + (save/update/delete).

    Signals are emitted in both model own methods and in selected queryset ones.

    :param new_model: newly constructed model
    :type new_model: Model class
    """
    if meta_field_not_set(model=new_model, field_name="signals"):
        signals = SignalEmitter()
        signals.pre_save = Signal()
        signals.pre_update = Signal()
        signals.pre_delete = Signal()
        signals.post_save = Signal()
        signals.post_update = Signal()
        signals.post_delete = Signal()
        new_model.Meta.signals = signals