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

abort storing transcript if previous version is missing #171

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
MarkLark86 marked this conversation as resolved.
Show resolved Hide resolved


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
13 changes: 12 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,15 @@ 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)

app.data.insert("items", [{"_id": "foo"}])
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
Loading