Refactor: removal of the unused GPG code and tests coverage added for migration
This commit is contained in:
parent
27cf302db4
commit
be6a42b0e5
@ -1,74 +1,36 @@
|
|||||||
# Generated by Django 4.2.16 on 2024-09-21 15:44
|
# Generated by Django 4.2.16 on 2024-09-21 15:44
|
||||||
|
import os
|
||||||
import datetime
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
import pikepdf
|
import pikepdf
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db import migrations
|
from django.db import migrations
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils import timezone
|
|
||||||
from django.utils.termcolors import colorize as colourise
|
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:
|
return os.path.join(settings.ORIGINALS_DIR, fname)
|
||||||
"""
|
|
||||||
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()
|
|
||||||
|
|
||||||
|
|
||||||
def add_number_of_pages_to_pages_count(apps, schema_editor):
|
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
|
return
|
||||||
|
|
||||||
for doc in documentModel.objects.filter(mime_type="application/pdf"):
|
for doc in Document.objects.filter(mime_type="application/pdf"):
|
||||||
document = Document(doc)
|
|
||||||
|
|
||||||
print(
|
print(
|
||||||
" {} {} {}".format(
|
" {} {} {}".format(
|
||||||
colourise("*", fg="green"),
|
colourise("*", fg="green"),
|
||||||
colourise("Calculating number of pages for", fg="white"),
|
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:
|
if pdf.pages is not None:
|
||||||
doc.pages_count = len(pdf.pages)
|
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):
|
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
|
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.pages_count = 0
|
||||||
document.save()
|
document.save()
|
||||||
|
|
||||||
|
58
src/documents/tests/test_migration_document_pages_count.py
Normal file
58
src/documents/tests/test_migration_document_pages_count.py
Normal file
@ -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()],
|
||||||
|
)
|
Loading…
x
Reference in New Issue
Block a user