Refactor pages_count --> page_count

This commit is contained in:
shamoon 2024-09-25 00:35:14 -07:00
parent 9b13c607db
commit 693ce6553d
21 changed files with 64 additions and 64 deletions

View File

@ -54,7 +54,7 @@ fields:
- `archived_file_name`: Verbose filename of the archived document. - `archived_file_name`: Verbose filename of the archived document.
Read-only. Null if no archived document is available. Read-only. Null if no archived document is available.
- `notes`: Array of notes associated with the document. - `notes`: Array of notes associated with the document.
- `pages_count`: Number of pages. - `page_count`: Number of pages.
- `set_permissions`: Allows setting document permissions. Optional, - `set_permissions`: Allows setting document permissions. Optional,
write-only. See [below](#permissions). write-only. See [below](#permissions).
- `custom_fields`: Array of custom fields & values, specified as - `custom_fields`: Array of custom fields & values, specified as

View File

@ -65,7 +65,7 @@ const savedView: SavedView = {
DisplayField.CORRESPONDENT, DisplayField.CORRESPONDENT,
DisplayField.DOCUMENT_TYPE, DisplayField.DOCUMENT_TYPE,
DisplayField.STORAGE_PATH, DisplayField.STORAGE_PATH,
DisplayField.PAGES_COUNT, DisplayField.PAGE_COUNT,
`${DisplayField.CUSTOM_FIELD}11` as any, `${DisplayField.CUSTOM_FIELD}11` as any,
`${DisplayField.CUSTOM_FIELD}15` as any, `${DisplayField.CUSTOM_FIELD}15` as any,
], ],
@ -345,7 +345,7 @@ describe('SavedViewWidgetComponent', () => {
expect(component.getColumnTitle(DisplayField.STORAGE_PATH)).toEqual( expect(component.getColumnTitle(DisplayField.STORAGE_PATH)).toEqual(
'Storage path' 'Storage path'
) )
expect(component.getColumnTitle(DisplayField.PAGES_COUNT)).toEqual('Pages') expect(component.getColumnTitle(DisplayField.PAGE_COUNT)).toEqual('Pages')
}) })
it('should get correct column title for custom field', () => { it('should get correct column title for custom field', () => {

View File

@ -111,10 +111,10 @@
</div> </div>
} }
} }
@if (displayFields.includes(DisplayField.PAGES_COUNT) && document.pages_count) { @if (displayFields.includes(DisplayField.PAGE_COUNT) && document.page_count) {
<div class="list-group-item bg-light text-dark p-1 border-0 d-flex align-items-center"> <div class="list-group-item bg-light text-dark p-1 border-0 d-flex align-items-center">
<i-bs width=".9em" height=".9em" class="me-2 text-muted" name="files"></i-bs> <i-bs width=".9em" height=".9em" class="me-2 text-muted" name="files"></i-bs>
<small i18n>{document.pages_count, plural, =1 {1 page} other {{{document.pages_count}} pages}}</small> <small i18n>{document.page_count, plural, =1 {1 page} other {{{document.page_count}} pages}}</small>
</div> </div>
} }
@if (displayFields.includes(DisplayField.OWNER) && document.owner && document.owner !== settingsService.currentUser.id) { @if (displayFields.includes(DisplayField.OWNER) && document.owner && document.owner !== settingsService.currentUser.id) {

View File

@ -31,7 +31,7 @@ const doc = {
correspondent: 8, correspondent: 8,
document_type: 10, document_type: 10,
storage_path: null, storage_path: null,
pages_count: 8, page_count: 8,
notes: [ notes: [
{ {
id: 11, id: 11,

View File

@ -88,11 +88,11 @@
</div> </div>
</div> </div>
} }
@if (displayFields.includes(DisplayField.PAGES_COUNT) && document.pages_count) { @if (displayFields.includes(DisplayField.PAGE_COUNT) && document.page_count) {
<div class="list-group-item bg-transparent p-0 border-0 d-flex flex-wrap-reverse justify-content-between"> <div class="list-group-item bg-transparent p-0 border-0 d-flex flex-wrap-reverse justify-content-between">
<div class="ps-0 p-1" placement="top"> <div class="ps-0 p-1" placement="top">
<i-bs width="1em" height="1em" class="me-2 text-muted" name="files"></i-bs> <i-bs width="1em" height="1em" class="me-2 text-muted" name="files"></i-bs>
<small i18n>{document.pages_count, plural, =1 {1 page} other {{{document.pages_count}} pages}}</small> <small i18n>{document.page_count, plural, =1 {1 page} other {{{document.page_count}} pages}}</small>
</div> </div>
</div> </div>
} }

View File

@ -34,7 +34,7 @@ const doc = {
correspondent: 8, correspondent: 8,
document_type: 10, document_type: 10,
storage_path: null, storage_path: null,
pages_count: 12, page_count: 12,
notes: [ notes: [
{ {
id: 11, id: 11,

View File

@ -246,9 +246,9 @@
(sort)="onSort($event)" (sort)="onSort($event)"
i18n>Added</th> i18n>Added</th>
} }
@if (activeDisplayFields.includes(DisplayField.PAGES_COUNT)) { @if (activeDisplayFields.includes(DisplayField.PAGE_COUNT)) {
<th class="cursor-pointer" <th class="cursor-pointer"
pngxSortable="pages_count" pngxSortable="page_count"
title="Sort by number of pages" i18n-title title="Sort by number of pages" i18n-title
[currentSortField]="list.sortField" [currentSortField]="list.sortField"
[currentSortReverse]="list.sortReverse" [currentSortReverse]="list.sortReverse"
@ -339,9 +339,9 @@
{{d.added | customDate}} {{d.added | customDate}}
</td> </td>
} }
@if (activeDisplayFields.includes(DisplayField.PAGES_COUNT)) { @if (activeDisplayFields.includes(DisplayField.PAGE_COUNT)) {
<td> <td>
{{ d.pages_count }} {{ d.page_count }}
</td> </td>
} }
@if (activeDisplayFields.includes(DisplayField.SHARED)) { @if (activeDisplayFields.includes(DisplayField.SHARED)) {

View File

@ -26,7 +26,7 @@ export enum DisplayField {
OWNER = 'owner', OWNER = 'owner',
SHARED = 'shared', SHARED = 'shared',
ASN = 'asn', ASN = 'asn',
PAGES_COUNT = 'pagescount', PAGE_COUNT = 'pagecount',
} }
export const DEFAULT_DISPLAY_FIELDS = [ export const DEFAULT_DISPLAY_FIELDS = [
@ -75,7 +75,7 @@ export const DEFAULT_DISPLAY_FIELDS = [
name: $localize`ASN`, name: $localize`ASN`,
}, },
{ {
id: DisplayField.PAGES_COUNT, id: DisplayField.PAGE_COUNT,
name: $localize`Pages`, name: $localize`Pages`,
}, },
] ]
@ -99,7 +99,7 @@ export const DOCUMENT_SORT_FIELDS = [
{ field: 'modified', name: $localize`Modified` }, { field: 'modified', name: $localize`Modified` },
{ field: 'num_notes', name: $localize`Notes` }, { field: 'num_notes', name: $localize`Notes` },
{ field: 'owner', name: $localize`Owner` }, { field: 'owner', name: $localize`Owner` },
{ field: 'pages_count', name: $localize`Pages` }, { field: 'page_count', name: $localize`Pages` },
] ]
export const DOCUMENT_SORT_FIELDS_FULLTEXT = [ export const DOCUMENT_SORT_FIELDS_FULLTEXT = [
@ -171,5 +171,5 @@ export interface Document extends ObjectWithPermissions {
// write-only field // write-only field
remove_inbox_tags?: boolean remove_inbox_tags?: boolean
pages_count?: number page_count?: number
} }

View File

@ -345,7 +345,7 @@ export class SettingsService {
DisplayField.CREATED, DisplayField.CREATED,
DisplayField.ADDED, DisplayField.ADDED,
DisplayField.ASN, DisplayField.ASN,
DisplayField.PAGES_COUNT, DisplayField.PAGE_COUNT,
DisplayField.SHARED, DisplayField.SHARED,
].includes(field.id) ].includes(field.id)
) { ) {

View File

@ -387,8 +387,8 @@ def delete_pages(doc_ids: list[int], pages: list[int]):
pdf.remove_unreferenced_resources() pdf.remove_unreferenced_resources()
pdf.save() pdf.save()
doc.checksum = hashlib.md5(doc.source_path.read_bytes()).hexdigest() doc.checksum = hashlib.md5(doc.source_path.read_bytes()).hexdigest()
if doc.pages_count is not None: if doc.page_count is not None:
doc.pages_count = doc.pages_count - len(pages) doc.page_count = doc.page_count - len(pages)
doc.save() doc.save()
update_document_archive_file.delay(document_id=doc.id) update_document_archive_file.delay(document_id=doc.id)
logger.info(f"Deleted pages {pages} from document {doc.id}") logger.info(f"Deleted pages {pages} from document {doc.id}")

View File

@ -586,7 +586,7 @@ class ConsumerPlugin(
date = None date = None
thumbnail = None thumbnail = None
archive_path = None archive_path = None
pages_count = None page_count = None
try: try:
self._send_progress( self._send_progress(
@ -622,7 +622,7 @@ class ConsumerPlugin(
) )
date = parse_date(self.filename, text) date = parse_date(self.filename, text)
archive_path = document_parser.get_archive_path() 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: except ParseError as e:
document_parser.cleanup() document_parser.cleanup()
@ -667,7 +667,7 @@ class ConsumerPlugin(
document = self._store( document = self._store(
text=text, text=text,
date=date, date=date,
pages_count=pages_count, page_count=page_count,
mime_type=mime_type, mime_type=mime_type,
) )
@ -797,7 +797,7 @@ class ConsumerPlugin(
self, self,
text: str, text: str,
date: Optional[datetime.datetime], date: Optional[datetime.datetime],
pages_count: Optional[int], page_count: Optional[int],
mime_type: str, mime_type: str,
) -> Document: ) -> Document:
# If someone gave us the original filename, use it instead of doc. # If someone gave us the original filename, use it instead of doc.
@ -843,7 +843,7 @@ class ConsumerPlugin(
created=create_date, created=create_date,
modified=create_date, modified=create_date,
storage_type=storage_type, storage_type=storage_type,
pages_count=pages_count, page_count=page_count,
original_filename=self.filename, original_filename=self.filename,
) )

View File

@ -80,7 +80,7 @@ def get_schema():
has_owner=BOOLEAN(), has_owner=BOOLEAN(),
viewer_id=KEYWORD(commas=True), viewer_id=KEYWORD(commas=True),
checksum=TEXT(), checksum=TEXT(),
pages_count=NUMERIC(sortable=True), page_count=NUMERIC(sortable=True),
original_filename=TEXT(sortable=True), original_filename=TEXT(sortable=True),
is_shared=BOOLEAN(), is_shared=BOOLEAN(),
) )
@ -182,7 +182,7 @@ def update_document(writer: AsyncWriter, doc: Document):
has_owner=doc.owner is not None, has_owner=doc.owner is not None,
viewer_id=viewer_ids if viewer_ids else None, viewer_id=viewer_ids if viewer_ids else None,
checksum=doc.checksum, checksum=doc.checksum,
pages_count=doc.pages_count, page_count=doc.page_count,
original_filename=doc.original_filename, original_filename=doc.original_filename,
is_shared=len(viewer_ids) > 0, is_shared=len(viewer_ids) > 0,
) )
@ -249,7 +249,7 @@ class DelayedQuery:
"archive_serial_number": "asn", "archive_serial_number": "asn",
"num_notes": "num_notes", "num_notes": "num_notes",
"owner": "owner", "owner": "owner",
"pages_count": "pages_count", "page_count": "page_count",
} }
if field.startswith("-"): if field.startswith("-"):

View File

@ -15,7 +15,7 @@ def source_path(self):
return Path(settings.ORIGINALS_DIR / fname).resolve() 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") Document = apps.get_model("documents", "Document")
if not Document.objects.all().exists(): if not Document.objects.all().exists():
@ -33,7 +33,7 @@ def add_number_of_pages_to_pages_count(apps, schema_editor):
try: try:
with pikepdf.Pdf.open(source_path(doc)) as pdf: with pikepdf.Pdf.open(source_path(doc)) as pdf:
if pdf.pages is not None: if pdf.pages is not None:
doc.pages_count = len(pdf.pages) doc.page_count = len(pdf.pages)
doc.save() doc.save()
except Exception as e: # pragma: no cover except Exception as e: # pragma: no cover
print(f"Error retrieving number of pages for {doc.filename}: {e}") print(f"Error retrieving number of pages for {doc.filename}: {e}")
@ -47,7 +47,7 @@ class Migration(migrations.Migration):
operations = [ operations = [
migrations.AddField( migrations.AddField(
model_name="document", model_name="document",
name="pages_count", name="page_count",
field=models.PositiveIntegerField( field=models.PositiveIntegerField(
blank=False, blank=False,
null=True, null=True,
@ -56,7 +56,7 @@ class Migration(migrations.Migration):
), ),
), ),
migrations.RunPython( migrations.RunPython(
add_number_of_pages_to_pages_count, add_number_of_pages_to_page_count,
migrations.RunPython.noop, migrations.RunPython.noop,
), ),
] ]

View File

@ -205,8 +205,8 @@ class Document(SoftDeleteModel, ModelWithOwner):
help_text=_("The checksum of the archived document."), help_text=_("The checksum of the archived document."),
) )
pages_count = models.PositiveIntegerField( page_count = models.PositiveIntegerField(
_("pages count"), _("page count"),
blank=False, blank=False,
null=True, null=True,
unique=False, unique=False,
@ -426,7 +426,7 @@ class SavedView(ModelWithOwner):
OWNER = ("owner", _("Owner")) OWNER = ("owner", _("Owner"))
SHARED = ("shared", _("Shared")) SHARED = ("shared", _("Shared"))
ASN = ("asn", _("ASN")) ASN = ("asn", _("ASN"))
PAGES_COUNT = ("pagescount", _("Pages")) PAGE_COUNT = ("pagecount", _("Pages"))
CUSTOM_FIELD = ("custom_field_%d", ("Custom Field")) CUSTOM_FIELD = ("custom_field_%d", ("Custom Field"))
name = models.CharField(_("name"), max_length=128) name = models.CharField(_("name"), max_length=128)

View File

@ -367,7 +367,7 @@ class DocumentParser(LoggingMixin):
def extract_metadata(self, document_path, mime_type): def extract_metadata(self, document_path, mime_type):
return [] return []
def get_pages_count(self, document_path, mime_type): def get_page_count(self, document_path, mime_type):
return None return None
def parse(self, document_path, mime_type, file_name=None): def parse(self, document_path, mime_type, file_name=None):

View File

@ -750,7 +750,7 @@ class DocumentSerializer(
original_file_name = SerializerMethodField() original_file_name = SerializerMethodField()
archived_file_name = SerializerMethodField() archived_file_name = SerializerMethodField()
created_date = serializers.DateField(required=False) created_date = serializers.DateField(required=False)
pages_count = SerializerMethodField() page_count = SerializerMethodField()
custom_fields = CustomFieldInstanceSerializer( custom_fields = CustomFieldInstanceSerializer(
many=True, many=True,
@ -771,8 +771,8 @@ class DocumentSerializer(
required=False, required=False,
) )
def get_pages_count(self, obj): def get_page_count(self, obj):
return obj.pages_count return obj.page_count
def get_original_file_name(self, obj): def get_original_file_name(self, obj):
return obj.original_filename return obj.original_filename
@ -889,7 +889,7 @@ class DocumentSerializer(
"notes", "notes",
"custom_fields", "custom_fields",
"remove_inbox_tags", "remove_inbox_tags",
"pages_count", "page_count",
) )
list_serializer_class = OwnedObjectListSerializer list_serializer_class = OwnedObjectListSerializer

View File

@ -389,7 +389,7 @@ class TestPDFActions(DirectoriesMixin, TestCase):
title="B", title="B",
filename=sample2, filename=sample2,
mime_type="application/pdf", mime_type="application/pdf",
pages_count=8, page_count=8,
) )
self.doc2.archive_filename = sample2_archive self.doc2.archive_filename = sample2_archive
self.doc2.save() self.doc2.save()
@ -682,19 +682,19 @@ class TestPDFActions(DirectoriesMixin, TestCase):
THEN: THEN:
- Save should be called once - Save should be called once
- Archive file should be updated 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] doc_ids = [self.doc2.id]
initial_pages_count = self.doc2.pages_count initial_page_count = self.doc2.page_count
pages = [1, 3] pages = [1, 3]
result = bulk_edit.delete_pages(doc_ids, pages) result = bulk_edit.delete_pages(doc_ids, pages)
mock_pdf_save.assert_called_once() mock_pdf_save.assert_called_once()
mock_update_archive_file.assert_called_once() mock_update_archive_file.assert_called_once()
self.assertEqual(result, "OK") 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.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("documents.tasks.update_document_archive_file.delay")
@mock.patch("pikepdf.Pdf.save") @mock.patch("pikepdf.Pdf.save")

View File

@ -14,9 +14,9 @@ def source_path_before(self):
return os.path.join(settings.ORIGINALS_DIR, fname) return os.path.join(settings.ORIGINALS_DIR, fname)
class TestMigrateDocumentPagesCount(TestMigrations): class TestMigrateDocumentPageCount(TestMigrations):
migrate_from = "1052_document_transaction_id" migrate_from = "1052_document_transaction_id"
migrate_to = "1053_document_pages_count" migrate_to = "1053_document_page_count"
def setUpBeforeMigration(self, apps): def setUpBeforeMigration(self, apps):
Document = apps.get_model("documents", "Document") Document = apps.get_model("documents", "Document")
@ -31,15 +31,15 @@ class TestMigrateDocumentPagesCount(TestMigrations):
source_path_before(doc), source_path_before(doc),
) )
def testDocumentPagesCountMigrated(self): def testDocumentPageCountMigrated(self):
Document = self.apps.get_model("documents", "Document") Document = self.apps.get_model("documents", "Document")
doc = Document.objects.get(id=self.doc_id) doc = Document.objects.get(id=self.doc_id)
self.assertEqual(doc.pages_count, 1) self.assertEqual(doc.page_count, 1)
class TestMigrateDocumentPagesCountBackwards(TestMigrations): class TestMigrateDocumentPageCountBackwards(TestMigrations):
migrate_from = "1053_document_pages_count" migrate_from = "1053_document_page_count"
migrate_to = "1052_document_transaction_id" migrate_to = "1052_document_transaction_id"
def setUpBeforeMigration(self, apps): def setUpBeforeMigration(self, apps):
@ -48,12 +48,12 @@ class TestMigrateDocumentPagesCountBackwards(TestMigrations):
title="test1", title="test1",
mime_type="application/pdf", mime_type="application/pdf",
filename="file1.pdf", filename="file1.pdf",
pages_count=8, page_count=8,
) )
self.doc_id = doc.id 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") Document = self.apps.get_model("documents", "Document")
self.assertFalse( 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()],
) )

View File

@ -361,7 +361,7 @@ class DocumentViewSet(
"archive_serial_number", "archive_serial_number",
"num_notes", "num_notes",
"owner", "owner",
"pages_count", "page_count",
) )
def get_queryset(self): def get_queryset(self):

View File

@ -41,14 +41,14 @@ class RasterisedDocumentParser(DocumentParser):
""" """
return OcrConfig() return OcrConfig()
def get_pages_count(self, document_path, mime_type): def get_page_count(self, document_path, mime_type):
pages_count = None page_count = None
if mime_type == "application/pdf": if mime_type == "application/pdf":
import pikepdf import pikepdf
with pikepdf.Pdf.open(document_path) as pdf: with pikepdf.Pdf.open(document_path) as pdf:
pages_count = len(pdf.pages) page_count = len(pdf.pages)
return pages_count return page_count
def extract_metadata(self, document_path, mime_type): def extract_metadata(self, document_path, mime_type):
result = [] result = []

View File

@ -57,7 +57,7 @@ class TestParser(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
self.assertContainsStrings(text.strip(), ["This is a test document."]) self.assertContainsStrings(text.strip(), ["This is a test document."])
def test_get_pages_count(self): def test_get_page_count(self):
""" """
GIVEN: GIVEN:
- PDF file with a single page - PDF file with a single page
@ -69,17 +69,17 @@ class TestParser(DirectoriesMixin, FileSystemAssertsMixin, TestCase):
- The method returns the correct number of pages (6) - The method returns the correct number of pages (6)
""" """
parser = RasterisedDocumentParser(uuid.uuid4()) 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"), os.path.join(self.SAMPLE_FILES, "simple-digital.pdf"),
"application/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"), os.path.join(self.SAMPLE_FILES, "multi-page-mixed.pdf"),
"application/pdf", "application/pdf",
) )
self.assertEqual(pages_count, 6) self.assertEqual(page_count, 6)
def test_thumbnail(self): def test_thumbnail(self):
parser = RasterisedDocumentParser(uuid.uuid4()) parser = RasterisedDocumentParser(uuid.uuid4())