Documents now have their own dedicated DocumentPage submodel. The old DocumentPage is now called DocumentVersionPage. This allows mappings between document pages and document version pages, allowing renumbering, appending pages. DocumentPages have a content_object to map them to any other object. For now they only map to DocumentVersionPages. New option added to the version upload form to append the pages of the new version. A new view was added to just append new pages with wraps the new document version upload form and hides the append pages checkbox set to True. Add a new action, reset_pages to reset the pages of the document to those of the latest version. Missing: appending tests, checks for proper content_object in OCR and document parsing. Author: Roberto Rosario <roberto.rosario@mayan-edms.com> Date: Thu Oct 11 12:00:25 2019 -0400
63 lines
2.0 KiB
Python
63 lines
2.0 KiB
Python
from __future__ import unicode_literals
|
|
|
|
from django.db import migrations
|
|
|
|
|
|
def get_latest_version(document):
|
|
return document.versions.order_by('timestamp').last()
|
|
|
|
|
|
def operation_reset_document_pages(apps, schema_editor):
|
|
Document = apps.get_model(app_label='documents', model_name='Document')
|
|
DocumentPage = apps.get_model(
|
|
app_label='documents', model_name='DocumentPage'
|
|
)
|
|
|
|
# Define inside the function to use the migration's apps instance
|
|
def pages_reset(document):
|
|
ContentType = apps.get_model('contenttypes', 'ContentType')
|
|
DocumentVersionPage = apps.get_model(
|
|
app_label='documents', model_name='DocumentVersionPage'
|
|
)
|
|
|
|
content_type = ContentType.objects.get_for_model(
|
|
model=DocumentVersionPage
|
|
)
|
|
|
|
for document_page in document.pages.all():
|
|
document_page.delete()
|
|
|
|
for version_page in get_latest_version(document=document).pages.all():
|
|
document_page = document.pages.create(
|
|
content_type=content_type,
|
|
page_number=version_page.page_number,
|
|
object_id=version_page.pk,
|
|
)
|
|
|
|
for document in Document.objects.using(schema_editor.connection.alias).all():
|
|
pages_reset(document=document)
|
|
|
|
|
|
def operation_reset_document_pages_reverse(apps, schema_editor):
|
|
Document = apps.get_model(app_label='documents', model_name='Document')
|
|
DocumentPage = apps.get_model(
|
|
app_label='documents', model_name='DocumentPage'
|
|
)
|
|
|
|
for document in Document.objects.using(schema_editor.connection.alias).all():
|
|
for document_page in document.pages.all():
|
|
document_page.delete()
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
dependencies = [
|
|
('documents', '0053_create_document_page_and_result_models'),
|
|
]
|
|
|
|
operations = [
|
|
migrations.RunPython(
|
|
code=operation_reset_document_pages,
|
|
reverse_code=operation_reset_document_pages_reverse
|
|
),
|
|
]
|