django 模型的状态机
项目描述
Django 状态
描述
django 模型的状态引擎。为模型定义状态图并记住每个对象的状态。可以记录对象的状态转换。
笔记
此分叉提供了专门针对 Pivotal Energy Solutions 的更改
安装
pip install pivotal-django-states
使用示例
from django_states.fields import StateField
from django_states.machine import StateMachine, StateDefinition, StateTransition
class PurchaseStateMachine(StateMachine):
log_transitions = True
# possible states
class initiated(StateDefinition):
description = _('Purchase initiated')
initial = True
class paid(StateDefinition):
description = _('Purchase paid')
def handler(self, instance):
code_to_execute_when_arriving_in_this_state()
class shipped(StateDefinition):
description = _('Purchase shipped')
# state transitions
class mark_paid(StateTransition):
from_state = 'initiated'
to_state = 'paid'
description = 'Mark this purchase as paid'
class ship(StateTransition):
from_state = 'paid'
to_state = 'shipped'
description = 'Ship purchase'
def handler(transition, instance, user):
code_to_execute_during_this_transition()
def has_permission(transition, instance, user):
return true_when_user_can_make_this_transition()
class Purchase(StateModel):
purchase_state = StateField(machine=PurchaseStateMachine, default='initiated')
... (other fields for a purchase)
如果log_transitions
启用,则会创建另一个模型。一切都应该与 South_ 兼容以进行迁移。
注意:如果您要DataMigration
在
South <http://south.aeracode.org/>
__ 中创建一个,请记住使用
obj.save(no_state_validation=True)
使用示例:
p = Purchase()
# Will automatically create state object for this purchase, in the
# initial state.
p.save()
p.get_purchase_state_info().make_transition('mark_paid', request.user) # User parameter is optional
p.state # Will return 'paid'
p.get_purchase_state_info().description # Will return 'Purchase paid'
# Returns an iterator of possible transitions for this purchase.
p.get_purchase_state_info().possible_transitions()
# Which can be used like this..
[x.get_name() for x in p.possible_transitions]
为了更好的过渡控制,覆盖:
has_permission(self, instance, user)
: 检查是否允许此用户进行此转换。handler(self, instance, user)
:在此过渡期间运行的代码。当此处引发异常时,将不会进行转换。
获取特定状态的所有对象::
Purchase.objects.filter(state='已启动')
验证
您可以添加一个需要在执行状态转换之前通过的测试。好吧,您可以添加 2:一个基于当前用户 ( has_permission
) 和一个通用 ( validate
)。
因此,在StateTransition
-object 上,您需要指定一个额外的
validate
函数(签名是validate(cls, instance)
)。这应该 yield TransitionValidationError
,这样你可以返回多个错误,需要在转换发生之前通过。
函数(has_permission
签名
has_permission(transition, instance, user)
)应检查是否允许给定用户进行转换。例如,超级用户可以审核所有评论,而其他用户只能审核他们博客帖子的评论。
团体
有时您想将多个状态组合在一起,因为对于某个视图(或其他内容)来说,它是哪个状态并不重要。我们支持 2 个不同的状态组,包括(仅这些)或排他(除这些之外的所有):
class is_paid(StateGroup):
states = ['paid', 'shipped']
class is_paid(StateGroup):
exclude_states = ['initiated']
状态图
您可以通过运行graph_states
管理命令来获取状态图。
python manage.py graph_states myapp.Purchase.state
这需要graphviz和 python 绑定用于 graphviz:pygraphviz
和yapgvb
.
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。
源分布
内置分布
pivotal_django_states -1.6.14.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 89d254daf4055f5533251210d5ee1059cbfc26551b9db1d2ba90d56335338e93 |
|
MD5 | 8ea8cd954ea88ef0e37f6378cf8889e2 |
|
布莱克2-256 | 6f7582c0194d8c42b1db85a13fb72abeaa6aa7f03ea7dc6bef71dcee01fa5e8d |
pivotal_django_states -1.6.14-py2.py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | e026a22450361b039e17868c84577e520b89cbd609eea226275f23bcb8e30a08 |
|
MD5 | a3777cb3ac284b3b9888fbb0c1252482 |
|
布莱克2-256 | 1278897dc7f64b44fd49043dd343f5d742e4758d151ae05b4dcaba5d2d95de00 |