Add data migration to the file metadata app
Synchronizes the document type settings model of existing document types. Signed-off-by: Roberto Rosario <roberto.rosario@mayan-edms.com>
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
def operation_create_file_metadata_setting_for_existing_document_types(apps, schema_editor):
|
||||
DocumentType = apps.get_model(
|
||||
app_label='documents', model_name='DocumentType'
|
||||
)
|
||||
DocumentTypeSettings = apps.get_model(
|
||||
app_label='file_metadata', model_name='DocumentTypeSettings'
|
||||
)
|
||||
|
||||
for document_type in DocumentType.objects.using(schema_editor.connection.alias).all():
|
||||
try:
|
||||
DocumentTypeSettings.objects.using(
|
||||
schema_editor.connection.alias
|
||||
).get_or_create(document_type=document_type)
|
||||
except DocumentTypeSettings.DoesNotExist:
|
||||
pass
|
||||
|
||||
|
||||
def operation_delete_file_metadata_setting_for_existing_document_types(apps, schema_editor):
|
||||
DocumentType = apps.get_model(
|
||||
app_label='documents', model_name='DocumentType'
|
||||
)
|
||||
DocumentTypeSettings = apps.get_model(
|
||||
app_label='file_metadata', model_name='DocumentTypeSettings'
|
||||
)
|
||||
|
||||
for document_type in DocumentType.objects.using(schema_editor.connection.alias).all():
|
||||
try:
|
||||
DocumentTypeSettings.objects.using(
|
||||
schema_editor.connection.alias
|
||||
).get(document_type=document_type).delete()
|
||||
except DocumentTypeSettings.DoesNotExist:
|
||||
pass
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('documents', '0047_auto_20180917_0737'),
|
||||
('file_metadata', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(
|
||||
code=operation_create_file_metadata_setting_for_existing_document_types,
|
||||
reverse_code=operation_delete_file_metadata_setting_for_existing_document_types,
|
||||
)
|
||||
]
|
||||
Reference in New Issue
Block a user