-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#8 refactoring and preparation to implement change state
- Loading branch information
1 parent
b73aae1
commit f6c8318
Showing
6 changed files
with
196 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
from .process import Process | ||
from .transition import Transition | ||
from .commands import Permissions, Conditions, Command | ||
from .commands import Permissions, Conditions, SideEffects, Callbacks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,54 @@ | ||
class BaseCommand: | ||
def __init__(self, commands=None): | ||
class BaseTransitionCommand(object): | ||
""" | ||
Command descriptor | ||
""" | ||
def __init__(self, commands=None, transition=None, *args, **kwargs): | ||
self.commands = commands or [] | ||
self.transition = transition | ||
|
||
def __get__(self, transition, owner): | ||
if self.transition is None: | ||
self.transition = transition | ||
return self | ||
|
||
def __set__(self, transition, commands): | ||
if self.transition is None: | ||
self.transition = transition | ||
self.commands = commands | ||
|
||
def __delete__(self, instance): | ||
del self.commands | ||
|
||
def execute(self, *args, **kwargs): | ||
raise NotImplementedError | ||
|
||
|
||
class CeleryCommand(BaseCommand): | ||
def execute(self): | ||
# TODO: https://github.com/Borderless360/django-logic/issues/1 | ||
pass | ||
class SideEffects(BaseTransitionCommand): | ||
def execute(self, instance: any, field_name): | ||
try: | ||
for command in self.commands: | ||
command(instance) | ||
except Exception: | ||
self.transition.fail_transition(instance, field_name) | ||
else: | ||
self.transition.complete_transition(instance, field_name) | ||
|
||
|
||
class Command(BaseCommand): | ||
def execute(self, instance: any): | ||
for command in self.commands: | ||
command(instance) | ||
class Callbacks(BaseTransitionCommand): | ||
def execute(self, instance, field_name): | ||
try: | ||
for command in self.commands: | ||
command(instance) | ||
except Exception: | ||
# TODO: logger | ||
pass | ||
|
||
|
||
class Conditions(BaseCommand): | ||
class Conditions(BaseTransitionCommand): | ||
def execute(self, instance: any): | ||
return all(command(instance) for command in self.commands) | ||
|
||
|
||
class Permissions(BaseCommand): | ||
class Permissions(BaseTransitionCommand): | ||
def execute(self, instance: any, user: any): | ||
return all(command(instance, user) for command in self.commands) | ||
return all(command(instance, user) for command in self.commands) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.