From 80e12b2020a6ecd78b9d66ee5a57270a7f1b9cfb Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Tue, 10 May 2016 19:56:20 -0400 Subject: [PATCH] Create a sample document index after the initial setup. GitLab issue #275. --- mayan/apps/document_indexing/apps.py | 10 ++++++-- mayan/apps/document_indexing/handlers.py | 30 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/mayan/apps/document_indexing/apps.py b/mayan/apps/document_indexing/apps.py index 3e0bf296ea..ec9d1e8ee6 100644 --- a/mayan/apps/document_indexing/apps.py +++ b/mayan/apps/document_indexing/apps.py @@ -15,6 +15,7 @@ from common import ( menu_setup, menu_tools ) from common.classes import Package +from common.signals import post_initial_setup from common.widgets import two_state_template from documents.signals import post_document_created from mayan.celery import app @@ -22,8 +23,9 @@ from navigation import SourceColumn from rest_api.classes import APIEndPoint from .handlers import ( - document_created_index_update, document_index_delete, - document_metadata_index_update, document_metadata_index_post_delete + document_created_index_update, create_default_document_index, + document_index_delete, document_metadata_index_update, + document_metadata_index_post_delete ) from .links import ( link_document_index_list, link_index_main_menu, link_index_setup, @@ -232,6 +234,10 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. document_created_index_update, dispatch_uid='document_created_index_update', sender=Document ) + post_initial_setup.connect( + create_default_document_index, + dispatch_uid='create_default_document_index' + ) post_save.connect( document_metadata_index_update, dispatch_uid='document_metadata_index_update', diff --git a/mayan/apps/document_indexing/handlers.py b/mayan/apps/document_indexing/handlers.py index 77322cfb65..eadc8fbceb 100644 --- a/mayan/apps/document_indexing/handlers.py +++ b/mayan/apps/document_indexing/handlers.py @@ -1,8 +1,38 @@ from __future__ import unicode_literals +from django.apps import apps +from django.utils.translation import ugettext_lazy as _ + from .tasks import task_delete_empty_index_nodes, task_index_document +def create_default_document_index(sender, **kwargs): + DocumentType = apps.get_model( + app_label='documents', model_name='DocumentType' + ) + Index = apps.get_model( + app_label='document_indexing', model_name='Index' + ) + + if not Index.objects.count(): + index = Index.objects.create( + label=_('Creation date'), slug='creation_date' + ) + for document_type in DocumentType.objects.all(): + index.document_types.add(document_type) + + root_template_node = index.template_root + node = root_template_node.get_children().create( + expression='{{ document.date_added|date:"Y" }}', index=index, + parent=root_template_node + ) + node.get_children().create( + expression='{{ document.date_added|date:"m" }}', + index=index, link_documents=True, parent=node + ) + + + def document_created_index_update(sender, **kwargs): task_index_document.apply_async( kwargs=dict(document_id=kwargs['instance'].pk)