From be6a42b0e5d4d26b9bcde663ddf1433fec87768e Mon Sep 17 00:00:00 2001 From: s0llvan <178677095+s0llvan@users.noreply.github.com> Date: Sun, 22 Sep 2024 12:18:34 +0000 Subject: [PATCH] Refactor: removal of the unused GPG code and tests coverage added for migration --- .../migrations/1053_document_pages_count.py | 64 ++++--------------- .../test_migration_document_pages_count.py | 58 +++++++++++++++++ 2 files changed, 71 insertions(+), 51 deletions(-) create mode 100644 src/documents/tests/test_migration_document_pages_count.py diff --git a/src/documents/migrations/1053_document_pages_count.py b/src/documents/migrations/1053_document_pages_count.py index 25210f446..4683d9978 100644 --- a/src/documents/migrations/1053_document_pages_count.py +++ b/src/documents/migrations/1053_document_pages_count.py @@ -1,74 +1,36 @@ # Generated by Django 4.2.16 on 2024-09-21 15:44 - -import datetime -from pathlib import Path +import os import pikepdf from django.conf import settings from django.db import migrations from django.db import models -from django.utils import timezone from django.utils.termcolors import colorize as colourise -from documents.parsers import get_default_file_extension +def source_path(self): + if self.filename: + fname = str(self.filename) -class Document: - """ - Django's migrations restrict access to model methods, so this is a snapshot - of the methods that existed at the time this migration was written, since - we need to make use of a lot of these shortcuts here. - """ - - def __init__(self, doc): - self.pk = doc.pk - self.correspondent = doc.correspondent - self.title = doc.title - self.mime_type = doc.mime_type - self.filename = doc.filename - self.created = doc.created - - def __str__(self) -> str: - # Convert UTC database time to local time - created = datetime.date.isoformat(timezone.localdate(self.created)) - - res = f"{created}" - - if self.correspondent: - res += f" {self.correspondent}" - if self.title: - res += f" {self.title}" - return res - - @property - def file_type(self): - return get_default_file_extension(self.mime_type) - - @property - def source_path(self) -> Path: - if self.filename: - fname = str(self.filename) - return (settings.ORIGINALS_DIR / Path(fname)).resolve() + return os.path.join(settings.ORIGINALS_DIR, fname) def add_number_of_pages_to_pages_count(apps, schema_editor): - documentModel = apps.get_model("documents", "Document") + Document = apps.get_model("documents", "Document") - if not documentModel.objects.all().exists(): + if not Document.objects.all().exists(): return - for doc in documentModel.objects.filter(mime_type="application/pdf"): - document = Document(doc) - + for doc in Document.objects.filter(mime_type="application/pdf"): print( " {} {} {}".format( colourise("*", fg="green"), colourise("Calculating number of pages for", fg="white"), - colourise(document.filename, fg="cyan"), + colourise(doc.filename, fg="cyan"), ), ) - pdf = pikepdf.open(document.source_path) + pdf = pikepdf.open(source_path(doc)) if pdf.pages is not None: doc.pages_count = len(pdf.pages) @@ -76,12 +38,12 @@ def add_number_of_pages_to_pages_count(apps, schema_editor): def remove_number_of_pages_to_pages_count(apps, schema_editor): - documentModel = apps.get_model("documents", "Document") + Document = apps.get_model("documents", "Document") - if not documentModel.objects.all().exists(): + if not Document.objects.all().exists(): return - for document in documentModel.objects.filter(mime_type="application/pdf"): + for document in Document.objects.filter(mime_type="application/pdf"): document.pages_count = 0 document.save() diff --git a/src/documents/tests/test_migration_document_pages_count.py b/src/documents/tests/test_migration_document_pages_count.py new file mode 100644 index 000000000..811633330 --- /dev/null +++ b/src/documents/tests/test_migration_document_pages_count.py @@ -0,0 +1,58 @@ +import os +import shutil + +from django.conf import settings + +from documents.tests.utils import TestMigrations + + +def source_path_before(self): + if self.filename: + fname = str(self.filename) + + return os.path.join(settings.ORIGINALS_DIR, fname) + + +class TestMigrateDocumentPagesCount(TestMigrations): + migrate_from = "1052_document_transaction_id" + migrate_to = "1053_document_pages_count" + + def setUpBeforeMigration(self, apps): + Document = apps.get_model("documents", "Document") + doc = Document.objects.create( + title="test1", + mime_type="application/pdf", + filename="file1.pdf", + ) + self.doc_id = doc.id + shutil.copy( + os.path.join(os.path.dirname(__file__), "samples", "simple.pdf"), + source_path_before(doc), + ) + + def testDocumentPagesCountMigrated(self): + Document = self.apps.get_model("documents", "Document") + + doc = Document.objects.get(id=self.doc_id) + self.assertEqual(doc.pages_count, 1) + + +class TestMigrateDocumentPagesCountBackwards(TestMigrations): + migrate_from = "1053_document_pages_count" + migrate_to = "1052_document_transaction_id" + + def setUpBeforeMigration(self, apps): + Document = apps.get_model("documents", "Document") + doc = Document.objects.create( + title="test1", + mime_type="application/pdf", + filename="file1.pdf", + pages_count=8, + ) + self.doc_id = doc.id + + def test_remove_number_of_pages_to_pages_count(self): + Document = self.apps.get_model("documents", "Document") + self.assertFalse( + "pages_count" in [field.name for field in Document._meta.get_fields()], + )