Skip to content

Commit

Permalink
[SDESK-4907] Overload the media topics with AAP specific values
Browse files Browse the repository at this point in the history
  • Loading branch information
marwoodandrew committed Mar 9, 2020
1 parent 673ed79 commit e5fccf7
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions server/aap/io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# at https://www.sourcefabric.org/superdesk/license

import aap.io.iptc_extension # noqa
import aap.io.media_topics_extension # noqa
from .feeding_services.aap_sports_service import AAPSportsHTTPFeedingService # noqa
from .feeding_services.intelematics_fuel_service import IntelematicsFuelHTTPFeedingService # noqa
from .feeding_services.intelematics_incidents_service import IntelematicsIncidentHTTPFeedingService # noqa
Expand Down
43 changes: 43 additions & 0 deletions server/aap/io/media_topics_extension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# -*- coding: utf-8; -*-
#
# This file is part of Superdesk.
#
# Copyright 2013, 2014 Sourcefabric z.u. and contributors.
#
# For the full copyright and license information, please see the
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license

import os
from bson import json_util


def load_codes(filename):
with open(filename, 'rb') as f:
codes_bytes = f.read()
codes = json_util.loads(codes_bytes.decode('UTF-8'))
return codes


dirname = os.path.dirname(os.path.realpath(__file__))
data_media_topics = os.path.join(dirname, 'data', 'media_topics.json')
aap_media_topics = load_codes(data_media_topics)


def init_app(app):
"""Provides a mechanism to overload the media topics
:param app:
:return:
"""

# Update the topics with any local versions
for topic in aap_media_topics:
app.mediatopics.media_topic_map[topic.get('qcode')] = topic

# clear the lookup maps
app.mediatopics.clear_maps()

# re-generate the lookup maps
mediatopics = [t for (_k, t) in app.mediatopics.media_topic_map.items()]
app.mediatopics.generate_mediatopic_to_subject_map(mediatopics)
52 changes: 52 additions & 0 deletions server/aap/io/media_topics_extension_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

from unittests import TestCase
from aap.io.media_topics_extension import init_app


class TopicTest(TestCase):
def test_all_subjects_map_to_a_media_topic(self):
init_app(self.app)
for (k, _v) in sorted(self.app.subjects.subjects.items()):
topic = self.app.mediatopics.get_media_topic(k)
# topics codes starting from 99 are AAP specific
if not k.startswith('99'):
self.assertIsNotNone(topic)
# print(k, v, '\t---->\t',topic,
# self.app.mediatopics.get_media_topic_item(topic).get('prefLabel').get('en-GB'))
else:
self.assertINone(topic)

def test_get_items(self):
init_app(self.app)
items = self.app.mediatopics.get_items()
for i in items:
subject = self.app.mediatopics.get_subject_code(i.get('qcode'))
self.assertIsNotNone(subject)
# print(i.get('qcode'), i.get('name'), '\t---->\t', subject, self.app.subjects.subjects.get(subject))

def test_get_media_topics(self):
init_app(self.app)
items = self.app.mediatopics.get_media_topics()
for i in items.keys():
subject = self.app.mediatopics.get_subject_code(i)
self.assertIsNotNone(subject)
# print(i, v.get('prefLabel').get('en-GB'), '\t---->\t', subject, self.app.subjects.subjects.get(subject))

def test_media_topic_to_subject(self):
init_app(self.app)
subject = self.app.mediatopics.get_subject_code('medtop:20000070')
self.assertEqual(subject, '16004000')

def test_subject_to_media_topic(self):
# The standard mapping maps hip hop to music
topic = self.app.mediatopics.get_media_topic('01011007')
self.assertEqual(topic, 'medtop:20000018')
init_app(self.app)
# AAP overrides to it to point to hip hop to hip hop
topic = self.app.mediatopics.get_media_topic('01011007')
self.assertEqual(topic, 'medtop:20001184')

def test_get_media_topic_item(self):
init_app(self.app)
topic = self.app.mediatopics.get_media_topic_item('medtop:20000070')
self.assertEqual(topic['type'], ['http://www.w3.org/2004/02/skos/core#Concept'])
1 change: 1 addition & 0 deletions server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def env(variable, fallback_value=None):
'aap.io.feed_parsers',
'aap.data_consistency',
'aap.io.iptc_extension',
'aap.io.media_topics_extension',
'instrumentation',
'planning',
'aap.io.feeding_services',
Expand Down

0 comments on commit e5fccf7

Please sign in to comment.