Skip to content

Commit

Permalink
Allow setting parent id in doc metadata
Browse files Browse the repository at this point in the history
This makes the API more consistent between physical and logical domains.
  • Loading branch information
marijnkoolen committed Nov 21, 2023
1 parent bb7850d commit fce990b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pagexml/model/physical_document_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,13 @@ def set_derived_id(self, parent_id: str):
self.id = f"{parent_id}-{self.main_type}-{box_string}"
# self.metadata['id'] = self.id

def add_parent_id_to_metadata(self):
if self.parent:
self.metadata['parent_type'] = self.parent.main_type
self.metadata['parent_id'] = self.parent.id
if hasattr(self.parent, 'main_type') and self.parent.main_type is not None:
self.metadata[f'{self.parent.main_type}_id'] = self.parent.id


class LogicalStructureDoc(StructureDoc):

Expand Down
8 changes: 8 additions & 0 deletions tests/physical_document_model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ def test_set_derived_id(self):
self.doc.set_derived_id(parent.id)
self.assertEqual('parent_doc-book-0-0-10-10', self.doc.id)

def test_add_parent_id_to_metadata(self):
child = pdm.PhysicalStructureDoc(doc_id='doc2', doc_type='chapter')
child.id = 'parent_doc'
self.doc.set_as_parent([child])
self.doc.add_parent_id_to_metadata()
self.assertIn('book_id', child.metadata)
self.assertEqual('doc1', child.metadata['book_id'])

def test_json(self):
expected_json = {
'id': 'doc1',
Expand Down

0 comments on commit fce990b

Please sign in to comment.