From 04e76129141972bec105be9035b93b6e900eb4a4 Mon Sep 17 00:00:00 2001 From: Petr Jasek Date: Fri, 26 Apr 2024 11:28:36 +0200 Subject: [PATCH] abort storing transcript if previous version is missing --- server/cp/signals.py | 17 ++++++++++++++--- server/tests/test_signals.py | 10 +++++++++- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/server/cp/signals.py b/server/cp/signals.py index e3d111c..dcfb676 100644 --- a/server/cp/signals.py +++ b/server/cp/signals.py @@ -1,4 +1,5 @@ import cp +import flask from typing import Literal, Optional from flask import current_app as app @@ -67,6 +68,11 @@ def on_push(sender, item, **kwargs): if item.get("language"): item["language"] = fix_language(item["language"]) + if get_media_type(item) and item.get("evolvedfrom"): + parent_item = get_resource_service("items").find_one(req=None, _id=item["evolvedfrom"]) + if parent_item is None: + flask.abort(503) # must be 50x error to trigger retry later + def user_auth_is_gip(user: User) -> bool: if not user.get("company"): @@ -81,12 +87,17 @@ def user_auth_is_gip(user: User) -> bool: return company.get("auth_provider") == "gip" +def get_media_type(item): + media_type_scheme = get_media_type_scheme() + return next( + (s for s in item.get("subject", []) if s.get("scheme") == media_type_scheme), None + ) + + def handle_transcripts(item): item.setdefault("subject", []) media_type_scheme = get_media_type_scheme() - media_type = next( - (s for s in item["subject"] if s.get("scheme") == media_type_scheme), None - ) + media_type = get_media_type(item) media_source_scheme = app.config.get("MEDIA_SOURCE_SCHEME", "station") media_source = next( (s for s in item["subject"] if s.get("scheme") == media_source_scheme), None diff --git a/server/tests/test_signals.py b/server/tests/test_signals.py index 980adf3..729ebe9 100644 --- a/server/tests/test_signals.py +++ b/server/tests/test_signals.py @@ -1,10 +1,12 @@ import cp import bson +import pytest import responses import cp.signals as signals from datetime import datetime, timedelta from responses import matchers +from werkzeug.exceptions import HTTPException def test_on_publish_no_extended_headline(app): @@ -172,7 +174,7 @@ def test_cem_notification_for_non_google_auth(app, mocker): assert len(sub.mock_calls) == 0 -def test_language_agenda(): +def test_language_agenda(app): item = {"language": "en-CA"} signals.init_app(None) signals.push.send(None, item=item) @@ -185,6 +187,12 @@ def test_language_agenda(): assert "fr" == item["language"] +def test_push_abort_missing_version(app): + item = {"evolvedfrom": "foo", "subject": [{"scheme": "mediaformat"}]} + with pytest.raises(HTTPException): + signals.on_push(None, item=item) + + def test_handle_transcripts(app): text_item = {"source": "CP", "subject": []} signals.on_publish_item(None, text_item)