Skip to content

Commit

Permalink
Merge pull request #3870 from tino097/3567-flask-feeds-controller
Browse files Browse the repository at this point in the history
3567 flask feeds controller
  • Loading branch information
amercader committed Oct 20, 2017
2 parents fe71b7e + 3c0f38a commit 8e15378
Show file tree
Hide file tree
Showing 3 changed files with 626 additions and 45 deletions.
7 changes: 0 additions & 7 deletions ckan/config/routing.py
Expand Up @@ -371,13 +371,6 @@ def make_map():
m.connect('/revision/list', action='list')
m.connect('/revision/{id}', action='read')

# feeds
with SubMapper(map, controller='feed') as m:
m.connect('/feeds/group/{id}.atom', action='group')
m.connect('/feeds/organization/{id}.atom', action='organization')
m.connect('/feeds/tag/{id}.atom', action='tag')
m.connect('/feeds/dataset.atom', action='general')
m.connect('/feeds/custom.atom', action='custom')

map.connect('ckanadmin_index', '/ckan-admin', controller='admin',
action='index', ckan_icon='gavel')
Expand Down
81 changes: 43 additions & 38 deletions ckan/tests/controllers/test_feed.py
@@ -1,89 +1,99 @@
# encoding: utf-8

from ckan.lib.helpers import url_for
from webhelpers.feedgenerator import GeoAtom1Feed

import ckan.plugins as plugins

import ckan.tests.helpers as helpers
import ckan.tests.factories as factories
import ckan.plugins as plugins
from webhelpers.feedgenerator import GeoAtom1Feed


class TestFeedNew(helpers.FunctionalTestBase):

@classmethod
def teardown_class(cls):
helpers.reset_db()

def test_atom_feed_page_zero_gives_error(self):
group = factories.Group()
offset = url_for(controller='feed', action='group',
id=group['name']) + '?page=0'
offset = url_for(u'feeds.group', id=group['name']) + '?page=0'
app = self._get_test_app()
offset = url_for(u'feeds.group', id=group['name']) + u'?page=0'

res = app.get(offset, status=400)
assert '"page" parameter must be a positive integer' in res, res

def test_atom_feed_page_negative_gives_error(self):
group = factories.Group()
offset = url_for(controller='feed', action='group',
id=group['name']) + '?page=-2'
offset = url_for(u'feeds.group', id=group['name']) + '?page=-2'
app = self._get_test_app()
offset = url_for(u'feeds.group', id=group['name']) + '?page=-2'
res = app.get(offset, status=400)
assert '"page" parameter must be a positive integer' in res, res

def test_atom_feed_page_not_int_gives_error(self):
group = factories.Group()
offset = url_for(controller='feed', action='group',
id=group['name']) + '?page=abc'
offset = url_for(u'feeds.group', id=group['name']) + '?page=abc'
app = self._get_test_app()
offset = url_for(u'feeds.group', id=group['name']) + '?page=abc'
res = app.get(offset, status=400)
assert '"page" parameter must be a positive integer' in res, res

def test_general_atom_feed_works(self):
dataset = factories.Dataset()
offset = url_for(controller='feed', action='general')
offset = url_for(u'feeds.general')
app = self._get_test_app()
offset = url_for(u'feeds.general')
res = app.get(offset)

assert '<title>{0}</title>'.format(dataset['title']) in res.body
assert u'<title>{0}</title>'.format(
dataset['title']) in res.body

def test_group_atom_feed_works(self):
group = factories.Group()
dataset = factories.Dataset(groups=[{'id': group['id']}])
offset = url_for(controller='feed', action='group',
id=group['name'])
offset = url_for(u'feeds.group', id=group['name'])
app = self._get_test_app()
offset = url_for(u'feeds.group', id=group['name'])
res = app.get(offset)

assert '<title>{0}</title>'.format(dataset['title']) in res.body
assert u'<title>{0}</title>'.format(
dataset['title']) in res.body

def test_organization_atom_feed_works(self):
group = factories.Organization()
dataset = factories.Dataset(owner_org=group['id'])
offset = url_for(controller='feed', action='organization',
id=group['name'])
offset = url_for(u'feeds.organization', id=group['name'])
app = self._get_test_app()
offset = url_for(u'feeds.organization', id=group['name'])
res = app.get(offset)

assert '<title>{0}</title>'.format(dataset['title']) in res.body
assert u'<title>{0}</title>'.format(
dataset['title']) in res.body

def test_custom_atom_feed_works(self):
dataset1 = factories.Dataset(
title='Test weekly',
extras=[{'key': 'frequency', 'value': 'weekly'}])
title=u'Test weekly',
extras=[{
'key': 'frequency',
'value': 'weekly'
}])
dataset2 = factories.Dataset(
title='Test daily',
extras=[{'key': 'frequency', 'value': 'daily'}])
offset = url_for(controller='feed', action='custom')
params = {
'q': 'frequency:weekly'
}
title=u'Test daily',
extras=[{
'key': 'frequency',
'value': 'daily'
}])

offset = url_for(u'feeds.custom')
params = {'q': 'frequency:weekly'}
app = self._get_test_app()
res = app.get(offset, params=params)

assert '<title>{0}</title>'.format(dataset1['title']) in res.body
assert u'<title>{0}</title>'.format(
dataset1['title']) in res.body

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


class TestFeedInterface(helpers.FunctionalTestBase):
Expand All @@ -102,8 +112,7 @@ def teardown_class(cls):
def test_custom_class_used(self):

app = self._get_test_app()
with app.flask_app.test_request_context():
offset = url_for(controller='feed', action='general')
offset = url_for(u'feeds.general')
app = self._get_test_app()
res = app.get(offset)

Expand All @@ -117,16 +126,12 @@ def test_additional_fields_added(self):
'xmax': '3567770',
}

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

factories.Dataset(extras=extras)

app = self._get_test_app()
with app.flask_app.test_request_context():
offset = url_for(controller='feed', action='general')
offset = url_for(u'feeds.general')
app = self._get_test_app()
res = app.get(offset)

Expand All @@ -142,6 +147,6 @@ def get_feed_class(self):
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'))
box = tuple(
float(extras.get(n)) for n in ('ymin', 'xmin', 'ymax', 'xmax'))
return {'geometry': box}

0 comments on commit 8e15378

Please sign in to comment.