63 lines
1.7 KiB
Python
63 lines
1.7 KiB
Python
# Generated by Django 4.2.16 on 2024-09-21 15:44
|
|
from pathlib import Path
|
|
|
|
import pikepdf
|
|
from django.conf import settings
|
|
from django.db import migrations
|
|
from django.db import models
|
|
from django.utils.termcolors import colorize as colourise
|
|
|
|
|
|
def source_path(self):
|
|
if self.filename:
|
|
fname = str(self.filename)
|
|
|
|
return Path(settings.ORIGINALS_DIR / fname).resolve()
|
|
|
|
|
|
def add_number_of_pages_to_pages_count(apps, schema_editor):
|
|
Document = apps.get_model("documents", "Document")
|
|
|
|
if not Document.objects.all().exists():
|
|
return
|
|
|
|
for doc in Document.objects.filter(mime_type="application/pdf"):
|
|
print(
|
|
" {} {} {}".format(
|
|
colourise("*", fg="green"),
|
|
colourise("Calculating number of pages for", fg="white"),
|
|
colourise(doc.filename, fg="cyan"),
|
|
),
|
|
)
|
|
|
|
try:
|
|
with pikepdf.Pdf.open(source_path(doc)) as pdf:
|
|
if pdf.pages is not None:
|
|
doc.pages_count = len(pdf.pages)
|
|
doc.save()
|
|
except Exception as e: # pragma: no cover
|
|
print(f"Error retrieving number of pages for {doc.filename}: {e}")
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
dependencies = [
|
|
("documents", "1052_document_transaction_id"),
|
|
]
|
|
|
|
operations = [
|
|
migrations.AddField(
|
|
model_name="document",
|
|
name="pages_count",
|
|
field=models.PositiveIntegerField(
|
|
blank=False,
|
|
null=True,
|
|
unique=False,
|
|
db_index=False,
|
|
),
|
|
),
|
|
migrations.RunPython(
|
|
add_number_of_pages_to_pages_count,
|
|
migrations.RunPython.noop,
|
|
),
|
|
]
|