Skip to content

Commit

Permalink
add custom atom handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tino097 committed Oct 17, 2017
1 parent 7fffe4b commit ee616b4
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 5 deletions.
80 changes: 79 additions & 1 deletion ckan/tests/controllers/test_feed.py
Expand Up @@ -4,6 +4,9 @@

import ckan.tests.helpers as helpers
import ckan.tests.factories as factories
import ckan.plugins as plugins
from ckan.views.feed import _FixedAtomFeed
from werkzeug.contrib.atom import AtomFeed, FeedEntry


class TestFeedNew(helpers.FunctionalTestBase):
Expand Down Expand Up @@ -82,7 +85,6 @@ def test_custom_atom_feed_works(self):
'value': 'daily'
}])

app = self._get_test_app()
offset = url_for('feeds.custom')
params = {'q': 'frequency:weekly'}
app = self._get_test_app()
Expand All @@ -93,3 +95,79 @@ def test_custom_atom_feed_works(self):

assert u'<title type="text">{0}</title>'.format(
dataset2['title']) not in res.body


class TestFeedInterface(helpers.FunctionalTestBase):
@classmethod
def setup_class(cls):
super(TestFeedInterface, cls).setup_class()

if not plugins.plugin_loaded('test_feed_plugin'):
plugins.load('test_feed_plugin')

@classmethod
def teardown_class(cls):
helpers.reset_db()
plugins.unload('test_feed_plugin')

def test_custom_class_used(self):

offset = url_for(u'feeds.custom')
app = self._get_test_app()
res = app.get(offset)

assert 'xmlns="http://www.w3.org/2005/Atom"' in res.body, res.body
assert 'CKAN - Custom query' in res.body, res.body

def test_additional_fields_added(self):
metadata = {
'ymin': '-2373790',
'xmin': '2937940',
'ymax': '-1681290',
'xmax': '3567770',
}

extras = [{'key': k, 'value': v} for (k, v) in metadata.items()]

factories.Dataset(extras=extras)

offset = url_for(u'feeds.custom')
app = self._get_test_app()
res = app.get(offset)
assert '<geometry>-2373790.000000 2937940.000000 -1681290.000000 3567770.000000</geometry>' in res.body, res.body
# assert '<extras>' in res.body, res.body


class MockFeedPlugin(plugins.SingletonPlugin):
plugins.implements(plugins.IFeed)

def get_feed_class(self):
return _GeoAtomFeed

def get_item_additional_fields(self, dataset_dict):
extras = {e['key']: e['value'] for e in dataset_dict['extras']}

box = tuple(
float(extras.get(n)) for n in ('ymin', 'xmin', 'ymax', 'xmax'))
return {'geometry': box}


class _GeoAtomFeed(_FixedAtomFeed):
def __init__(self, **kwargs):
_FixedAtomFeed.__init__(self, **kwargs)

def add(self, *args, **kwargs):
import pdb; pdb.set_trace()
if u'extras' in kwargs:
kwargs.pop(u'extras')
extras = {u'extras': None}
extras.update(kwargs)
entrie = _CustomFeedEntry(**extras)
_FixedAtomFeed.add(self, entrie, id=entrie.id, updated=entrie.updated)


class _CustomFeedEntry(FeedEntry):

def __init__(self, **kwargs):
FeedEntry.__init__(self, **kwargs)
self.extras = kwargs.get('extras')
15 changes: 11 additions & 4 deletions ckan/views/feed.py
Expand Up @@ -5,7 +5,6 @@

from flask import Blueprint
from werkzeug.contrib.atom import AtomFeed

from ckan.common import _, config, g, request
import ckan.lib.helpers as h
import ckan.lib.base as base
Expand Down Expand Up @@ -59,6 +58,13 @@ def _enclosure(pkg):
return links


def _set_extras(**kw):
extras = []
for key, value in kw.iteritems():
extras.append({key: value})
return extras


def output_feed(results, feed_title, feed_description, feed_link, feed_url,
navigation_urls, feed_guid):
author_name = config.get(u'ckan.feeds.author_name', u'').strip() or \
Expand Down Expand Up @@ -91,6 +97,7 @@ def output_feed(results, feed_title, feed_description, feed_link, feed_url,
if hasattr(plugin, u'get_item_additional_fields'):
additional_fields = plugin.get_item_additional_fields(pkg)

import pdb; pdb.set_trace()
feed.add(
title=pkg.get(u'title', u''),
description=pkg.get(u'notes', u''),
Expand All @@ -100,7 +107,7 @@ def output_feed(results, feed_title, feed_description, feed_link, feed_url,
author=pkg.get(u'author', u''),
categories=[{u'terms': t['name']} for t in pkg.get(u'tags')],
links=_enclosure(pkg),
**additional_fields)
extras=_set_extras(**additional_fields))

# response.content_type = feed.get_response()
return feed.get_response()
Expand Down Expand Up @@ -487,7 +494,7 @@ def add(self, *args, **kwargs):
kwargs.pop(u'generator')
defaults = {u'updated': None, u'published': None}
defaults.update(kwargs)
super(_FixedAtomFeed, self).add(*args, **defaults)
AtomFeed.add(self, *args, **defaults)

def latest_post_date(self):
"""
Expand All @@ -501,7 +508,7 @@ def latest_post_date(self):
if not len(updates): # delegate to parent for default behaviour
return super(_FixedAtomFeed, self).latest_post_date()
return max(updates)


# Routing
feeds.add_url_rule(u'/dataset.atom', methods=[u'GET'], view_func=general)
Expand Down

0 comments on commit ee616b4

Please sign in to comment.