Skip to content

Commit

Permalink
Merge branch 'package-blueprint' of https://github.com/smotornyuk/ckan
Browse files Browse the repository at this point in the history
…into smotornyuk-package-blueprint
  • Loading branch information
amercader committed Jun 8, 2018
2 parents 3c7ad61 + affd4fc commit b788193
Show file tree
Hide file tree
Showing 138 changed files with 3,117 additions and 1,151 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -124,6 +124,12 @@ Changes and deprecations:
* The old Celery based background jobs have been removed in CKAN 2.8 in favour of the new RQ based
jobs (http://docs.ckan.org/en/latest/maintaining/background-tasks.html). Extensions can still
of course use Celery but they will need to handle the management themselves.
* `ckan.recaptcha.version` config option is removed, since v2 is the only valid
version now (#4061)
* After introducing dataset blueprint, `h.get_facet_items_dict` takes search_facets as second argument.
This change is aimed to reduce usage of global variables in context. For a while, it has default value
of None, in which case, `c.search_facets` will be used. But all template designers are strongly advised
to specify this argument explicitly, as in future it'll become required.
* The ``ckan.recaptcha.version`` config option is now removed, since v2 is the only valid version now (#4061)

v2.7.4 2018-05-09
Expand Down
2 changes: 2 additions & 0 deletions ckan/common.py
Expand Up @@ -24,6 +24,8 @@

import simplejson as json

current_app = flask.current_app

try:
from collections import OrderedDict # from python 2.7
except ImportError:
Expand Down
2 changes: 1 addition & 1 deletion ckan/config/environment.py
Expand Up @@ -279,7 +279,7 @@ def update_config():
# any Pylons config options)

# Initialize SQLAlchemy
engine = sqlalchemy.engine_from_config(config, client_encoding='utf8')
engine = sqlalchemy.engine_from_config(config)
model.init_model(engine)

for plugin in p.PluginImplementations(p.IConfigurable):
Expand Down
80 changes: 5 additions & 75 deletions ckan/config/routing.py
Expand Up @@ -12,7 +12,7 @@
from routes.mapper import SubMapper, Mapper as _Mapper

import ckan.plugins as p
from ckan.common import config
from ckan.common import config, current_app

named_routes = {}

Expand Down Expand Up @@ -87,6 +87,7 @@ def make_map():

import ckan.lib.plugins as lib_plugins
lib_plugins.reset_package_plugins()
lib_plugins.reset_group_plugins()

map = Mapper(directory=config['pylons.paths']['controllers'],
always_scan=config['debug'])
Expand Down Expand Up @@ -148,79 +149,6 @@ def make_map():
map.redirect('/package', '/dataset')
map.redirect('/package/{url:.*}', '/dataset/{url}')

with SubMapper(map, controller='package') as m:
m.connect('search', '/dataset', action='search',
highlight_actions='index search')
m.connect('dataset_new', '/dataset/new', action='new')
m.connect('/dataset/{action}',
requirements=dict(action='|'.join([
'list',
'autocomplete',
'search'
])))

m.connect('/dataset/{action}/{id}/{revision}', action='read_ajax',
requirements=dict(action='|'.join([
'read',
'edit',
'history',
])))
m.connect('/dataset/{action}/{id}',
requirements=dict(action='|'.join([
'new_resource',
'history',
'read_ajax',
'history_ajax',
'follow',
'activity',
'groups',
'unfollow',
'delete',
'api_data',
])))
m.connect('dataset_edit', '/dataset/edit/{id}', action='edit',
ckan_icon='pencil-square-o')
m.connect('dataset_followers', '/dataset/followers/{id}',
action='followers', ckan_icon='users')
m.connect('dataset_activity', '/dataset/activity/{id}',
action='activity', ckan_icon='clock-o')
m.connect('/dataset/activity/{id}/{offset}', action='activity')
m.connect('dataset_groups', '/dataset/groups/{id}',
action='groups', ckan_icon='users')
m.connect('dataset_resources', '/dataset/resources/{id}',
action='resources', ckan_icon='bars')
m.connect('dataset_read', '/dataset/{id}', action='read',
ckan_icon='sitemap')
m.connect('/dataset/{id}/resource/{resource_id}',
action='resource_read')
m.connect('/dataset/{id}/resource_delete/{resource_id}',
action='resource_delete')
m.connect('resource_edit', '/dataset/{id}/resource_edit/{resource_id}',
action='resource_edit', ckan_icon='pencil-square-o')
m.connect('/dataset/{id}/resource/{resource_id}/download',
action='resource_download')
m.connect('/dataset/{id}/resource/{resource_id}/download/{filename}',
action='resource_download')
m.connect('/dataset/{id}/resource/{resource_id}/embed',
action='resource_embedded_dataviewer')
m.connect('/dataset/{id}/resource/{resource_id}/viewer',
action='resource_embedded_dataviewer', width="960",
height="800")
m.connect('/dataset/{id}/resource/{resource_id}/preview',
action='resource_datapreview')
m.connect('views', '/dataset/{id}/resource/{resource_id}/views',
action='resource_views', ckan_icon='bars')
m.connect('new_view', '/dataset/{id}/resource/{resource_id}/new_view',
action='edit_view', ckan_icon='pencil-square-o')
m.connect('edit_view',
'/dataset/{id}/resource/{resource_id}/edit_view/{view_id}',
action='edit_view', ckan_icon='pencil-square-o')
m.connect('resource_view',
'/dataset/{id}/resource/{resource_id}/view/{view_id}',
action='resource_view')
m.connect('/dataset/{id}/resource/{resource_id}/view/',
action='resource_view')

# group
map.redirect('/groups', '/group')
map.redirect('/groups/{url:.*}', '/group/{url}')
Expand Down Expand Up @@ -289,7 +217,9 @@ def make_map():
m.connect('organization_bulk_process',
'/organization/bulk_process/{id}',
action='bulk_process', ckan_icon='sitemap')
lib_plugins.register_package_plugins(map)

lib_plugins.register_package_plugins(current_app)

lib_plugins.register_group_plugins(map)

# tags
Expand Down
3 changes: 1 addition & 2 deletions ckan/controllers/feed.py
Expand Up @@ -401,8 +401,7 @@ def output_feed(self, results, feed_title, feed_description,

feed.add_item(
title=pkg.get('title', ''),
link=self.base_url + h.url_for(controller='package',
action='read',
link=self.base_url + h.url_for('dataset.read',
id=pkg['id']),
description=pkg.get('notes', ''),
updated=h.date_str_to_datetime(pkg.get('metadata_modified')),
Expand Down
2 changes: 1 addition & 1 deletion ckan/controllers/tag.py
Expand Up @@ -76,5 +76,5 @@ def read(self, id):
if asbool(config.get('ckan.legacy_templates', False)):
return base.render('tag/read.html')
else:
h.redirect_to(controller='package', action='search',
h.redirect_to('dataset.search',
tags=c.tag.get('name'))
1 change: 1 addition & 0 deletions ckan/lib/base.py
Expand Up @@ -9,6 +9,7 @@
import inspect
import sys


from pylons import cache
from pylons.controllers import WSGIController
from pylons.controllers.util import abort as _abort
Expand Down
2 changes: 1 addition & 1 deletion ckan/lib/cli.py
Expand Up @@ -739,7 +739,7 @@ def export_datasets(self, out_folder):
if not dd['state'] == 'active':
continue

url = h.url_for(controller='package', action='read', id=dd['name'])
url = h.url_for('dataset.read', id=dd['name'])

url = urljoin(fetch_url, url[1:]) + '.rdf'
try:
Expand Down
4 changes: 1 addition & 3 deletions ckan/lib/dictization/model_dictize.py
Expand Up @@ -109,8 +109,7 @@ def resource_dictize(res, context):
## in the frontend. Without for_edit the whole qualified url is returned.
if resource.get('url_type') == 'upload' and not context.get('for_edit'):
cleaned_name = munge.munge_filename(url)
resource['url'] = h.url_for(controller='package',
action='resource_download',
resource['url'] = h.url_for('resource.download',
id=resource['package_id'],
resource_id=res.id,
filename=cleaned_name,
Expand Down Expand Up @@ -764,4 +763,3 @@ def resource_view_list_dictize(resource_views, context):
for view in resource_views:
resource_view_dicts.append(resource_view_dictize(view, context))
return resource_view_dicts

42 changes: 24 additions & 18 deletions ckan/lib/helpers.py
Expand Up @@ -153,12 +153,12 @@ def redirect_to(*args, **kw):
import ckan.plugins.toolkit as toolkit
# Redirect to /dataset/my_dataset.
toolkit.redirect_to(controller='package', action='read',
toolkit.redirect_to('dataset.read',
id='my_dataset')
Or, using a named route::
toolkit.redirect_to('dataset_read', id='changed')
toolkit.redirect_to('dataset.read', id='changed')
If given a single string as argument, this redirects without url parsing
Expand Down Expand Up @@ -274,12 +274,12 @@ def url_for(*args, **kw):
URLs built by Pylons use the Routes syntax::
url_for(controller='package', action='read', id='my_dataset')
url_for(controller='my_ctrl', action='my_action', id='my_dataset')
# Returns '/dataset/my_dataset'
Or, using a named route::
url_for('dataset_read', id='changed')
url_for('dataset.read', id='changed')
# Returns '/dataset/changed'
Use ``qualified=True`` for a fully qualified URL when targeting a Pylons
Expand Down Expand Up @@ -953,7 +953,8 @@ def default_group_type():


@core_helper
def get_facet_items_dict(facet, limit=None, exclude_active=False):
def get_facet_items_dict(
facet, search_facets=None, limit=None, exclude_active=False):
'''Return the list of unselected facet items for the given facet, sorted
by count.
Expand All @@ -968,15 +969,19 @@ def get_facet_items_dict(facet, limit=None, exclude_active=False):
Arguments:
facet -- the name of the facet to filter.
search_facets -- dict with search facets(c.search_facets in Pylons)
limit -- the max. number of facet items to return.
exclude_active -- only return unselected facets.
'''
if not hasattr(c, u'search_facets') or not c.search_facets.get(
facet, {}).get(u'items'):
if search_facets is None:
search_facets = getattr(c, u'search_facets', None)

if not search_facets or not search_facets.get(
facet, {}).get('items'):
return []
facets = []
for facet_item in c.search_facets.get(facet)['items']:
for facet_item in search_facets.get(facet)['items']:
if not len(facet_item['name'].strip()):
continue
if not (facet, facet_item['name']) in request.params.items():
Expand All @@ -995,7 +1000,7 @@ def get_facet_items_dict(facet, limit=None, exclude_active=False):


@core_helper
def has_more_facets(facet, limit=None, exclude_active=False):
def has_more_facets(facet, search_facets, limit=None, exclude_active=False):
'''
Returns True if there are more facet items for the given facet than the
limit.
Expand All @@ -1006,12 +1011,13 @@ def has_more_facets(facet, limit=None, exclude_active=False):
Arguments:
facet -- the name of the facet to filter.
search_facets -- dict with search facets(c.search_facets in Pylons)
limit -- the max. number of facet items.
exclude_active -- only return unselected facets.
'''
facets = []
for facet_item in c.search_facets.get(facet)['items']:
for facet_item in search_facets.get(facet)['items']:
if not len(facet_item['name'].strip()):
continue
if not (facet, facet_item['name']) in request.params.items():
Expand Down Expand Up @@ -1044,7 +1050,8 @@ def unselected_facet_items(facet, limit=10):
limit -- the max. number of facet items to return.
'''
return get_facet_items_dict(facet, limit=limit, exclude_active=True)
return get_facet_items_dict(
facet, c.search_facets, limit=limit, exclude_active=True)


@core_helper
Expand Down Expand Up @@ -1082,7 +1089,7 @@ def _url_with_params(url, params):


def _search_url(params):
url = url_for(controller='package', action='search')
url = url_for('dataset.search')
return _url_with_params(url, params)


Expand Down Expand Up @@ -1590,7 +1597,7 @@ def dataset_link(package_or_package_dict):
text = dataset_display_name(package_or_package_dict)
return tags.link_to(
text,
url_for(controller='package', action='read', id=name)
url_for('dataset.read', id=name)
)


Expand All @@ -1614,8 +1621,7 @@ def resource_display_name(resource_dict):
@core_helper
def resource_link(resource_dict, package_id):
text = resource_display_name(resource_dict)
url = url_for(controller='package',
action='resource_read',
url = url_for('resource.read',
id=package_id,
resource_id=resource_dict['id'])
return tags.link_to(text, url)
Expand Down Expand Up @@ -1773,9 +1779,9 @@ def _create_url_with_params(params=None, controller=None, action=None,
''' internal function for building urls with parameters. '''

if not controller:
controller = c.controller
controller = getattr(c, 'controller', False) or request.blueprint
if not action:
action = c.action
action = getattr(c, 'action', False) or request.endpoint.split('.')[1]
if not extras:
extras = {}

Expand Down Expand Up @@ -2197,7 +2203,7 @@ def resource_preview(resource, package):
data_dict = {'resource': resource, 'package': package}

if datapreview.get_preview_plugin(data_dict, return_first=True):
url = url_for(controller='package', action='resource_datapreview',
url = url_for('resource.datapreview',
resource_id=resource['id'], id=package['id'],
qualified=True)
else:
Expand Down

0 comments on commit b788193

Please sign in to comment.