Skip to content

Commit

Permalink
abort storing transcript if previous version is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
petrjasek committed Apr 26, 2024
1 parent dc06626 commit 04e7612
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
17 changes: 14 additions & 3 deletions server/cp/signals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cp
import flask

from typing import Literal, Optional
from flask import current_app as app
Expand Down Expand Up @@ -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"):
Expand All @@ -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
Expand Down
10 changes: 9 additions & 1 deletion server/tests/test_signals.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 04e7612

Please sign in to comment.