I am migrating an MVC to REST API/front end JS approach and there is one piece that I am not sure about the implementation pattern with DRF:
- Every model has a method that returns list of available actions (say, if this is task, it will have things like 'assign', 'set due date', 'mark as completed', 'confirm completion')
- Once an action is requested, a specific set of model's fields needs to be displayed to the user to fill out
- When the form is returned to the server (along with its action ID) the backend logic does its actions
How do I migrate this into REST-based workflow? What is the generic pattern for implementing these?
This is how this is implemented currently:
Base class method that defines the actions available:
@property
def wf_get_actions(self, *args, **kwargs):
if not hasattr(self, 'get_wf_def') or not hasattr(self, 'get_wf_field'):
return None
else:
all_actions = WFTransition.get_from_schema(self.get_wf_def())
available_actions = []
for action in all_actions:
if getattr(self, self.get_wf_field()).id in action.trans_from:
if not action.perm_getter or getattr(self, action.perm_getter).__call__(*args, **kwargs):
available_actions.append(action)
else:
pass
return available_actions
all_actions are collected from WFTransition class:
class WFTransition:
def __init__(self, code, name, trans_from, trans_to, fields, perm_getter):
self.code = code
self.name = name
self.trans_from = trans_from
self.trans_to = trans_to
self.fields = fields
self.perm_getter = perm_getter
@staticmethod
def get_from_schema(schema):
return [WFTransition(code=item[0],
name=item[1],
trans_from=item[2],
trans_to=item[3],
fields=item[4],
perm_getter=item[5]) for item in schema]
It retrieves the list of actions from settings:
# 0 1 2 3 4 5
#('<action_code>' '<Transition Name>', [<statuses from>], <status to>, [<fields to display>], <Permission getter>)
#Permission getter will return True of False based on the action availability based on user context
wf_ActionItem = (
('close', 'Close', [1, 2, 3], 4, ['resolution'], 'get_perm_close'),
('mark_done', 'Mark as Done', [1, 2], 3, ['resolution'], None),
('confirm_done', 'Confirm as Done', [3], 4, ['resolution'], 'get_perm_confirm'),
('reopen', 'Reopen', [4], 1, ['resolution', 'assignee'], None),
('assign', 'Assign', [1, 2, 3], None, ['assignee'], None),
('delete', 'Delete', [1, 2, 3, 4], 5, ['resolution'], 'get_perm_delete'),
)
the outcome of the property above is the list of actions that it can perform. It generates the URL at the UI level. They all go to the same view. The view (on GET) will return the form based on the action_id and (on POST) will validate and transition entity from one status to another.
I have broken my head around how this would be implemented with REST now.
Aucun commentaire:
Enregistrer un commentaire