A state machine, is a mathematical model of computation used to design sequential logic circuits for items of a module. It is conceived as an abstract machine that can be in one of a finite number of states. The machine is in only one state at a time; the state it is in at any given time is called the current state. It can change from one state to another when initiated by a triggering event or condition; this is called a transition.
Initialise the statemachine for the given item.
Item: | Attach the state machine to this BaseItem |
---|---|
Item_state_attr: | |
name of the attribute which store the value of |
the current state of the statemachine in the given item. :init_state: Initialize the statemachine with an alternative state. (Not the value coming from item_state_attr) :request: Current request.
Need to be implemented in the inherited class.
Example:
s1 = State(self, 1, "On")
s2 = State(self, 2, "Off")
s1.add_transition(s2, "Turn on", handler, condition)
s2.add_transition(s1, "Turn off", handler, condition)
return s1
Returns a list of all states in the statemachine
Returns: | List of State objects. |
---|
Will set the current state of the state machine
State: | Id of the State or the State object to |
---|
be set as current state :returns: None
Returns the current state of the Statemachine
Returns: | Current State |
---|
A single state in a statemachine.
Initialise the State
Statemachine: | Statemachine |
---|---|
Id: | Id of the state |
Label: | Label of the Statemachine (short description) |
Description: | Long description of the state. |
Disabled_actions: | |
Dictionary with a list of actions which are |
disabled for a role. {‘rolename’: [‘read’, ‘update’]}
Will add a transtion to the given state
To_state: | End state of the transition |
---|---|
Label: | Label of the transition. Is usually a verb. |
Handler: | TransitionHandler which will be called if the state has been changed |
Condition: | TransitionCondition which must be true to make the transition available |
Returns: | None |
Returns a list of disabled actions of the state for the given role
Returns: | List of disabled actions |
---|
Returns the available transitions to other states.
Returns: | List of Transition objects. |
---|
A transitions is used to switch from one state into another. The two states are called start state and end state where the end state is the state of the state machine after the transtions has been done.
Every transtion can have a handler and a condition. The handler can be used to add some logic which should be trigger after the transition has been finished. The handler is a simple callable which is called with the item of the state machine.
The condition is also a callable and can be used to restrict the availability to whatever want. The function is called with the item of the state machine and returns true or false. If the condition returns true then a transition is available.
Creates a transition between to states.
Start_state: | Start State of the transition |
---|---|
End_state: | End of the transition |
Handler: | TransitionHandler which will be called if the state has been changed |
Condition: | TransitionCondition which must be true to make the transition available |
Returns the start state of the transition
Returns: | Start State |
---|
Returns the end state of the transition
Returns: | End State |
---|
Returns the label of the transtion. If no label was set return the two lables of the start and end state separated with a “->”.
Returns: | Label of the State |
---|
Do the transition! Exchanges the start state to the end state.
Returns: | End State of the transtion. |
---|
Check is the transition is available for the item in the statemachine. The function will call the condition function with the item of the statemachine. It returns true if the state is available (check succeeds) and returns False if the conditions for a state change are not met.
Handler callable which will be called if the transition between to State objects has been finished.
Condition callable which must be true to make the Transition between two State objects applicable.
Wrapper for pyramid’s buitin has_permission function. This wrapper sets dynamically the __acl__ attribute of the given context and then . check if the user has the given permission in the current context using pyramid’s has_permission function.
Context can be: * Instance of BaseItem * Subclass of BaseItem * Ressource, built from a RessourceFactory
If context is an instance or subclass of BaseItem the wrapper will dynamically set the __acl__ attribute. This attribute is used by the pyramid’s has_permission function the check the permission. If the context is a resource the function does nothing as the resource already has the __acl__ attribute set.
If the user has the permission the it returns True, else False (Actually it returns a boolean like object, see pyramids has_permission doc for more details.)
Permission: | String. Name of the permission. E.g list, create, read |
---|---|
Context: | Either Resource, Instance of BaseItem or Subclass of BaseItem |
Request: | current request |
Returns: | True or False (Boolean like object) |
Will return a list permissions attached to the modul and optionally to particular item of the modul. The returned list is suitable for using it as ACL in pyramid’s authorization system. The function will at least return class based permissions. If a item is provided then additional item level permissions are returned.
The function will iterate over all available actions of the given modul and tries to adds a permission entry to the for every role which has granted access to the action.
For every role it will check the following things:
Model: | The modul for which the permissions are returned |
---|---|
Item: | Optional: Item of the model for which the permissons as returned |
Returns: | List of permissions |
Returns a list of pricipals for the user with userid for the given request.
Principals are basically strings naming the groups or roles the user have. Example: role:admin or group:users are typical principals.
Userid: | id of the user |
---|---|
Request: | current request |
Returns: | list with pricipals |
Return True if the user has the given role. Else False” :user: User instance :returns: True or False
Returns a list of roles the user has. The list contains Role object and are collected by loading roles directly attached to the user plus roles attached to the groups the user is member of
User: | User instance |
---|---|
Returns: | List of Role instances |
Return True if the user is in the the given group. Else False” :user: User instance :returns: True or False