Add migration function for custom document model
This commit is contained in:
parent
cc9efd216b
commit
ea67b8b64c
|
|
@ -0,0 +1,14 @@
|
|||
from django.core.management import BaseCommand
|
||||
|
||||
from books.migrate_document_model import migrate_documents_to_custom_document_model
|
||||
from books.models import Module
|
||||
from surveys.models import Survey
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
def handle(self, *args, **options):
|
||||
self.stdout.write("Migrating Wagtail documents to Custommyskillbox documents")
|
||||
migrate_documents_to_custom_document_model()
|
||||
self.stdout.write("Finish migration")
|
||||
|
||||
|
||||
|
|
@ -1,9 +1,37 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Iterativ GmbH
|
||||
# http://www.iterativ.ch/
|
||||
#
|
||||
# Copyright (c) 2015 Iterativ GmbH. All rights reserved.
|
||||
#
|
||||
# Created on 2022-07-28
|
||||
# @author: lorenz.padberg@iterativ.ch
|
||||
from wagtail.documents.models import Document
|
||||
|
||||
from .models.custom_document import CustomDocument
|
||||
|
||||
|
||||
def migrate_documents_to_custom_document_model(dryrun=False):
|
||||
all_documents = Document.objects.all()
|
||||
print(f"Found {all_documents.count()} Documents")
|
||||
|
||||
for document in all_documents:
|
||||
tags = document.title.replace(':', '').split(' ')
|
||||
|
||||
if dryrun:
|
||||
print('')
|
||||
print(document.title)
|
||||
print(tags)
|
||||
print(document.created_at)
|
||||
print(document.url)
|
||||
print(document.file)
|
||||
print(document.uploaded_by_user)
|
||||
|
||||
else:
|
||||
print('')
|
||||
new_document, created = CustomDocument.objects.get_or_create(title=document.title)
|
||||
new_document.display_text = document.title
|
||||
new_document.created_at = document.created_at
|
||||
new_document.uploaded_by_user = document.uploaded_by_user
|
||||
new_document.file = document.file
|
||||
new_document.collection = document.collection
|
||||
new_document.file_hash = document.file_hash
|
||||
new_document.file_size = document.file_size
|
||||
|
||||
new_document.save()
|
||||
if created:
|
||||
print(f'Created New Document {new_document}')
|
||||
else:
|
||||
print(f'Udated document {document}')
|
||||
|
|
|
|||
|
|
@ -1,9 +1,29 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Iterativ GmbH
|
||||
# http://www.iterativ.ch/
|
||||
#
|
||||
# Copyright (c) 2015 Iterativ GmbH. All rights reserved.
|
||||
#
|
||||
# Created on 2022-08-02
|
||||
# @author: lorenz.padberg@iterativ.ch
|
||||
from django.test import TestCase
|
||||
from wagtail.documents.models import Document
|
||||
|
||||
from books.migrate_document_model import migrate_documents_to_custom_document_model
|
||||
from books.models.custom_document import CustomDocument
|
||||
|
||||
TITLE = 'T9 M2 A2: Quellencheck'
|
||||
|
||||
|
||||
class NewContentBlockMutationTest(TestCase):
|
||||
def setUp(self):
|
||||
old_document, created = Document.objects.get_or_create(title=TITLE, file='whatever_is_green.pdf')
|
||||
migrate_documents_to_custom_document_model()
|
||||
|
||||
def test_migrate_creates_new_document(self):
|
||||
self.assertEqual(Document.objects.all().count(), 1)
|
||||
self.assertEqual(Document.objects.filter(title=TITLE).count(), 1)
|
||||
self.assertEqual(CustomDocument.objects.filter(title=TITLE).count(), 1)
|
||||
|
||||
def test_migrate_display_title_migration(self):
|
||||
new_document = CustomDocument.objects.get(title=TITLE)
|
||||
self.assertEqual(new_document.display_text, TITLE)
|
||||
|
||||
def test_migration_is_indempodent(self):
|
||||
migrate_documents_to_custom_document_model()
|
||||
migrate_documents_to_custom_document_model()
|
||||
self.assertEqual(Document.objects.all().count(), 1)
|
||||
self.assertEqual(Document.objects.filter(title=TITLE).count(), 1)
|
||||
self.assertEqual(CustomDocument.objects.filter(title=TITLE).count(), 1)
|
||||
|
|
|
|||
Loading…
Reference in New Issue