This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 313
/
__init__.py
768 lines (613 loc) · 25.6 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
# -*- coding: utf-8 -*-
"""
State tracking functionality for django models
"""
import inspect
import sys
from functools import wraps
import django
from django.db import models
from django.db.models import Field
from django.db.models.query_utils import DeferredAttribute
from django.db.models.signals import class_prepared
from django_fsm.signals import pre_transition, post_transition
try:
from functools import partialmethod
except ImportError:
# python 2.7, so we are on django<=1.11
from django.utils.functional import curry as partialmethod
try:
from django.apps import apps as django_apps
def get_model(app_label, model_name):
app = django_apps.get_app_config(app_label)
return app.get_model(model_name)
except ImportError:
from django.db.models.loading import get_model
__all__ = [
"TransitionNotAllowed",
"ConcurrentTransition",
"FSMFieldMixin",
"FSMField",
"FSMIntegerField",
"FSMKeyField",
"ConcurrentTransitionMixin",
"transition",
"can_proceed",
"has_transition_perm",
"GET_STATE",
"RETURN_VALUE",
]
import warnings
def show_deprecation_warning():
message = (
"The 'django-fsm' package has been integrated into 'viewflow' as 'viewflow.fsm' starting from version 3.0. "
"This version of 'django-fsm' is no longer maintained and will not receive further updates. "
"If you require new functionality introduced in 'django-fsm' version 3.0 or later, "
"please migrate to 'viewflow.fsm'. For detailed instructions on the migration process and accessing new features, "
"refer to the official documentation at https://docs.viewflow.io/fsm/index.html"
)
warnings.warn(message, UserWarning, stacklevel=2)
show_deprecation_warning()
if sys.version_info[:2] == (2, 6):
# Backport of Python 2.7 inspect.getmembers,
# since Python 2.6 ships buggy implementation
def __getmembers(object, predicate=None):
"""Return all members of an object as (name, value) pairs sorted by name.
Optionally, only return members that satisfy a given predicate."""
results = []
for key in dir(object):
try:
value = getattr(object, key)
except AttributeError:
continue
if not predicate or predicate(value):
results.append((key, value))
results.sort()
return results
inspect.getmembers = __getmembers
# South support; see http://south.aeracode.org/docs/tutorial/part4.html#simple-inheritance
try:
from south.modelsinspector import add_introspection_rules
except ImportError:
pass
else:
add_introspection_rules([], [r"^django_fsm\.FSMField"])
add_introspection_rules([], [r"^django_fsm\.FSMIntegerField"])
add_introspection_rules([], [r"^django_fsm\.FSMKeyField"])
class TransitionNotAllowed(Exception):
"""Raised when a transition is not allowed"""
def __init__(self, *args, **kwargs):
self.object = kwargs.pop("object", None)
self.method = kwargs.pop("method", None)
super(TransitionNotAllowed, self).__init__(*args, **kwargs)
class InvalidResultState(Exception):
"""Raised when we got invalid result state"""
class ConcurrentTransition(Exception):
"""
Raised when the transition cannot be executed because the
object has become stale (state has been changed since it
was fetched from the database).
"""
class Transition(object):
def __init__(
self, method, source, target, on_error, conditions, permission, custom
):
self.method = method
self.source = source
self.target = target
self.on_error = on_error
self.conditions = conditions
self.permission = permission
self.custom = custom
@property
def name(self):
return self.method.__name__
def has_perm(self, instance, user):
if not self.permission:
return True
elif callable(self.permission):
return bool(self.permission(instance, user))
elif user.has_perm(self.permission, instance):
return True
elif user.has_perm(self.permission):
return True
else:
return False
def __hash__(self):
return hash(self.name)
def __eq__(self, other):
if isinstance(other, str):
return other == self.name
if isinstance(other, Transition):
return other.name == self.name
return False
def get_available_FIELD_transitions(instance, field):
"""
List of transitions available in current model state
with all conditions met
"""
curr_state = field.get_state(instance)
transitions = field.transitions[instance.__class__]
for name, transition in transitions.items():
meta = transition._django_fsm
if meta.has_transition(curr_state) and meta.conditions_met(
instance, curr_state
):
yield meta.get_transition(curr_state)
def get_all_FIELD_transitions(instance, field):
"""
List of all transitions available in current model state
"""
return field.get_all_transitions(instance.__class__)
def get_available_user_FIELD_transitions(instance, user, field):
"""
List of transitions available in current model state
with all conditions met and user have rights on it
"""
for transition in get_available_FIELD_transitions(instance, field):
if transition.has_perm(instance, user):
yield transition
class FSMMeta(object):
"""
Models methods transitions meta information
"""
def __init__(self, field, method):
self.field = field
self.transitions = {} # source -> Transition
def get_transition(self, source):
transition = self.transitions.get(source, None)
if transition is None:
transition = self.transitions.get("*", None)
if transition is None:
transition = self.transitions.get("+", None)
return transition
def add_transition(
self,
method,
source,
target,
on_error=None,
conditions=[],
permission=None,
custom={},
):
if source in self.transitions:
raise AssertionError("Duplicate transition for {0} state".format(source))
self.transitions[source] = Transition(
method=method,
source=source,
target=target,
on_error=on_error,
conditions=conditions,
permission=permission,
custom=custom,
)
def has_transition(self, state):
"""
Lookup if any transition exists from current model state using current method
"""
if state in self.transitions:
return True
if "*" in self.transitions:
return True
if "+" in self.transitions and self.transitions["+"].target != state:
return True
return False
def conditions_met(self, instance, state):
"""
Check if all conditions have been met
"""
transition = self.get_transition(state)
if transition is None:
return False
elif transition.conditions is None:
return True
else:
return all(
map(lambda condition: condition(instance), transition.conditions)
)
def has_transition_perm(self, instance, state, user):
transition = self.get_transition(state)
if not transition:
return False
else:
return transition.has_perm(instance, user)
def next_state(self, current_state):
transition = self.get_transition(current_state)
if transition is None:
raise TransitionNotAllowed("No transition from {0}".format(current_state))
return transition.target
def exception_state(self, current_state):
transition = self.get_transition(current_state)
if transition is None:
raise TransitionNotAllowed("No transition from {0}".format(current_state))
return transition.on_error
class FSMFieldDescriptor(object):
def __init__(self, field):
self.field = field
def __get__(self, instance, type=None):
if instance is None:
return self
return self.field.get_state(instance)
def __set__(self, instance, value):
if self.field.protected and self.field.name in instance.__dict__:
raise AttributeError(
"Direct {0} modification is not allowed".format(self.field.name)
)
# Update state
self.field.set_proxy(instance, value)
self.field.set_state(instance, value)
class FSMFieldMixin(object):
descriptor_class = FSMFieldDescriptor
def __init__(self, *args, **kwargs):
self.protected = kwargs.pop("protected", False)
self.transitions = {} # cls -> (transitions name -> method)
self.state_proxy = {} # state -> ProxyClsRef
state_choices = kwargs.pop("state_choices", None)
choices = kwargs.get("choices", None)
if state_choices is not None and choices is not None:
raise ValueError("Use one of choices or state_choices value")
if state_choices is not None:
choices = []
for state, title, proxy_cls_ref in state_choices:
choices.append((state, title))
self.state_proxy[state] = proxy_cls_ref
kwargs["choices"] = choices
super(FSMFieldMixin, self).__init__(*args, **kwargs)
def deconstruct(self):
name, path, args, kwargs = super(FSMFieldMixin, self).deconstruct()
if self.protected:
kwargs["protected"] = self.protected
return name, path, args, kwargs
def get_state(self, instance):
# The state field may be deferred. We delegate the logic of figuring
# this out and loading the deferred field on-demand to Django's
# built-in DeferredAttribute class. DeferredAttribute's instantiation
# signature changed over time, so we need to check Django version
# before proceeding to call DeferredAttribute. An alternative to this
# would be copying the latest implementation of DeferredAttribute to
# django_fsm, but this comes with the added responsibility of keeping
# the copied code up to date.
if django.VERSION[:3] >= (3, 0, 0):
return DeferredAttribute(self).__get__(instance)
elif django.VERSION[:3] >= (2, 1, 0):
return DeferredAttribute(self.name).__get__(instance)
elif django.VERSION[:3] >= (1, 10, 0):
return DeferredAttribute(self.name, model=None).__get__(instance)
else:
# The field was either not deferred (in which case we can return it
# right away) or ir was, but we are running on an unknown version
# of Django and we do not know the appropriate DeferredAttribute
# interface, and accessing the field will raise KeyError.
return instance.__dict__[self.name]
def set_state(self, instance, state):
instance.__dict__[self.name] = state
def set_proxy(self, instance, state):
"""
Change class
"""
if state in self.state_proxy:
state_proxy = self.state_proxy[state]
try:
app_label, model_name = state_proxy.split(".")
except ValueError:
# If we can't split, assume a model in current app
app_label = instance._meta.app_label
model_name = state_proxy
model = get_model(app_label, model_name)
if model is None:
raise ValueError("No model found {0}".format(state_proxy))
instance.__class__ = model
def change_state(self, instance, method, *args, **kwargs):
meta = method._django_fsm
method_name = method.__name__
current_state = self.get_state(instance)
if not meta.has_transition(current_state):
raise TransitionNotAllowed(
"Can't switch from state '{0}' using method '{1}'".format(
current_state, method_name
),
object=instance,
method=method,
)
if not meta.conditions_met(instance, current_state):
raise TransitionNotAllowed(
"Transition conditions have not been met for method '{0}'".format(
method_name
),
object=instance,
method=method,
)
next_state = meta.next_state(current_state)
signal_kwargs = {
"sender": instance.__class__,
"instance": instance,
"name": method_name,
"field": meta.field,
"source": current_state,
"target": next_state,
"method_args": args,
"method_kwargs": kwargs,
}
pre_transition.send(**signal_kwargs)
try:
result = method(instance, *args, **kwargs)
if next_state is not None:
if hasattr(next_state, "get_state"):
next_state = next_state.get_state(
instance, transition, result, args=args, kwargs=kwargs
)
signal_kwargs["target"] = next_state
self.set_proxy(instance, next_state)
self.set_state(instance, next_state)
except Exception as exc:
exception_state = meta.exception_state(current_state)
if exception_state:
self.set_proxy(instance, exception_state)
self.set_state(instance, exception_state)
signal_kwargs["target"] = exception_state
signal_kwargs["exception"] = exc
post_transition.send(**signal_kwargs)
raise
else:
post_transition.send(**signal_kwargs)
return result
def get_all_transitions(self, instance_cls):
"""
Returns [(source, target, name, method)] for all field transitions
"""
transitions = self.transitions[instance_cls]
for name, transition in transitions.items():
meta = transition._django_fsm
for transition in meta.transitions.values():
yield transition
def contribute_to_class(self, cls, name, **kwargs):
self.base_cls = cls
super(FSMFieldMixin, self).contribute_to_class(cls, name, **kwargs)
setattr(cls, self.name, self.descriptor_class(self))
setattr(
cls,
"get_all_{0}_transitions".format(self.name),
partialmethod(get_all_FIELD_transitions, field=self),
)
setattr(
cls,
"get_available_{0}_transitions".format(self.name),
partialmethod(get_available_FIELD_transitions, field=self),
)
setattr(
cls,
"get_available_user_{0}_transitions".format(self.name),
partialmethod(get_available_user_FIELD_transitions, field=self),
)
class_prepared.connect(self._collect_transitions)
def _collect_transitions(self, *args, **kwargs):
sender = kwargs["sender"]
if not issubclass(sender, self.base_cls):
return
def is_field_transition_method(attr):
return (
(inspect.ismethod(attr) or inspect.isfunction(attr))
and hasattr(attr, "_django_fsm")
and (
attr._django_fsm.field in [self, self.name]
or (
isinstance(attr._django_fsm.field, Field)
and attr._django_fsm.field.name == self.name
and attr._django_fsm.field.creation_counter
== self.creation_counter
)
)
)
sender_transitions = {}
transitions = inspect.getmembers(sender, predicate=is_field_transition_method)
for method_name, method in transitions:
method._django_fsm.field = self
sender_transitions[method_name] = method
self.transitions[sender] = sender_transitions
class FSMField(FSMFieldMixin, models.CharField):
"""
State Machine support for Django model as CharField
"""
def __init__(self, *args, **kwargs):
kwargs.setdefault("max_length", 50)
super(FSMField, self).__init__(*args, **kwargs)
class FSMIntegerField(FSMFieldMixin, models.IntegerField):
"""
Same as FSMField, but stores the state value in an IntegerField.
"""
pass
class FSMKeyField(FSMFieldMixin, models.ForeignKey):
"""
State Machine support for Django model
"""
def get_state(self, instance):
return instance.__dict__[self.attname]
def set_state(self, instance, state):
instance.__dict__[self.attname] = self.to_python(state)
class FSMModelMixin(object):
"""
Mixin that allows refresh_from_db for models with fsm protected fields
"""
def _get_protected_fsm_fields(self):
def is_fsm_and_protected(f):
return isinstance(f, FSMFieldMixin) and f.protected
protected_fields = filter(is_fsm_and_protected, self._meta.concrete_fields)
return {f.attname for f in protected_fields}
def refresh_from_db(self, *args, **kwargs):
fields = kwargs.pop("fields", None)
# Use provided fields, if not set then reload all non-deferred fields.0
if not fields:
deferred_fields = self.get_deferred_fields()
protected_fields = self._get_protected_fsm_fields()
skipped_fields = deferred_fields.union(protected_fields)
fields = [
f.attname
for f in self._meta.concrete_fields
if f.attname not in skipped_fields
]
kwargs["fields"] = fields
super(FSMModelMixin, self).refresh_from_db(*args, **kwargs)
class ConcurrentTransitionMixin(object):
"""
Protects a Model from undesirable effects caused by concurrently executed transitions,
e.g. running the same transition multiple times at the same time, or running different
transitions with the same SOURCE state at the same time.
This behavior is achieved using an idea based on optimistic locking. No additional
version field is required though; only the state field(s) is/are used for the tracking.
This scheme is not that strict as true *optimistic locking* mechanism, it is however
more lightweight - leveraging the specifics of FSM models.
Instance of a model based on this Mixin will be prevented from saving into DB if any
of its state fields (instances of FSMFieldMixin) has been changed since the object
was fetched from the database. *ConcurrentTransition* exception will be raised in such
cases.
For guaranteed protection against such race conditions, make sure:
* Your transitions do not have any side effects except for changes in the database,
* You always run the save() method on the object within django.db.transaction.atomic()
block.
Following these recommendations, you can rely on ConcurrentTransitionMixin to cause
a rollback of all the changes that have been executed in an inconsistent (out of sync)
state, thus practically negating their effect.
"""
def __init__(self, *args, **kwargs):
super(ConcurrentTransitionMixin, self).__init__(*args, **kwargs)
self._update_initial_state()
@property
def state_fields(self):
return filter(lambda field: isinstance(field, FSMFieldMixin), self._meta.fields)
def _do_update(self, base_qs, using, pk_val, values, update_fields, forced_update):
# _do_update is called once for each model class in the inheritance hierarchy.
# We can only filter the base_qs on state fields (can be more than one!) present in this particular model.
# Select state fields to filter on
filter_on = filter(
lambda field: field.model == base_qs.model, self.state_fields
)
# state filter will be used to narrow down the standard filter checking only PK
state_filter = dict(
(field.attname, self.__initial_states[field.attname]) for field in filter_on
)
updated = super(ConcurrentTransitionMixin, self)._do_update(
base_qs=base_qs.filter(**state_filter),
using=using,
pk_val=pk_val,
values=values,
update_fields=update_fields,
forced_update=forced_update,
)
# It may happen that nothing was updated in the original _do_update method not because of unmatching state,
# but because of missing PK. This codepath is possible when saving a new model instance with *preset PK*.
# In this case Django does not know it has to do INSERT operation, so it tries UPDATE first and falls back to
# INSERT if UPDATE fails.
# Thus, we need to make sure we only catch the case when the object *is* in the DB, but with changed state; and
# mimic standard _do_update behavior otherwise. Django will pick it up and execute _do_insert.
if not updated and base_qs.filter(pk=pk_val).using(using).exists():
raise ConcurrentTransition(
"Cannot save object! The state has been changed since fetched from the database!"
)
return updated
def _update_initial_state(self):
self.__initial_states = dict(
(field.attname, field.value_from_object(self))
for field in self.state_fields
)
def refresh_from_db(self, *args, **kwargs):
super(ConcurrentTransitionMixin, self).refresh_from_db(*args, **kwargs)
self._update_initial_state()
def save(self, *args, **kwargs):
super(ConcurrentTransitionMixin, self).save(*args, **kwargs)
self._update_initial_state()
def transition(
field,
source="*",
target=None,
on_error=None,
conditions=[],
permission=None,
custom={},
):
"""
Method decorator to mark allowed transitions.
Set target to None if current state needs to be validated and
has not changed after the function call.
"""
def inner_transition(func):
wrapper_installed, fsm_meta = True, getattr(func, "_django_fsm", None)
if not fsm_meta:
wrapper_installed = False
fsm_meta = FSMMeta(field=field, method=func)
setattr(func, "_django_fsm", fsm_meta)
if isinstance(source, (list, tuple, set)):
for state in source:
func._django_fsm.add_transition(
func, state, target, on_error, conditions, permission, custom
)
else:
func._django_fsm.add_transition(
func, source, target, on_error, conditions, permission, custom
)
@wraps(func)
def _change_state(instance, *args, **kwargs):
return fsm_meta.field.change_state(instance, func, *args, **kwargs)
if not wrapper_installed:
return _change_state
return func
return inner_transition
def can_proceed(bound_method, check_conditions=True):
"""
Returns True if model in state allows to call bound_method
Set ``check_conditions`` argument to ``False`` to skip checking
conditions.
"""
if not hasattr(bound_method, "_django_fsm"):
im_func = getattr(bound_method, "im_func", getattr(bound_method, "__func__"))
raise TypeError("%s method is not transition" % im_func.__name__)
meta = bound_method._django_fsm
im_self = getattr(bound_method, "im_self", getattr(bound_method, "__self__"))
current_state = meta.field.get_state(im_self)
return meta.has_transition(current_state) and (
not check_conditions or meta.conditions_met(im_self, current_state)
)
def has_transition_perm(bound_method, user):
"""
Returns True if model in state allows to call bound_method and user have rights on it
"""
if not hasattr(bound_method, "_django_fsm"):
im_func = getattr(bound_method, "im_func", getattr(bound_method, "__func__"))
raise TypeError("%s method is not transition" % im_func.__name__)
meta = bound_method._django_fsm
im_self = getattr(bound_method, "im_self", getattr(bound_method, "__self__"))
current_state = meta.field.get_state(im_self)
return (
meta.has_transition(current_state)
and meta.conditions_met(im_self, current_state)
and meta.has_transition_perm(im_self, current_state, user)
)
class State(object):
def get_state(self, model, transition, result, args=[], kwargs={}):
raise NotImplementedError
class RETURN_VALUE(State):
def __init__(self, *allowed_states):
self.allowed_states = allowed_states if allowed_states else None
def get_state(self, model, transition, result, args=[], kwargs={}):
if self.allowed_states is not None:
if result not in self.allowed_states:
raise InvalidResultState(
"{} is not in list of allowed states\n{}".format(
result, self.allowed_states
)
)
return result
class GET_STATE(State):
def __init__(self, func, states=None):
self.func = func
self.allowed_states = states
def get_state(self, model, transition, result, args=[], kwargs={}):
result_state = self.func(model, *args, **kwargs)
if self.allowed_states is not None:
if result_state not in self.allowed_states:
raise InvalidResultState(
"{} is not in list of allowed states\n{}".format(
result_state, self.allowed_states
)
)
return result_state