Skip to content

Commit

Permalink
Merge pull request #32 from Borderless360/#29-process-name
Browse files Browse the repository at this point in the history
#28 process name implementation and refactoring
  • Loading branch information
emil-balashov committed Jan 14, 2020
2 parents 861e6ff + 6a935b2 commit c5ee0d7
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 44 deletions.
1 change: 1 addition & 0 deletions demo/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


class InvoiceProcess(Process):
process_name = 'invoice_process'
states = (
('draft', 'Draft'),
('paid', 'Paid'),
Expand Down
2 changes: 1 addition & 1 deletion demo/tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def setUp(self):
self.process_class = InvoiceProcess

def test_process_class_method(self):
self.assertEqual(self.process_class.get_readable_name(), 'Invoice Process')
self.assertEqual(self.process_class.process_name, 'invoice_process')

def test_invoice_process(self):
invoice = Invoice.objects.create(status='draft')
Expand Down
12 changes: 8 additions & 4 deletions django_logic/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ def get_conditions_id(obj):
return '{}|conditions'.format(id(obj))


def get_readable_process_name(process) -> str:
return ' '.join(process.process_name.split('_')).capitalize()


def annotate_nodes(process, node_name=None):
"""
This function annotate node names and nodes into two dicts:
- node_names contains either process or trasition unique node name.
- node_names contains either process or transition unique node name.
- nodes contains the information of the given process
:param process: Process class
:param node_name: None or str
Expand Down Expand Up @@ -63,12 +67,12 @@ def annotate_nodes(process, node_name=None):
}
"""
node_name = node_name or ''
node_name += process.get_readable_name() + '|'
node_name += get_readable_process_name(process) + '|'

# process
node = {
'id': get_object_id(process),
'name': process.get_readable_name(),
'name': get_readable_process_name(process),
'type': 'process',
'nodes': []
}
Expand All @@ -84,7 +88,7 @@ def annotate_nodes(process, node_name=None):
if process.conditions:
node['nodes'].append({
'id': get_conditions_id(process),
'name': '\n'.join([condition.__name__ for condition in process.conditions.commands]),
'name': '\n'.join([condition.__name__ for condition in process.conditions]),
'type': 'process_conditions',
})

Expand Down
14 changes: 3 additions & 11 deletions django_logic/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django_logic.commands import Conditions, Permissions
from django_logic.exceptions import ManyTransitions, TransitionNotAllowed
from django_logic.state import State
from django_logic.utils import convert_to_snake_case, convert_to_readable_name

logger = logging.getLogger(__name__)

Expand All @@ -27,8 +26,9 @@ class Process(object):
permissions = []
conditions_class = Conditions
permissions_class = Permissions
process_name = 'process'

def __init__(self, field_name: str, instance=None):
def __init__(self, field_name: str,instance=None):
"""
:param field_name:
"""
Expand All @@ -49,14 +49,6 @@ def __getattr__(self, item):
else:
# TODO: transition not available
raise TransitionNotAllowed('Transition not allowed')

@classmethod
def get_process_name(cls):
return convert_to_snake_case(str(cls.__name__))

@classmethod
def get_readable_name(cls):
return convert_to_readable_name(str(cls.__name__))

def validate(self, user=None) -> bool:
"""
Expand Down Expand Up @@ -107,7 +99,7 @@ def make_process_getter(field_name, field_class):
for state_field, process_class in kwargs.items():
if not issubclass(process_class, Process):
raise TypeError('Must be a sub class of Process')
parameters[process_class.get_process_name()] = property(make_process_getter(state_field, process_class))
parameters[process_class.process_name] = property(make_process_getter(state_field, process_class))
parameters['state_fields'].append(state_field)
return type('Process', (cls, ), parameters)

Expand Down
11 changes: 0 additions & 11 deletions django_logic/utils.py

This file was deleted.

2 changes: 2 additions & 0 deletions tests/test_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@


class FirstProcess(Process):
process_name = 'first_process'
transitions = [
Transition(action_name='transition1', sources=['state1'], target='state3')
]


class SecondProcess(Process):
process_name = 'second_process'
transitions = [
Transition(action_name='transition2', sources=['state2'], target='state3')
]
Expand Down
17 changes: 0 additions & 17 deletions tests/test_utils.py

This file was deleted.

0 comments on commit c5ee0d7

Please sign in to comment.