Automatically Calculate Document Number

210 views
Skip to first unread message

Rachael Sewell

unread,
Dec 18, 2015, 1:34:13 PM12/18/15
to Mayan EDMS
Hello,

I'd like to know if there is already a way to add an automatically created document number to a document. For example I would like to start with a initial document number of AN00001 and increment (AN00002, AN00003) for each additional doc. Does this functionality already exist? If not could it be added to the core or would it need to be a custom application? Any pointers on how to get started on this would be helpful as well. 

Thanks,
Rachael

Roberto Rosario

unread,
Dec 19, 2015, 12:06:34 PM12/19/15
to Mayan EDMS
That capacity is not available. I would suggest creating a custom app first and then it could be included in core once stable. To keep track of the numbering there would need to be a Sequence model that keeps track of the next increment in a persistent manner and templating support to build something like 'AN{{ sequence1|leading_zeros:"5" }}' for each document type. The renaming will be connected to the documents.signals.post_document_created signal so that it happens only once and not for each document version. If you have a GitLab account I can create the app repository and initial code in the Mayan EDMS GitLab group. Development chapters for reference: http://mayan.readthedocs.org/en/latest/topics/app_creation.html and http://mayan.readthedocs.org/en/latest/topics/development.html

Roberto Rosario

unread,
Dec 20, 2015, 3:02:15 AM12/20/15
to Mayan EDMS

Hello,

I've pushed initial code to implement this as a separate app here: https://gitlab.com/mayan-edms/document_renaming. Separate sequences can be created with different increments. Renaming templates are then created which reference existing sequences. A renaming template is associated with a document type. Everytime a new document is uploaded the renaming template is rendered and the result is applied a the label for the new documents.  Still missing: Locking to avoid repeated sequence numbers and proper setup views. If you can provide a GitLab account I will add developer permissions for it for the repository.







On Friday, December 18, 2015 at 2:34:13 PM UTC-4, Rachael Sewell wrote:

Roberto Rosario

unread,
Dec 20, 2015, 11:22:51 PM12/20/15
to Mayan EDMS
Proper UI integration added with views, links and permissions. Only locking issue remains to solve.
Screenshot from 2015-12-21 00-19-25.png
Screenshot from 2015-12-21 00-19-35.png
Screenshot from 2015-12-21 00-19-46.png
Screenshot from 2015-12-21 00-20-07.png
Screenshot from 2015-12-21 00-21-28.png

Rachael Sewell

unread,
Dec 21, 2015, 12:27:46 PM12/21/15
to Mayan EDMS
Roberto,

Thanks so much for getting that started. I have a gitlab account. My username is rachaelmsewell:

Kind regards,
Rachael

Rachael Sewell

unread,
Jan 5, 2016, 5:31:23 PM1/5/16
to Mayan EDMS
Hi Roberto,

I cloned the repository outside of my mayan-edms repo and created a symbolic link to the directory in my mayan/apps directory. The directions in the gitlab account specify:

In your settings/local.py file add document_renaming to your INSTALLED_APPS list:

INSTALLED_APPS += (
    'document_renaming',
)
Should that actually read mayan/settings/base.py?

I have added the application to the base.py file and run migrations. However, I still don't see this application added in the admin dashboard.
Rachael

On Sunday, December 20, 2015 at 8:22:51 PM UTC-8, Roberto Rosario wrote:

Roberto Rosario

unread,
Jan 7, 2016, 5:04:19 AM1/7/16
to Mayan EDMS
Hi,

run the command:

./manage.py migrate document_renaming  --list

it should show:

document_renaming
 [X] 0001_initial
 [X] 0002_sequence_increment
 [X] 0003_auto_20151220_0717
 [X] 0004_auto_20151220_0751
 [X] 0005_auto_20151221_0402
 [X] 0006_auto_20151221_0402


If the output is as shown above the app was enabled and migrate, any other output means the app is not being recognized. Be sure to add the lines:


INSTALLED_APPS += (
    'document_renaming',
)

At the end of the settings/local.py.

Rachael Sewell

unread,
Jan 7, 2016, 12:52:17 PM1/7/16
to mayan...@googlegroups.com
Roberto,

I had added this to the mayan/settings/base.py file:
INSTALLED_APPS += (
    'document_renaming',
)

and this is the output I got when I ran ./manage.py migrate document_renaming —list

/mayan-edms-repository/venv/local/lib/python2.7/site-packages/django/contrib/comments/__init__.py:13: RemovedInDjango18Warning: django.contrib.comments is deprecated and will be removed before Django 1.8.
  warnings.warn("django.contrib.comments is deprecated and will be removed before Django 1.8.", RemovedInDjango18Warning)

common.apps <534> [DEBUG] "ready() App mimetype doesn't have URLs defined."
common.apps <534> [DEBUG] "ready() App navigation doesn't have URLs defined."
smart_settings.classes <534> [DEBUG] "initialize() App appearance has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App suit has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App django.contrib.admin has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App django.contrib.admindocs has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App django.contrib.auth has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App django.contrib.comments has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App django.contrib.contenttypes has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App django.contrib.messages has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App django.contrib.sessions has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App django.contrib.sites has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App django.contrib.staticfiles has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() Imported settings.py file for app actstream"
smart_settings.classes <534> [DEBUG] "initialize() Imported settings.py file for app autoadmin"
smart_settings.classes <534> [DEBUG] "initialize() App colorful has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App compressor has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App corsheaders has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App djcelery has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App filetransfers has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() Imported settings.py file for app mptt"
smart_settings.classes <534> [DEBUG] "initialize() App pure_pagination has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() Imported settings.py file for app rest_framework"
smart_settings.classes <534> [DEBUG] "initialize() App rest_framework.authtoken has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() Imported settings.py file for app solo"
smart_settings.classes <534> [DEBUG] "initialize() App widget_tweaks has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App acls has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() Imported settings.py file for app authentication"
smart_settings.classes <534> [DEBUG] "initialize() Imported settings.py file for app common"
smart_settings.classes <534> [DEBUG] "initialize() Imported settings.py file for app converter"
smart_settings.classes <534> [DEBUG] "initialize() Imported settings.py file for app django_gpg"
smart_settings.classes <534> [DEBUG] "initialize() Imported settings.py file for app dynamic_search"
smart_settings.classes <534> [DEBUG] "initialize() Imported settings.py file for app lock_manager"
smart_settings.classes <534> [DEBUG] "initialize() App mimetype has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App navigation has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App permissions has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App smart_settings has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App user_management has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App checkouts has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App document_comments has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App document_indexing has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App document_renaming has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() Imported settings.py file for app document_signatures"
smart_settings.classes <534> [DEBUG] "initialize() App document_states has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() Imported settings.py file for app documents"
smart_settings.classes <534> [DEBUG] "initialize() App events has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App folders has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App installation has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App linking has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() Imported settings.py file for app mailer"
smart_settings.classes <534> [DEBUG] "initialize() Imported settings.py file for app metadata"
smart_settings.classes <534> [DEBUG] "initialize() Imported settings.py file for app mirroring"
smart_settings.classes <534> [DEBUG] "initialize() Imported settings.py file for app ocr"
smart_settings.classes <534> [DEBUG] "initialize() App rest_api has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App sources has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App statistics has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() Imported settings.py file for app storage"
smart_settings.classes <534> [DEBUG] "initialize() App tags has no settings.py file"
smart_settings.classes <534> [DEBUG] "initialize() App rest_framework_swagger has no settings.py file"
document_renaming
 [X] 0001_initial
 [X] 0002_sequence_increment
 [X] 0003_auto_20151220_0717
 [X] 0004_auto_20151220_0751
 [X] 0005_auto_20151221_0402
 [X] 0006_auto_20151221_0402


I just want to confirm whether I add the installed app to mayan/settings/base.py, mayan/settings/development.py, or in /venv/lib/python2.7/site-packages/mayan/settings/local.py.


Thanks for the help,
Rachael







--

---
You received this message because you are subscribed to a topic in the Google Groups "Mayan EDMS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mayan-edms/HRy_us-jp10/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mayan-edms+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rachael Sewell

unread,
Jan 7, 2016, 2:17:32 PM1/7/16
to mayan...@googlegroups.com
Roberto,

Disregard my previous email. I started with a fresh VM installation and added the new renaming app. I must have had an installation where the initial setup was not executed properly because now I see the local.py file in the settings directory. I added the new app and migrated. I now see the new sequences item in the admin view.

Rachael

Rachael Sewell

unread,
Jan 8, 2016, 3:52:07 PM1/8/16
to Mayan EDMS
Roberto,

I'm working with this app currently. I have two document types:
  1. Specs 
  2. Memos
Memos will be renamed according to the logged in user. I would like to create a number like this <username>-<incremental number>. For example: "rachael.sewell-00"

I've tried a few things, but it seems the user is not accessible in the view. Any pointers to where I should be updating code would be helpful to add this. Thanks for the continued help.

Rachael

Roberto Rosario

unread,
Jan 12, 2016, 4:30:02 AM1/12/16
to Mayan EDMS
The UI, document upload and document renamer execute in different contexts, it is not possible for them to share the web session. The alternative would be to access the event log of the document and extract the first entry which would indicate the user who created the document and expose this to the context of the renaming template here: https://gitlab.com/mayan-edms/document_renaming/blob/master/document_renaming/models.py#L80

The even log app uses Django activity stream (https://github.com/justquick/django-activity-stream/blob/master/actstream/models.py) and they provide a reverse reference. Something like

document.target_actions.first().actor

should give your the user instance.

The second problem would be to get the user's sequence dynamically. Sequences are passed to the rename template's context as 'sequence_XXX' variables because Django's template engine doesn't allow parameter method calling.

In my mind the easiest path would be to ditch Django's template engine and use something like Jinja2 (http://jinja.pocoo.org/) which allows method calls with parameters and the renaming template itself can contain all the required logic:

{% set username = document.target_actions.first.actor.username %}  {{ username }}-{{ Sequence.objects.get(slug=%username%).next_value() }}

Provided you have already the sequences created for each use. This is all from the top of my head without any actual test of course :)

Roberto Rosario

unread,
Jan 12, 2016, 4:37:22 AM1/12/16
to Mayan EDMS
Did a quick test and with Jinja2 it is possible:

from jinja2 import Template
from document_renaming.models import Sequence

document=Document.objects.first()

Template('{% set user = document.target_actions.first().actor %}{{user}}-{{ Sequence.objects.get(slug=user).next_value() }}').render(document=document, Sequence=Sequence)


outputs:

admin-1

it is even possible to create the user sequences from the template (using get_or_create):


Template('{% set user = document.target_actions.first().actor %}{{user}}-{{ Sequence.objects.get_or_create(slug=user)[0].next_value() }}').render(document=document, Sequence=Sequence)

Rachael Sewell

unread,
Jan 12, 2016, 1:59:26 PM1/12/16
to mayan...@googlegroups.com
Hi Roberto,

I’m still a little confused. Do I add the following to the document_renaming model or the document model?

from jinja2 import Template
from document_renaming.models import Sequence

document=Document.objects.first()

Did you add the following code as the renaming template via the web interface:

Template('{% set user = document.target_actions.first().actor %}{{user}}-{{ Sequence.objects.get(slug=user).next_value() }}').render(document=document, Sequence=Sequence)


Kind regards,
Rachael

Roberto Rosario

unread,
Jan 13, 2016, 2:47:35 AM1/13/16
to Mayan EDMS
Sorry about that. The code was just a test using the Python shell. I've updated the app code (https://gitlab.com/mayan-edms/document_renaming/commit/b09bf4a8dadf381cd8b54cb5f452119db8173a4f) and now sequences can be created automatically with a rename template like this:

{% set username = document.target_actions.first().actor.username %}{{ username }}-{{ "%05d"|format(Sequence.objects.get_or_create(slug=username,label=username)[0].next_value()) }}


The first part extracts the document event log, gets the first entry and stores the actor's username string in a variable called username.
The second part renders the username wih a dash.
The final part tries to the sequence with the username and if it is not available creates it. Since get_or_created returns two values (a tuple), the actual sequence instance and a boolean if it was newly created, only the first value is retained using [0]. get_next() if called to get the value and advance the sequence. And everything is send as an argument to the 'format' template tag which behaves like Python's .format() and padded to 5 zeros.

Here are the tests for the new functionality: https://gitlab.com/mayan-edms/document_renaming/blob/master/document_renaming/tests/test_models.py#L104
If no new bugs are discovered release 2 can be done in the following days. Hope this helps.




On Tuesday, January 12, 2016 at 2:59:26 PM UTC-4, Rachael Sewell wrote:
Hi Roberto,

I’m still a little confused. Do I add the following to the document_renaming model or the document model?

from jinja2 import Template
from document_renaming.models import Sequence

document=Document.objects.first()

Did you add the following code as the renaming template via the web interface:

Template('{% set user = document.target_actions.first().actor %}{{user}}-{{ Sequence.objects.get(slug=user).next_value() }}').render(document=document, Sequence=Sequence)


Kind regards,
Rachael

To unsubscribe from this group and all its topics, send an email to mayan-edms+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages