Add app creation chapter to documentation
Created by: rosarior
Imported comments:
By yitjob on 2015-05-25 19:09:06 UTC
I propose this to address this issue:
If you need to access the document right after is uploaded:
Create an app like this:
1.- Create a directory inside mayan/apps for your new app. 2.- Create an empty init.py and a models.py file with the line:
from django.db import models
#this is so Django recognizes this directory as a valid Django app.
3.- Tie your code to the post_version_upload signal by adding this to the models.py file of your app, this signal fires when a new document is uploaded:
# -*- coding: utf-8 -*-
import logging
from django.db import models
from django.dispatch import receiver
from django.core.files import File
from documents.signals import post_version_upload
@receiver(post_version_upload, dispatch_uid='my_doc_handler')
def my_doc_handler(sender, instance, **kwargs):
logger = logging.getLogger('common')
new_version = kwargs['new_version']
"""
new_version is a tuple that contains:
new_version[0]: a DocumentVersion object of the recently uploaded file
new_version[1]: a File object that is the uploaded one, opened from the shared path.
"""
#Here you can do stuff to suit your needs. For example: open, process and save as a different file in the shared uploads folder.
new_doc_shared_path = new_version[1].file.path
new_processed_version_path = new_doc_shared_path[:new_doc_shared_path.rfind('.')] + '-processed.xls' #xls as an example if you processed a spreadsheet with openpyxl or xl[rd|wt]
save_in_filesystem(new_processed_version_path)
#now save it as a new version of the original Mayan document just uploaded
with open(new_processed_version_path, 'rb') as f:
doc = new_version[0].document
mf = File(f)
version = doc.new_version(file_object=mf, comment='This is an automatic version')
#It would be possible to generate a different document.
#To do that, instantiate a documents.managers.DocumentManager object and generate it with the new_document function.
4.- Add you app to INSTALLED_APPS in mayan/settings/base.py and your app's urls.py file to mayan/urls.py
If you need views, forms, or links
You also must add the ui components by the following steps:
5.- To register a view, create a normal views.py, urls.py. Then create a file named links.py and add a dictionary for every link you want to add:
link_my_app_link = {'text': _('Document summary'), 'view': 'myapp:my_view'}
6.- In you init.py file add:
from navigation.api import register_top_menu
from .links import link_my_app_link
register_top_menu(name='my_app', link=link_my_app_link)
@rosarior For this procedure to take effect, I also needed to modify two files (pull request https://github.com/mayan-edms/mayan-edms/pull/193):
1.- Edit sources.models in lines 73-77 so they receive a tuple (DocumentVersion, File) instead the DocumentVersion: 73: new_version[0].apply_default_transformations(transformations) post_version_upload.send(sender=self.class, instance=self, user=user, new_version=new_version)
if metadata_dict_list:
save_metadata_list(metadata_dict_list, new_version[0].document, create=True)
2.- Edit documents.managers so the new_document function returns this tuple
By yitjob on 2015-05-25 19:10:50 UTC
Btw I see that I missed to log the operation at the end.