Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement updating of picture metadata on publish #2594

Merged
merged 4 commits into from
Jun 5, 2024

Conversation

petrjasek
Copy link
Member

SDCP-737

@petrjasek petrjasek requested a review from MarkLark86 June 4, 2024 08:21
@petrjasek petrjasek added this to the 2.8 milestone Jun 4, 2024

#: Map picture item data to photo metadata
#:
#: .. versionadded:: 2.7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be 2.8?

setup.py Outdated
@@ -67,6 +67,7 @@
"itsdangerous>=1.1,<2.0",
"pymemcache>=4.0,<4.1",
"xmlsec>=1.3.13,<1.3.15",
"pyexiv2>=2.12.0,<2.13",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using this library may be troublesome on devices with ARM CPUs (LeoHsiao1/pyexiv2#108) such as the Apple M1.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep true, haven't really seen any alternatives, and as this won't be used by default might make it import the library only when calling those methods so it won't break anything by default

@@ -914,6 +918,31 @@ def _inherit_publish_schedule(self, original, updates, associated_item):
associated_item[PUBLISH_SCHEDULE] = publish_schedule
associated_item[SCHEDULE_SETTINGS] = schedule_settings

def _update_picture_metadata(self, updates, original, updated):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a personal preference.
To avoid nested if statements, and along the idea of fail fast / return early, this method could be re-written as:

def _update_picture_metadata(self, updates, original, updated):
    renditions = updated.get("renditions") or {}
    mapping = app.config.get("PHOTO_METADATA_MAPPING")
    if not mapping or not renditions:
        return

    try:
        media_id = renditions["original"]["media"]
    except (KeyError, TypeError):
        return

    if not media_id:
        return

    picture = app.media.get(media_id)
    binary = picture.read()
    metadata = get_metadata_from_item(updated, mapping)
    updated_binary = write_metadata(binary, metadata)

    if updated_binary == binary:
        return

    updated_media_id = app.media.put(
        updated_binary, content_type=picture.content_type, filename=picture.filename
    )
    updates.setdefault("renditions", deepcopy(renditions))["original"].update(
        {
            "media": updated_media_id,
            "href": app.media.url_for_media(updated_media_id, picture.content_type),
        }
    )
    updated["renditions"] = updates["renditions"]

@petrjasek petrjasek merged commit 5aee57a into superdesk:develop Jun 5, 2024
16 checks passed
@petrjasek petrjasek deleted the write-picture-metadata branch June 5, 2024 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants