1

Knowing task instance is there a way to get the url of it? For example in the cookbook: https://github.com/viewflow/cookbook/blob/master/helloworld/demo/helloworld/flows.py - how do I get the url of assign task of approve flow_task?

I know there is flow_task.get_task_url(task, url_type='guess', namespace='', **kwargs), but the point is that from what I can see the namespace is usually fetched from self.request.resolver_match.namespace. That's not ideal - what if we are in other part of the app and we simply want to provide links to the tasks directly?

emihir0
  • 1,200
  • 3
  • 16
  • 39

3 Answers3

1

Same as with django reverse you need to pass a namespace to get an URL. In case of build-in viewflow frontend the namespace is viewflow:[app_label]:[flow_label] ex: "viewflow:helloworld:helloworld"

kmmbvnr
  • 5,863
  • 4
  • 35
  • 44
  • Yes, I see. It would be worthwhile to simply add `get_absolute_url` to Task model though. – emihir0 Jun 07 '18 at 11:19
  • It's not possible, since namespace mapping could be different depends on end user url config. – kmmbvnr Jun 08 '18 at 03:07
  • I suppose you are right, but it should still be better to have global SETTINGS variable for viewflow namespace and use that in `get_absolute_url`, rather than this kind of complicated view of getting the url. By default it should use `get_absolute_url`, if an end user wants to modify default behaviour, he can set the settings variable to let the framework know about it... – emihir0 Jun 08 '18 at 11:58
  • I don't think that adding more settings could solve any problem. Explicit namespace passing is match better. – kmmbvnr Jun 09 '18 at 03:28
  • I am trying to reverse the url but I got `AttributeError: 'str' object has no attribute 'urls'`, coming from `base_url, self.viewset.urls, module=self)`, and `self.viewset` is a string value `'viewflow.frontend.viewset.FrontendViewSet'` – James Lin Oct 29 '19 at 20:30
0

To get the url of a task all you need is the app_name(app_namespace), flow_namespace and flow_label. The most challenging item here is the flow_namespace (if you have not used the frontend urls). To resolve this, you could use a map borrowing from FlowListMixin's ns_map. Defining the flow_namespace for every flow in your project. You then determine the flows namespace and url_name from the above.

ns_map = {'MyFlow':'flow_namespace', 'AnotherFlow':'flow_namespace2'}

# flow_namespace as defined in the urls.py
# e.g if you defined your flow urls as
# flow_urls = FlowViewSet(MyFlow).urls
# flow_urls2 = FLowViewSet(MyFlow2).urls
# urlpatterns = [url(r'flow_path/', include(flow_urls, name=flow_namespace)),
# url(r'flow_path2/', include(flow_urls2, name=flow_namespace2)),
# ]
# With this is included in the root_url as
# urlpatterns = [
#     url(r'app/' include(app_urls, namespace='app_namespace')
#]

What you need is to reverse the flow like this reverse('app_name:flow_namespace:flow_label', kwargs={'process_pk':ppk, 'task_pk':tpk})

flow_class_name = task.process.flow_class.__name__
flow_namespace = ns_map.get(flow_class_name)
app_name = task.process.flow_class.__module__.split('.')[0]
flow_label = task.flow_task.name
url_name = "{}:{}:{}".format(app_name, flow_label, url_name)

Then you can reverse your task url

url = reverse(url_name, kwargs={"task_pk", task.pk, "process_pk":task.flow_process.pk}

# If you know where you are you could use resolver match to determine
# the app namespace. Be Sure of this, see more of that [here][1]

NOTE: I am assuming that you namespaced your apps as app_name If it is different you have to find alternatives to finding the app_names namespace but that should not be too difficult.

unlockme
  • 3,897
  • 3
  • 28
  • 42
0

If you have the task object in a template, you can extract the url as follows:

<a href="/workflow/formworkflow/{{ task.process.flow_class.flow_name }}/{{ task.process.flow_class.process_name }}/{{ task.process.pk }}/{{task.flow_task.name}}/{{task.id}}">Task Link</a>

This could be added as a template filter if used often.

This is a dirty hack untill I understand how the namespaces work.

Dan Walters
  • 1,218
  • 1
  • 18
  • 30