Refactor pages_count --> page_count
This commit is contained in:
@@ -387,8 +387,8 @@ def delete_pages(doc_ids: list[int], pages: list[int]):
|
||||
pdf.remove_unreferenced_resources()
|
||||
pdf.save()
|
||||
doc.checksum = hashlib.md5(doc.source_path.read_bytes()).hexdigest()
|
||||
if doc.pages_count is not None:
|
||||
doc.pages_count = doc.pages_count - len(pages)
|
||||
if doc.page_count is not None:
|
||||
doc.page_count = doc.page_count - len(pages)
|
||||
doc.save()
|
||||
update_document_archive_file.delay(document_id=doc.id)
|
||||
logger.info(f"Deleted pages {pages} from document {doc.id}")
|
||||
|
||||
@@ -586,7 +586,7 @@ class ConsumerPlugin(
|
||||
date = None
|
||||
thumbnail = None
|
||||
archive_path = None
|
||||
pages_count = None
|
||||
page_count = None
|
||||
|
||||
try:
|
||||
self._send_progress(
|
||||
@@ -622,7 +622,7 @@ class ConsumerPlugin(
|
||||
)
|
||||
date = parse_date(self.filename, text)
|
||||
archive_path = document_parser.get_archive_path()
|
||||
pages_count = document_parser.get_pages_count(self.working_copy, mime_type)
|
||||
page_count = document_parser.get_page_count(self.working_copy, mime_type)
|
||||
|
||||
except ParseError as e:
|
||||
document_parser.cleanup()
|
||||
@@ -667,7 +667,7 @@ class ConsumerPlugin(
|
||||
document = self._store(
|
||||
text=text,
|
||||
date=date,
|
||||
pages_count=pages_count,
|
||||
page_count=page_count,
|
||||
mime_type=mime_type,
|
||||
)
|
||||
|
||||
@@ -797,7 +797,7 @@ class ConsumerPlugin(
|
||||
self,
|
||||
text: str,
|
||||
date: Optional[datetime.datetime],
|
||||
pages_count: Optional[int],
|
||||
page_count: Optional[int],
|
||||
mime_type: str,
|
||||
) -> Document:
|
||||
# If someone gave us the original filename, use it instead of doc.
|
||||
@@ -843,7 +843,7 @@ class ConsumerPlugin(
|
||||
created=create_date,
|
||||
modified=create_date,
|
||||
storage_type=storage_type,
|
||||
pages_count=pages_count,
|
||||
page_count=page_count,
|
||||
original_filename=self.filename,
|
||||
)
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ def get_schema():
|
||||
has_owner=BOOLEAN(),
|
||||
viewer_id=KEYWORD(commas=True),
|
||||
checksum=TEXT(),
|
||||
pages_count=NUMERIC(sortable=True),
|
||||
page_count=NUMERIC(sortable=True),
|
||||
original_filename=TEXT(sortable=True),
|
||||
is_shared=BOOLEAN(),
|
||||
)
|
||||
@@ -182,7 +182,7 @@ def update_document(writer: AsyncWriter, doc: Document):
|
||||
has_owner=doc.owner is not None,
|
||||
viewer_id=viewer_ids if viewer_ids else None,
|
||||
checksum=doc.checksum,
|
||||
pages_count=doc.pages_count,
|
||||
page_count=doc.page_count,
|
||||
original_filename=doc.original_filename,
|
||||
is_shared=len(viewer_ids) > 0,
|
||||
)
|
||||
@@ -249,7 +249,7 @@ class DelayedQuery:
|
||||
"archive_serial_number": "asn",
|
||||
"num_notes": "num_notes",
|
||||
"owner": "owner",
|
||||
"pages_count": "pages_count",
|
||||
"page_count": "page_count",
|
||||
}
|
||||
|
||||
if field.startswith("-"):
|
||||
|
||||
@@ -15,7 +15,7 @@ def source_path(self):
|
||||
return Path(settings.ORIGINALS_DIR / fname).resolve()
|
||||
|
||||
|
||||
def add_number_of_pages_to_pages_count(apps, schema_editor):
|
||||
def add_number_of_pages_to_page_count(apps, schema_editor):
|
||||
Document = apps.get_model("documents", "Document")
|
||||
|
||||
if not Document.objects.all().exists():
|
||||
@@ -33,7 +33,7 @@ def add_number_of_pages_to_pages_count(apps, schema_editor):
|
||||
try:
|
||||
with pikepdf.Pdf.open(source_path(doc)) as pdf:
|
||||
if pdf.pages is not None:
|
||||
doc.pages_count = len(pdf.pages)
|
||||
doc.page_count = len(pdf.pages)
|
||||
doc.save()
|
||||
except Exception as e: # pragma: no cover
|
||||
print(f"Error retrieving number of pages for {doc.filename}: {e}")
|
||||
@@ -47,7 +47,7 @@ class Migration(migrations.Migration):
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="document",
|
||||
name="pages_count",
|
||||
name="page_count",
|
||||
field=models.PositiveIntegerField(
|
||||
blank=False,
|
||||
null=True,
|
||||
@@ -56,7 +56,7 @@ class Migration(migrations.Migration):
|
||||
),
|
||||
),
|
||||
migrations.RunPython(
|
||||
add_number_of_pages_to_pages_count,
|
||||
add_number_of_pages_to_page_count,
|
||||
migrations.RunPython.noop,
|
||||
),
|
||||
]
|
||||
@@ -205,8 +205,8 @@ class Document(SoftDeleteModel, ModelWithOwner):
|
||||
help_text=_("The checksum of the archived document."),
|
||||
)
|
||||
|
||||
pages_count = models.PositiveIntegerField(
|
||||
_("pages count"),
|
||||
page_count = models.PositiveIntegerField(
|
||||
_("page count"),
|
||||
blank=False,
|
||||
null=True,
|
||||
unique=False,
|
||||
@@ -426,7 +426,7 @@ class SavedView(ModelWithOwner):
|
||||
OWNER = ("owner", _("Owner"))
|
||||
SHARED = ("shared", _("Shared"))
|
||||
ASN = ("asn", _("ASN"))
|
||||
PAGES_COUNT = ("pagescount", _("Pages"))
|
||||
PAGE_COUNT = ("pagecount", _("Pages"))
|
||||
CUSTOM_FIELD = ("custom_field_%d", ("Custom Field"))
|
||||
|
||||
name = models.CharField(_("name"), max_length=128)
|
||||
|
||||
@@ -367,7 +367,7 @@ class DocumentParser(LoggingMixin):
|
||||
def extract_metadata(self, document_path, mime_type):
|
||||
return []
|
||||
|
||||
def get_pages_count(self, document_path, mime_type):
|
||||
def get_page_count(self, document_path, mime_type):
|
||||
return None
|
||||
|
||||
def parse(self, document_path, mime_type, file_name=None):
|
||||
|
||||
@@ -750,7 +750,7 @@ class DocumentSerializer(
|
||||
original_file_name = SerializerMethodField()
|
||||
archived_file_name = SerializerMethodField()
|
||||
created_date = serializers.DateField(required=False)
|
||||
pages_count = SerializerMethodField()
|
||||
page_count = SerializerMethodField()
|
||||
|
||||
custom_fields = CustomFieldInstanceSerializer(
|
||||
many=True,
|
||||
@@ -771,8 +771,8 @@ class DocumentSerializer(
|
||||
required=False,
|
||||
)
|
||||
|
||||
def get_pages_count(self, obj):
|
||||
return obj.pages_count
|
||||
def get_page_count(self, obj):
|
||||
return obj.page_count
|
||||
|
||||
def get_original_file_name(self, obj):
|
||||
return obj.original_filename
|
||||
@@ -889,7 +889,7 @@ class DocumentSerializer(
|
||||
"notes",
|
||||
"custom_fields",
|
||||
"remove_inbox_tags",
|
||||
"pages_count",
|
||||
"page_count",
|
||||
)
|
||||
list_serializer_class = OwnedObjectListSerializer
|
||||
|
||||
|
||||
@@ -389,7 +389,7 @@ class TestPDFActions(DirectoriesMixin, TestCase):
|
||||
title="B",
|
||||
filename=sample2,
|
||||
mime_type="application/pdf",
|
||||
pages_count=8,
|
||||
page_count=8,
|
||||
)
|
||||
self.doc2.archive_filename = sample2_archive
|
||||
self.doc2.save()
|
||||
@@ -682,19 +682,19 @@ class TestPDFActions(DirectoriesMixin, TestCase):
|
||||
THEN:
|
||||
- Save should be called once
|
||||
- Archive file should be updated once
|
||||
- The document's pages_count should be reduced by the number of deleted pages
|
||||
- The document's page_count should be reduced by the number of deleted pages
|
||||
"""
|
||||
doc_ids = [self.doc2.id]
|
||||
initial_pages_count = self.doc2.pages_count
|
||||
initial_page_count = self.doc2.page_count
|
||||
pages = [1, 3]
|
||||
result = bulk_edit.delete_pages(doc_ids, pages)
|
||||
mock_pdf_save.assert_called_once()
|
||||
mock_update_archive_file.assert_called_once()
|
||||
self.assertEqual(result, "OK")
|
||||
|
||||
expected_pages_count = initial_pages_count - len(pages)
|
||||
expected_page_count = initial_page_count - len(pages)
|
||||
self.doc2.refresh_from_db()
|
||||
self.assertEqual(self.doc2.pages_count, expected_pages_count)
|
||||
self.assertEqual(self.doc2.page_count, expected_page_count)
|
||||
|
||||
@mock.patch("documents.tasks.update_document_archive_file.delay")
|
||||
@mock.patch("pikepdf.Pdf.save")
|
||||
|
||||
@@ -14,9 +14,9 @@ def source_path_before(self):
|
||||
return os.path.join(settings.ORIGINALS_DIR, fname)
|
||||
|
||||
|
||||
class TestMigrateDocumentPagesCount(TestMigrations):
|
||||
class TestMigrateDocumentPageCount(TestMigrations):
|
||||
migrate_from = "1052_document_transaction_id"
|
||||
migrate_to = "1053_document_pages_count"
|
||||
migrate_to = "1053_document_page_count"
|
||||
|
||||
def setUpBeforeMigration(self, apps):
|
||||
Document = apps.get_model("documents", "Document")
|
||||
@@ -31,15 +31,15 @@ class TestMigrateDocumentPagesCount(TestMigrations):
|
||||
source_path_before(doc),
|
||||
)
|
||||
|
||||
def testDocumentPagesCountMigrated(self):
|
||||
def testDocumentPageCountMigrated(self):
|
||||
Document = self.apps.get_model("documents", "Document")
|
||||
|
||||
doc = Document.objects.get(id=self.doc_id)
|
||||
self.assertEqual(doc.pages_count, 1)
|
||||
self.assertEqual(doc.page_count, 1)
|
||||
|
||||
|
||||
class TestMigrateDocumentPagesCountBackwards(TestMigrations):
|
||||
migrate_from = "1053_document_pages_count"
|
||||
class TestMigrateDocumentPageCountBackwards(TestMigrations):
|
||||
migrate_from = "1053_document_page_count"
|
||||
migrate_to = "1052_document_transaction_id"
|
||||
|
||||
def setUpBeforeMigration(self, apps):
|
||||
@@ -48,12 +48,12 @@ class TestMigrateDocumentPagesCountBackwards(TestMigrations):
|
||||
title="test1",
|
||||
mime_type="application/pdf",
|
||||
filename="file1.pdf",
|
||||
pages_count=8,
|
||||
page_count=8,
|
||||
)
|
||||
self.doc_id = doc.id
|
||||
|
||||
def test_remove_number_of_pages_to_pages_count(self):
|
||||
def test_remove_number_of_pages_to_page_count(self):
|
||||
Document = self.apps.get_model("documents", "Document")
|
||||
self.assertFalse(
|
||||
"pages_count" in [field.name for field in Document._meta.get_fields()],
|
||||
"page_count" in [field.name for field in Document._meta.get_fields()],
|
||||
)
|
||||
|
||||
@@ -361,7 +361,7 @@ class DocumentViewSet(
|
||||
"archive_serial_number",
|
||||
"num_notes",
|
||||
"owner",
|
||||
"pages_count",
|
||||
"page_count",
|
||||
)
|
||||
|
||||
def get_queryset(self):
|
||||
|
||||
@@ -41,14 +41,14 @@ class RasterisedDocumentParser(DocumentParser):
|
||||
"""
|
||||
return OcrConfig()
|
||||
|
||||
def get_pages_count(self, document_path, mime_type):
|
||||
pages_count = None
|
||||
def get_page_count(self, document_path, mime_type):
|
||||
page_count = None
|
||||
if mime_type == "application/pdf":
|
||||
import pikepdf
|
||||
|
||||
with pikepdf.Pdf.open(document_path) as pdf:
|
||||
pages_count = len(pdf.pages)
|
||||
return pages_count
|
||||
page_count = len(pdf.pages)
|
||||
return page_count
|
||||
|
||||
def extract_metadata(self, document_path, mime_type):
|
||||
result = []
|
||||
|
||||
@@ -57,7 +57,7 @@ class TestParser(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
||||
|
||||
self.assertContainsStrings(text.strip(), ["This is a test document."])
|
||||
|
||||
def test_get_pages_count(self):
|
||||
def test_get_page_count(self):
|
||||
"""
|
||||
GIVEN:
|
||||
- PDF file with a single page
|
||||
@@ -69,17 +69,17 @@ class TestParser(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
|
||||
- The method returns the correct number of pages (6)
|
||||
"""
|
||||
parser = RasterisedDocumentParser(uuid.uuid4())
|
||||
pages_count = parser.get_pages_count(
|
||||
page_count = parser.get_page_count(
|
||||
os.path.join(self.SAMPLE_FILES, "simple-digital.pdf"),
|
||||
"application/pdf",
|
||||
)
|
||||
self.assertEqual(pages_count, 1)
|
||||
self.assertEqual(page_count, 1)
|
||||
|
||||
pages_count = parser.get_pages_count(
|
||||
page_count = parser.get_page_count(
|
||||
os.path.join(self.SAMPLE_FILES, "multi-page-mixed.pdf"),
|
||||
"application/pdf",
|
||||
)
|
||||
self.assertEqual(pages_count, 6)
|
||||
self.assertEqual(page_count, 6)
|
||||
|
||||
def test_thumbnail(self):
|
||||
parser = RasterisedDocumentParser(uuid.uuid4())
|
||||
|
||||
Reference in New Issue
Block a user