XWorkflows - Principes

  • Description lisible de la logique métier
  • États et transitions
  • Actions spécifiques lors des transitions
  • Validation des enchaînements d'état
example.png

Presenter Notes

Définition

class TicketWorkflow(xworkflows.Workflow):
    states = [
        ('new', _(u"New")),
        ('assigned', _(u"Assigned")),
        ('closed', _(u"Closed")),
    ]
    transitions = [
        ('assign', ['new'], 'assigned'),
        ('reject', ['assigned'], 'new'),
        ('reassign', ['assigned'], 'assigned'),
        ('close', ['new', 'assigned'], 'closed'),
    ]
    initial_state = 'new'

Presenter Notes

Utilisation

class Ticket(xworkflows.WorkflowEnabled):
    state = TicketWorkflow()

>>> ticket = MyTicket()
>>> ticket.state
State('new')

>>> ticket.assign()
>>> ticket.state
State('assigned')

>>> ticket.assign()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
InvalidTransitionError: Transition "assign" isn't available from state "new"

Presenter Notes

Actions

class Ticket(xworkflows.WorkflowEnabled):
    state = TicketWorkflow()

    @transition()
    def assign(self, to):
        self.assignee = to

>>> ticket = MyTicket()
>>> ticket.state
State('new')

>>> ticket.assign('ponyconseil')
>>> ticket.state
State('assigned')
>>> ticket.assignee
'ponyconseil'

Presenter Notes

Fonctions complémentaires

if ticket.state.is_new:
    # short for <ticket.state == TicketWorkflow.states.new>

class SomeObject(xworkflows.WorkflowEnabled):
    @transition('bar')
    def gobar(self, x):
        # In case 'bar' has another use (attribute, ...)
        if x != 42:
            raise xworkflows.AbortTransition()


>>> ticket.state
State('assigned')
>>> ticket.state.transitions
[Transition('reassign'), Transition('close')]

Presenter Notes

Intégration avec Django

http://github.com/rbarrois/django_xworkflows/

  • Création d'un TransitionLog à chaque transition
  • Sauvegarde automatique de l'objet après la transition
class MyModel(django_xworkflows.XWorkflowEnabled):
    state = MyWorkflow()

MyModel().gobar(log=True, save=True)

django_xworkflows.TransitionLog.objects.all()
[<TransitionLog(obj, 'gobar')>]

Presenter Notes