|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import contextlib |
| 4 | +import typing |
| 5 | +from dataclasses import dataclass |
| 6 | +from functools import partial |
| 7 | +from functools import wraps |
| 8 | + |
| 9 | +from django.contrib.contenttypes.models import ContentType |
| 10 | +from django.db import models |
| 11 | + |
| 12 | +from .models import StateLog |
| 13 | +from .models import TransitionLogBase |
| 14 | +from .signals import post_transition |
| 15 | + |
| 16 | +if typing.TYPE_CHECKING: # pragma: no cover |
| 17 | + from collections.abc import Callable |
| 18 | + |
| 19 | + from . import _Field |
| 20 | + |
| 21 | + |
| 22 | +__all__ = [ |
| 23 | + "StateLog", |
| 24 | + "TransitionLogBase", |
| 25 | + "fsm_log_by", |
| 26 | + "fsm_log_description", |
| 27 | + "track", |
| 28 | +] |
| 29 | + |
| 30 | + |
| 31 | +@dataclass(frozen=True) |
| 32 | +class TrackConfig: |
| 33 | + log_model: type[TransitionLogBase] | None |
| 34 | + relation_field: str | None |
| 35 | + |
| 36 | + |
| 37 | +_registry: dict[type[models.Model], TrackConfig] = {} |
| 38 | +NOTSET = object() |
| 39 | + |
| 40 | + |
| 41 | +def track( |
| 42 | + *, |
| 43 | + log_model: type[TransitionLogBase] | None = None, |
| 44 | + relation_field: str | None = None, |
| 45 | +) -> Callable[[type[models.Model]], type[models.Model]]: |
| 46 | + def decorator(model_cls: type[models.Model]) -> type[models.Model]: |
| 47 | + if model_cls._meta.abstract: |
| 48 | + raise TypeError("django_fsm.track cannot be used with abstract models") |
| 49 | + config = TrackConfig(log_model=log_model, relation_field=relation_field) |
| 50 | + _registry[model_cls] = config |
| 51 | + |
| 52 | + post_transition.connect( |
| 53 | + _log_transition, |
| 54 | + sender=model_cls, |
| 55 | + dispatch_uid=f"django_fsm.track.{model_cls._meta.label_lower}", |
| 56 | + weak=False, |
| 57 | + ) |
| 58 | + return model_cls |
| 59 | + |
| 60 | + return decorator |
| 61 | + |
| 62 | + |
| 63 | +class FSMLogDescriptor: |
| 64 | + ATTR_PREFIX = "__django_fsm_log_attr_" |
| 65 | + |
| 66 | + def __init__(self, instance: models.Model, attribute: str, value: typing.Any = NOTSET): |
| 67 | + self.instance = instance |
| 68 | + self.attribute = attribute |
| 69 | + if value is not NOTSET: |
| 70 | + self.set(value) |
| 71 | + |
| 72 | + def get(self) -> typing.Any: |
| 73 | + return getattr(self.instance, self.ATTR_PREFIX + self.attribute) |
| 74 | + |
| 75 | + def set(self, value: typing.Any) -> None: |
| 76 | + setattr(self.instance, self.ATTR_PREFIX + self.attribute, value) |
| 77 | + |
| 78 | + def __enter__(self) -> typing.Self: |
| 79 | + return self |
| 80 | + |
| 81 | + def __exit__(self, *args: object) -> None: |
| 82 | + with contextlib.suppress(AttributeError): |
| 83 | + delattr(self.instance, self.ATTR_PREFIX + self.attribute) |
| 84 | + |
| 85 | + |
| 86 | +def fsm_log_by(func: typing.Callable[..., typing.Any]) -> typing.Callable[..., typing.Any]: |
| 87 | + @wraps(func) |
| 88 | + def wrapped(instance: models.Model, *args: typing.Any, **kwargs: typing.Any) -> typing.Any: |
| 89 | + if "by" in kwargs: |
| 90 | + author = kwargs.pop("by") |
| 91 | + else: |
| 92 | + return func(instance, *args, **kwargs) |
| 93 | + |
| 94 | + with FSMLogDescriptor(instance, "by", author): |
| 95 | + return func(instance, *args, **kwargs) |
| 96 | + |
| 97 | + return wrapped |
| 98 | + |
| 99 | + |
| 100 | +def fsm_log_description( |
| 101 | + func: typing.Callable[..., typing.Any] | None = None, |
| 102 | + *, |
| 103 | + description: str | None = None, |
| 104 | +) -> typing.Callable[..., typing.Any]: |
| 105 | + if func is None: |
| 106 | + return partial(fsm_log_description, description=description) |
| 107 | + |
| 108 | + @wraps(func) |
| 109 | + def wrapped(instance: models.Model, *args: typing.Any, **kwargs: typing.Any) -> typing.Any: |
| 110 | + with FSMLogDescriptor(instance, "description") as descriptor: |
| 111 | + if "description" in kwargs: |
| 112 | + descriptor.set(kwargs.pop("description")) |
| 113 | + else: |
| 114 | + descriptor.set(description) |
| 115 | + return func(instance, *args, **kwargs) |
| 116 | + |
| 117 | + return wrapped |
| 118 | + |
| 119 | + |
| 120 | +def _log_transition( |
| 121 | + sender: type[models.Model], |
| 122 | + instance: models.Model, |
| 123 | + name: str, |
| 124 | + source: typing.Any, |
| 125 | + target: typing.Any, |
| 126 | + field: _Field, |
| 127 | + **kwargs: typing.Any, |
| 128 | +) -> None: |
| 129 | + config = _registry.get(sender) |
| 130 | + if not config or instance.pk is None: |
| 131 | + return |
| 132 | + |
| 133 | + log_model = config.log_model or StateLog |
| 134 | + log_kwargs: dict[str, typing.Any] = { |
| 135 | + "transition": name, |
| 136 | + "state_field": field.name, |
| 137 | + "source_state": _coerce_state(source), |
| 138 | + "state": _coerce_state(target), |
| 139 | + "by": _extract_log_value(instance, "by"), |
| 140 | + "description": _extract_log_value(instance, "description"), |
| 141 | + } |
| 142 | + |
| 143 | + if issubclass(log_model, StateLog): |
| 144 | + log_kwargs["content_type"] = ContentType.objects.get_for_model(sender) |
| 145 | + log_kwargs["object_id"] = str(instance.pk) |
| 146 | + else: |
| 147 | + relation_field = config.relation_field or _resolve_relation_field(log_model, sender) |
| 148 | + log_kwargs[relation_field] = instance |
| 149 | + |
| 150 | + log_model._default_manager.using(instance._state.db).create(**log_kwargs) |
| 151 | + |
| 152 | + |
| 153 | +def _resolve_relation_field( |
| 154 | + log_model: type[TransitionLogBase], model_cls: type[models.Model] |
| 155 | +) -> str: |
| 156 | + relation_fields = [ |
| 157 | + field.name |
| 158 | + for field in log_model._meta.fields |
| 159 | + if isinstance(field, models.ForeignKey) |
| 160 | + and _matches_model(field.remote_field.model, model_cls) |
| 161 | + ] |
| 162 | + if len(relation_fields) == 1: |
| 163 | + return relation_fields[0] |
| 164 | + |
| 165 | + if not relation_fields: |
| 166 | + raise ValueError( |
| 167 | + f"{log_model.__name__} does not define a ForeignKey to {model_cls.__name__}" |
| 168 | + ) |
| 169 | + raise ValueError( |
| 170 | + f"{log_model.__name__} has multiple ForeignKey fields to {model_cls.__name__}; " |
| 171 | + "set relation_field when calling track()" |
| 172 | + ) |
| 173 | + |
| 174 | + |
| 175 | +def _coerce_state(value: typing.Any) -> str | None: |
| 176 | + if value is None: |
| 177 | + return None |
| 178 | + if isinstance(value, models.Model): |
| 179 | + return str(value.pk) |
| 180 | + return str(value) |
| 181 | + |
| 182 | + |
| 183 | +def _matches_model(remote_model: typing.Any, model_cls: type[models.Model]) -> bool: |
| 184 | + if remote_model == model_cls: |
| 185 | + return True |
| 186 | + if isinstance(remote_model, str): |
| 187 | + return remote_model == model_cls.__name__ or remote_model.endswith(f".{model_cls.__name__}") |
| 188 | + return False |
| 189 | + |
| 190 | + |
| 191 | +def _extract_log_value( |
| 192 | + instance: models.Model, |
| 193 | + attribute: str, |
| 194 | +) -> typing.Any: |
| 195 | + try: |
| 196 | + return FSMLogDescriptor(instance, attribute).get() |
| 197 | + except AttributeError: |
| 198 | + return None |
0 commit comments