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 »

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 1825 times

User avatar
rosarior
Developer
Developer
Posts: 522
Joined: Tue Aug 21, 2018 3:28 am
Location: Puerto Rico
Contact:

Re: Workflow state action: perform a POST request

Post by rosarior »

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 »

Great message. Thanks a lot :D

User avatar
rosarior
Developer
Developer
Posts: 522
Joined: Tue Aug 21, 2018 3:28 am
Location: Puerto Rico
Contact:

Re: Workflow state action: perform a POST request

Post by rosarior »

Version 3.3 (and 3.3.1, 3.3.2) is out and includes support for authentication of the HTTP POST workflow action. Give it a try it should work as you expect.

utka
Posts: 1
Joined: Fri Mar 20, 2020 6:18 pm

Re: Workflow state action: perform a POST request

Post by utka »

Hi,
I tried this with the new Mayan version, but with payload
{
"metadata_type_pk":1,
"value":{{ document.label|slice :"15:2" }}
}
I always get error
WorkflowStateActionError; payload template error: slice requires 2 arguments, 1 provided

I am new to Mayan and Django, testing it.
I wanted to add part of the document name as metadata to the document in a workflow.

Thx for any help

gtrot
Posts: 13
Joined: Tue Oct 01, 2019 9:03 pm

Re: Workflow state action: perform a POST request

Post by gtrot »

Hi,

There seems to be an unwanted whitespace character before the colon.
Also, try to put the Django model in quotes (not sure about this!).

Code: Select all

"value":"{{ document.label|slice:"15:2" }}"
I'm new to Django too, but there seems to be an error with the slicing code. I suggest you look at this page for explanations at slicing: https://www.webucator.com/how-to/how-sl ... python.cfm.

Extract :

Code: Select all

phrase = "Monty Python"

first_5_letters = phrase[0:5]
#[Monty] Python
print(first_5_letters)

letters_2_thru_4 = phrase[1:4]
#M[ont]y Python
print(letters_2_thru_4)

letter_5_to_end = phrase[4:]
#Mont[y Python]
print(letter_5_to_end)

last_3_letters = phrase[-3:]
#Monty Pyt[hon]
print(last_3_letters)

first_3_letters = phrase[:3]
#[Mon]ty Python
print(first_3_letters)

three_letters_before_last = phrase[-4:-1]
#Monty Py[tho]n
print(three_letters_before_last)

copy_of_string = phrase[:]
#[Monty Python]
print(copy_of_string)
Regards,

Guillaume

Post Reply