OpenStack workflow workflows principle of use is described in detail

  • 2020-05-27 07:47:43
  • OfStack


Workflows are complex forms (forms) and tabs, and each workflow must contain Workflow,Step, and Action

Here's an example of how to use workflow:

The following example shows how data is passed from urls, views, workflows, templates to each other

In, a parameter is defined, such as.resource_class_id.

RESOURCE_CLASS = r'^(?P<resource_class_id>[^/]+)/%s$'

urlpatterns = patterns(
url(RESOURCE_CLASS % 'update', UpdateView.as_view(), name='update'))

In, we can pass data to template (templates) and action(form). (action can also pass data to the get_context_data method or template)

class UpdateView(workflows.WorkflowView):
  workflow_class = UpdateResourceClass

  def get_context_data(self, **kwargs):
    context = super(UpdateView, self).get_context_data(**kwargs)
    # url The data in self.kwargs Where we can pass the data to template.url
    context["resource_class_id"] = self.kwargs['resource_class_id']
    #  The data comes from Workflow's Steps And save in context['workflow'].context In the list, we can do the same thing template  Use them in 
    return context

  def _get_object(self, *args, **kwargs):
    #url The data in self.kwargs , where we can load the object of interest 
    resource_class_id = self.kwargs['resource_class_id']
    # eg:my_objects = api.nova.get_by_id(resource_class_id)

  def get_initial(self):
    resource_class = self._get_object()
    #  The data here can be Action Methods and Workflow's handle Method is used, however steps Must be depend on The value 
    return {'resource_class_id':,
        'service_type': resource_class.service_type}

In, where we process data, workflows is essentially a more complex django form (form)

class ResourcesAction(workflows.Action):
  #  As defined below name Domain values   Of all the action  You can get it from either method 
  #  If we expect this value to be able to be in other steps Or other workflow The use of , It must come from the present step And to establish depend on On the other step In the 

  name = forms.CharField(max_length=255,
              label=_("Testing Name"),

  def handle(self, request, data):
    #  If you want to use url Is the parameter value in the Action The corresponding step Must be established in depend on Relationship between 
    #  can self.initial['resource_class_id'] or data['resource_class_id'] Get the value 

    #  If we want to use something else step The data in, then the others step  Must be contribute  Data, and two step It's in order 

class UpdateResources(workflows.Step):
  #  The transfer Workflow  The data to the action methods handle/clean , action The value you want to use in the depends_on  You have to define 
   # Workflow  the context  The data include url Data from others as well step In the contributed  The data coming in 
  depends_on = ("resource_class_id",)

  #  through contributes  Parameter, where the data can be other workflow Or other step Use, and it's worth noting, object_ids key You need to manually add to contributes  In the 

  contributes = ("resources_object_ids", "name")

  def contribute(self, data, context):
    #  Here you can get workflow the http request data 
    request = self.workflow.request
    if data:
      #  Only in the action The data defined in, is only available here, and if you want to get other values, you need to override them contribute  Method to add to the dictionary manually 
      context["resources_object_ids"] =\

    #  Merge the data passed above, which can also be given to the parent class to merge 
    return context

class UpdateResourceClass(workflows.Workflow):
  default_steps = (UpdateResources,)

  def handle(self, request, data):
    #  This method is executed at the end (all Action the handle After the method) 
    #  You can use it here step All of the 'contributes='  and 'depends_on='  The data of 
    #  Complex business logic can be handled here 

    # Available values here:  data["resources_object_ids"], data["name"] data["resources_class_id"] 

Thank you for reading, I hope to help you, thank you for your support of this site!

Related articles: