Workflow state action: perform a POST request

Questions, comments, discussions. Over time certain topics might be moved to their own category.
Post Reply
qra
Posts: 2
Joined: Fri Oct 25, 2019 9:08 pm

Workflow state action: perform a POST request

Post by qra » Fri Oct 25, 2019 9:34 pm

Hi. Is it possible to call the Mayan API from a workflow state action (in the same installation)? For example, to add a set of metadata to a document.

A fragment of the error log (mayan-gunicorn-stderr---supervisor-Kr2M_b.log):

Code: Select all

mayan.apps.common.middleware.error_logging <29973> [ERROR] "process_exception() line 18 Exception caught by request middleware; <WSGIRequest: POST '/api/documents/31/metadata/'>, int() argument must be a string or a number, not 'AnonymousUser'"
Traceback (most recent call last):
  File "/opt/mayan-edms/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/opt/mayan-edms/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
    return view_func(*args, **kwargs)
  File "/opt/mayan-edms/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "/opt/mayan-edms/lib/python2.7/site-packages/rest_framework/views.py", line 494, in dispatch
    response = self.handle_exception(exc)
  File "/opt/mayan-edms/lib/python2.7/site-packages/rest_framework/views.py", line 454, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/opt/mayan-edms/lib/python2.7/site-packages/rest_framework/views.py", line 491, in dispatch
    response = handler(request, *args, **kwargs)
  File "/opt/mayan-edms/lib/python2.7/site-packages/rest_framework/generics.py", line 244, in post
    return self.create(request, *args, **kwargs)
  File "/opt/mayan-edms/lib/python2.7/site-packages/rest_framework/mixins.py", line 19, in create
    serializer = self.get_serializer(data=request.data)
  File "/opt/mayan-edms/lib/python2.7/site-packages/mayan/apps/metadata/api_views.py", line 59, in get_serializer
    return super(APIDocumentMetadataListView, self).get_serializer(*args, **kwargs)
  File "/opt/mayan-edms/lib/python2.7/site-packages/rest_framework/generics.py", line 111, in get_serializer
    kwargs['context'] = self.get_serializer_context()
  File "/opt/mayan-edms/lib/python2.7/site-packages/mayan/apps/metadata/api_views.py", line 75, in get_serializer_context
    'document': self.get_document(),
  File "/opt/mayan-edms/lib/python2.7/site-packages/mayan/apps/metadata/api_views.py", line 47, in get_document
    user=self.request.user
  File "/opt/mayan-edms/lib/python2.7/site-packages/mayan/apps/acls/managers.py", line 229, in check_access
    permission=permission, queryset=source_queryset, user=user
  File "/opt/mayan-edms/lib/python2.7/site-packages/mayan/apps/acls/managers.py", line 245, in restrict_queryset
    permissions=(permission,), user=user
  File "/opt/mayan-edms/lib/python2.7/site-packages/mayan/apps/permissions/classes.py", line 78, in check_user_permissions
    if permission.stored_permission.user_has_this(user=user):
  File "/opt/mayan-edms/lib/python2.7/site-packages/mayan/apps/permissions/models.py", line 79, in user_has_this
    if Role.objects.filter(groups__user=user, permissions=self).exists():
  File "/opt/mayan-edms/lib/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/opt/mayan-edms/lib/python2.7/site-packages/django/db/models/query.py", line 787, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/opt/mayan-edms/lib/python2.7/site-packages/django/db/models/query.py", line 805, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/opt/mayan-edms/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1250, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/opt/mayan-edms/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1276, in _add_q
    allow_joins=allow_joins, split_subq=split_subq,
  File "/opt/mayan-edms/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1206, in build_filter
    condition = lookup_class(lhs, value)
  File "/opt/mayan-edms/lib/python2.7/site-packages/django/db/models/lookups.py", line 24, in __init__
    self.rhs = self.get_prep_lookup()
  File "/opt/mayan-edms/lib/python2.7/site-packages/django/db/models/fields/related_lookups.py", line 112, in get_prep_lookup
    self.rhs = target_field.get_prep_value(self.rhs)
  File "/opt/mayan-edms/lib/python2.7/site-packages/django/db/models/fields/__init__.py", line 966, in get_prep_value
    return int(value)
TypeError: int() argument must be a string or a number, not 'AnonymousUser'
Attachments
workflow_state_action_1.png
workflow_state_action_1.png (62.2 KiB) Viewed 276 times

User avatar
rosarior
Posts: 406
Joined: Tue Aug 21, 2018 3:28 am

Re: Workflow state action: perform a POST request

Post by rosarior » Mon Oct 28, 2019 12:21 am

This is an interesting solution! Two pieces are missing for it to work:

1- Avoid errors when testing for permissions for no authenticated users.
2- Add fields to allow adding authentication credentials to the POST action.

The first was just fixed now: https://gitlab.com/mayan-edms/mayan-edm ... fc5be491ed
We are discussing adding three fields to the HTTP POST workflow action: username, password, headers.

Thanks for the post, this is a great idea, to have Mayan call its own API from the workflow engine! We'll do our best to add the missing parts to make this work.

qra
Posts: 2
Joined: Fri Oct 25, 2019 9:08 pm

Re: Workflow state action: perform a POST request

Post by qra » Mon Oct 28, 2019 6:31 am

Great message. Thanks a lot :D

Post Reply