carj/pyPreservica

Entity.has_metadata() returns False when metadata exists

Closed this issue · 3 comments

Hi James!

What follows is code that asks for children of SO. In this case it is given that all children are IOs. I wish to check those IOs for metadata (and then parse that metadata). However, just iterating over children doesn't work and additional query is required to get the metadata.

from pyPreservica import EntityAPI

# SO that cointains IOs
SO_UUID = "967b244d-216a-4e34-8bdd-656e686ee2e9"

client = EntityAPI()

assets = client.children(SO_UUID)

for asset_1 in assets.results:
    # Iterating over results to check for metadata
    print(f"{asset_1.reference}, {asset_1.has_metadata()}")
    # Output: 990a6523-0620-43f6-be2d-b3e6e340e24c, False
   
    # Querying API directly for given reference
    asset_2 = client.asset(asset_1.reference)
    print(f"{asset_2.reference}, {asset_2.has_metadata()}")
    # Output: 990a6523-0620-43f6-be2d-b3e6e340e24c, True

Is this the intended behaviour?

From entityAPI.py#L1895 in EntityAPI.children() I see that on requesting children, new Asset objects are created where metadata is set None.

From common.py#L479 I see that Entity.has_metadata() returns bool(self.metadata) so in our case bool(None) which is False

From entityAPI.py#L1090 in EntityAPI.metadata_for_entity() I see that this case is expected, as entity.metadata is checked directly for None and if this is the case, new Entity object is created much like my second case.

So I guess Entity.has_metadata() should either:

  1. when entity.metadata is None, return None

  2. when entity.metadata is None, do an API request to figure out if entity has metadata like EntityAPI.metadata_for_entity() does and return boolean.

since responding with False is false.

carj commented
carj commented

You could replace your code with this:

from pyPreservica import *

# SO that cointains IOs
SO_UUID = "967b244d-216a-4e34-8bdd-656e686ee2e9"

client = EntityAPI()

for asset in filter(only_assets, client.descendants(SO_UUID)):
    a = client.asset(asset.reference)
    if a.has_metadata():
        print(a)

Indeed there is a note in documentation about this! Thanks!

I guess since I didn't read about descendants bit and was using children on my own, I didn't notice that. Thanks again!