Auto-categorize documents based on document name

Questions, comments, discussions. Over time certain topics might be moved to their own category.
Post Reply
alexschlueter
Posts: 2
Joined: Wed Jun 24, 2020 11:55 am

Auto-categorize documents based on document name

Post by alexschlueter »

Hi, I am thinking about using the Scanbot Android app to upload documents. I believe the easiest way to categorize documents would be to use a naming scheme such as "id - cabinet - tag1, tag2, tag3.pdf" and have Mayan auto-categorize the documents in the watch folder based on their names.

However, I haven't found a way to do this in Mayan. Am I missing something? Things I have looked into:
  • Workflows: Unfortunately, I don't see a way to have the "Add to cabinets" / "Attach a tag" actions be dependent on the document name, which means I would have to manually add different actions for every tag and cabinet.
  • Indexes: I don't think the Django templating language supports splitting a string, so I can't do {{ document.label.split(",") }}
I'm thinking about adding a custom "split" filter to use in templates, however I would have to modify the Docker image to do that. Where in the mayan-edms folder would I have to put my python file with custom filters?

Thank you for your help!
jorgeegomez
Posts: 2
Joined: Fri Mar 29, 2019 11:45 pm

Re: Auto-categorize documents based on document name

Post by jorgeegomez »

Haven't tried this, but you can find the existing filters in

Code: Select all

.../site-packages/django/template/defaultfilters.py
And here's Django documentation on adding your own filters.

In the case of split, you would probably want to define a split_filter function in order not to clash with python's split method. (see the slice_filter function in the file I mentioned).

If you get it working, please post your solution here.
alexschlueter
Posts: 2
Joined: Wed Jun 24, 2020 11:55 am

Re: Auto-categorize documents based on document name

Post by alexschlueter »

Hi, I actually did get this to work by writing a custom django app for mayan. I've uploaded the source code to github:

https://github.com/alexschlueter/mayan-custom-app

This app does a couple of other things as well, I'll explain the relevant parts for the problem described in the original post here:

The templatetags directory contains a file custom_filters.py with a split filter (as you suggested). In a template field in mayan, you could then (for example) type

Code: Select all

{% load custom_filters %}
{{ document.label | split:"-" }}
to split the document label at minus signs.

Unfortunately, I found this difficult to use together with indices, since an index template is only allowed to return a single string on each tree level, while a document could have multiple keywords in the label (which would correspond to multiple tags).

For this reason, I settled on writing a custom workflow action in my app called "Attach multiple tags". This action is defined in the file "workflow_actions.py" and automatically appears in the mayan action menu when you add my custom app to mayan.
When you add this action to a workflow state, you can enter a django template which returns a comma separated list of tags to add to the document.

For example, you could add the "Attach multiple tags" action to a workflow state with the following django template in the "Tags" field:

Code: Select all

{% load custom_filters %}
{% with parts=document.label|split:"." %}
{{ parts.0|split:None|join:"," }}
{% endwith %}
If your document has the label "tag1 tag2 tag3.pdf", this template would return the string "tag1,tag2,tag3". The "Attach multiple tags" action would now attach the tags "tag1", "tag2" and "tag3" to your document.
Post Reply