Skip to content

Commit

Permalink
Merge pull request #5189 from DataShades/package_type-in-urls
Browse files Browse the repository at this point in the history
Use current package_type for urls
  • Loading branch information
wardi committed Apr 1, 2020
2 parents 64af68c + ae6934b commit 6477467
Show file tree
Hide file tree
Showing 45 changed files with 635 additions and 203 deletions.
19 changes: 8 additions & 11 deletions ckan/lib/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1270,11 +1270,6 @@ def _url_with_params(url, params):
return url + u'?' + urlencode(params)


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


@core_helper
def sorted_extras(package_extras, auto_clean=False, subs=None, exclude=None):
''' Used for outputting package extras
Expand Down Expand Up @@ -1774,12 +1769,14 @@ def dataset_display_name(package_or_package_dict):
def dataset_link(package_or_package_dict):
if isinstance(package_or_package_dict, dict):
name = package_or_package_dict['name']
type_ = package_or_package_dict.get('type', 'dataset')
else:
name = package_or_package_dict.name
type_ = package_or_package_dict.type
text = dataset_display_name(package_or_package_dict)
return link_to(
text,
url_for('dataset.read', id=name)
url_for('{}.read'.format(type_), id=name)
)


Expand All @@ -1801,17 +1798,17 @@ def resource_display_name(resource_dict):


@core_helper
def resource_link(resource_dict, package_id):
def resource_link(resource_dict, package_id, package_type='dataset'):
text = resource_display_name(resource_dict)
url = url_for('resource.read',
url = url_for('{}_resource.read'.format(package_type),
id=package_id,
resource_id=resource_dict['id'])
return link_to(text, url)


@core_helper
def tag_link(tag):
url = url_for('dataset.search', tags=tag['name'])
def tag_link(tag, package_type='dataset'):
url = url_for('{}.search'.format(package_type), tags=tag['name'])
return link_to(tag.get('title', tag['name']), url)


Expand Down Expand Up @@ -2376,7 +2373,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('resource.datapreview',
url = url_for('{}_resource.datapreview'.format(package['type']),
resource_id=resource['id'], id=package['id'],
qualified=True)
else:
Expand Down
18 changes: 15 additions & 3 deletions ckan/lib/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ def register_package_plugins():
raise ValueError("An existing IDatasetForm is "
"already associated with the package type "
"'%s'" % package_type)

_package_plugins[package_type] = plugin

# Setup the fallback behaviour if one hasn't been defined.
Expand Down Expand Up @@ -147,14 +146,23 @@ def register_package_blueprints(app):
dataset.import_name,
url_prefix='/{}'.format(package_type),
url_defaults={'package_type': package_type})
if hasattr(plugin, 'prepare_dataset_blueprint'):
dataset_blueprint = plugin.prepare_dataset_blueprint(
package_type,
dataset_blueprint)
register_dataset_plugin_rules(dataset_blueprint)

app.register_blueprint(dataset_blueprint)

resource_blueprint = Blueprint(
u'{}_resource'.format(package_type),
resource.import_name,
url_prefix=u'/{}/<id>/resource'.format(package_type),
url_defaults={u'package_type': package_type})
if hasattr(plugin, 'prepare_resource_blueprint'):
resource_blueprint = plugin.prepare_resource_blueprint(
package_type,
resource_blueprint)
dataset_resource_rules(resource_blueprint)
app.register_blueprint(resource_blueprint)
log.debug(
Expand Down Expand Up @@ -260,8 +268,12 @@ def register_group_blueprints(app):
blueprint = Blueprint(group_type,
group.import_name,
url_prefix='/{}'.format(group_type),
url_defaults={u'group_type': group_type,
u'is_organization': is_organization})
url_defaults={
u'group_type': group_type,
u'is_organization': is_organization})
if hasattr(plugin, 'prepare_group_blueprint'):
blueprint = plugin.prepare_group_blueprint(
group_type, blueprint)
register_group_plugin_rules(blueprint)
app.register_blueprint(blueprint)

Expand Down
50 changes: 50 additions & 0 deletions ckan/plugins/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,39 @@ def validate(self, context, data_dict, schema, action):
:rtype: (dictionary, dictionary)
'''

def prepare_dataset_blueprint(self, package_type, blueprint):
u'''Update or replace dataset blueprint for given package type.
Internally CKAN registers blueprint for every custom dataset
type. Before default routes added to this blueprint and it
registered inside application this method is called. It can be
used either for registration of the view function under new
path or under existing path(like `/new`), in which case this
new function will be used instead of default one.
Note, this blueprint has prefix `/{package_type}`.
:rtype: flask.Blueprint
'''

def prepare_resource_blueprint(self, package_type, blueprint):
u'''Update or replace resource blueprint for given package type.
Internally CKAN registers separate resource blueprint for
every custom dataset type. Before default routes added to this
blueprint and it registered inside application this method is
called. It can be used either for registration of the view
function under new path or under existing path(like `/new`),
in which case this new function will be used instead of
default one.
Note, this blueprint has prefix `/{package_type}/<id>/resource`.
:rtype: flask.Blueprint
'''


class IGroupForm(Interface):
u'''
Expand Down Expand Up @@ -1477,6 +1510,23 @@ def validate(self, context, data_dict, schema, action):
:rtype: (dictionary, dictionary)
'''

def prepare_group_blueprint(self, group_type, blueprint):
u'''Update or replace group blueprint for given group type.
Internally CKAN registers separate blueprint for
every custom group type. Before default routes added to this
blueprint and it registered inside application this method is
called. It can be used either for registration of the view
function under new path or under existing path(like `/new`),
in which case this new function will be used instead of
default one.
Note, this blueprint has prefix `/{group_type}`.
:rtype: flask.Blueprint
'''

# End of hooks ############################################################


Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/admin/trash.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ul class="user-list">
{% for pkg in deleted_packages %}
{% set title = pkg.title or pkg.name %}
<li>{{ h.link_to(h.truncate(title, truncate_title), h.url_for('dataset.read', id=pkg.name)) }}</li>
<li>{{ h.link_to(h.truncate(title, truncate_title), h.url_for(pkg.type ~ '.read', id=pkg.name)) }}</li>
{% endfor %}

</ul>
Expand Down
4 changes: 2 additions & 2 deletions ckan/templates/organization/bulk_process.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ <h3 class="page-heading">
<input type="checkbox" name="dataset_{{ package.id }}">
</td>
<td class="context">
<a href="{% url_for 'dataset.edit', id=package.name %}" class="edit pull-right">
<a href="{% url_for package.type ~ '.edit', id=package.name %}" class="edit pull-right">
{{ _('Edit') }}
</a>
<h3 class="dataset-heading">
{{ h.link_to(h.truncate(title, truncate_title), h.url_for('dataset.read', id=package.name)) }}
{{ h.link_to(h.truncate(title, truncate_title), h.url_for(package.type ~ '.read', id=package.name)) }}
{% if package.get('state', '').startswith('draft') %}
<span class="label label-info">{{ _('Draft') }}</span>
{% elif package.get('state', '').startswith('deleted') %}
Expand Down
13 changes: 7 additions & 6 deletions ckan/templates/package/base.html
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
{% extends "page.html" %}

{% set pkg = pkg_dict %}
{% set dataset_type = dataset_type or pkg.type or 'dataset' %}

{% block breadcrumb_content_selected %} class="active"{% endblock %}

{% block subtitle %}{{ _('Datasets') }}{% endblock %}
{% block subtitle %}{{ _(dataset_type.title()) }}{% endblock %}

{% block breadcrumb_content %}
{% if pkg %}
{% set dataset = h.dataset_display_name(pkg) %}
{% if pkg.organization %}
{% set organization = h.get_translated(pkg.organization, 'title') or pkg.organization.name %}
{% set group_type = pkg.organization.type %}
<li>{% link_for _('Organizations'), named_route='organization.index' %}</li>
<li>{% link_for organization|truncate(30), named_route='organization.read', id=pkg.organization.name %}</li>
<li>{% link_for _('Organizations'), named_route=pkg.organization.type ~ '.index' %}</li>
<li>{% link_for organization|truncate(30), named_route=pkg.organization.type ~ '.read', id=pkg.organization.name %}</li>
{% else %}
<li>{% link_for _('Datasets'), named_route='dataset.search' %}</li>
<li>{% link_for _(dataset_type.title()), named_route=dataset_type ~ '.search' %}</li>
{% endif %}
<li{{ self.breadcrumb_content_selected() }}>{% link_for dataset|truncate(30), named_route='dataset.read', id=pkg.id if is_activity_archive else pkg.name %}</li>
<li{{ self.breadcrumb_content_selected() }}>{% link_for dataset|truncate(30), named_route=pkg.type ~ '.read', id=pkg.id if is_activity_archive else pkg.name %}</li>
{% else %}
<li>{% link_for _('Datasets'), named_route='dataset.search' %}</li>
<li>{% link_for _(dataset_type.title()), named_route=dataset_type ~ '.search' %}</li>
<li class="active"><a href="">{{ _('Create Dataset') }}</a></li>
{% endif %}
{% endblock %}
6 changes: 3 additions & 3 deletions ckan/templates/package/changes.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

{% block breadcrumb_content %}
{{ super() }}
<li>{% link_for _('Changes'), named_route='dataset.activity', id=pkg_dict.name %}</li>
<li class="active">{% link_for activity_diffs[0].activities[1].id|truncate(30), named_route='dataset.changes', id=activity_diffs[0].activities[1].id %}</li>
<li>{% link_for _('Changes'), named_route=dataset_type ~ '.activity', id=pkg_dict.name %}</li>
<li class="active">{% link_for activity_diffs[0].activities[1].id|truncate(30), named_route=dataset_type ~ '.changes', id=activity_diffs[0].activities[1].id %}</li>
{% endblock %}

{% block primary %}
Expand All @@ -19,7 +19,7 @@ <h1 class="page-heading">{{ _('Changes') }}</h1>

{% set select_list1 = h.activity_list_select(pkg_activity_list, activity_diffs[-1].activities[0].id) %}
{% set select_list2 = h.activity_list_select(pkg_activity_list, activity_diffs[0].activities[1].id) %}
<form id="range_form" action="{{ h.url_for('dataset.changes_multiple') }}" data-module="select-switch" data-module-target="">
<form id="range_form" action="{{ h.url_for(dataset_type ~ '.changes_multiple') }}" data-module="select-switch" data-module-target="">
<input type="hidden" name="current_old_id" value="{{ activity_diffs[-1].activities[0].id }}">
<input type="hidden" name="current_new_id" value="{{ activity_diffs[0].activities[1].id }}">
View changes from
Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/package/confirm_delete.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{% set dataset = h.dataset_display_name(pkg_dict) %}
<p>{{ _('Are you sure you want to delete dataset - {name}?').format(name=dataset) }}</p>
<p class="form-actions">
<form id='confirm-dataset-delete-form' action="{% url_for 'dataset.delete', id=pkg_dict.name %}" method="post">
<form id='confirm-dataset-delete-form' action="{% url_for pkg_dict.type ~ '.delete', id=pkg_dict.name %}" method="post">
<button class="btn btn-danger" type="submit" name="cancel" >{{ _('Cancel') }}</button>
<button class="btn btn-primary" type="submit" name="delete" >{{ _('Confirm Delete') }}</button>
</form>
Expand Down
8 changes: 4 additions & 4 deletions ckan/templates/package/edit_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
{% block breadcrumb_content %}
{{ super() }}
{% if pkg %}
<li class="active">{% link_for _('Edit'), named_route='dataset.edit', id=pkg.name %}</li>
<li class="active">{% link_for _('Edit'), named_route=pkg.type ~ '.edit', id=pkg.name %}</li>
{% endif %}
{% endblock %}

{% block content_action %}
{% link_for _('View dataset'), named_route='dataset.read', id=pkg.name, class_='btn btn-default', icon='eye' %}
{% link_for _('View dataset'), named_route=pkg.type ~ '.read', id=pkg.name, class_='btn btn-default', icon='eye' %}
{% endblock %}

{% block content_primary_nav %}
{{ h.build_nav_icon('dataset.edit', _('Edit metadata'), id=pkg.name, icon='pencil-square-o') }}
{{ h.build_nav_icon('dataset.resources', _('Resources'), id=pkg.name, icon='bars') }}
{{ h.build_nav_icon(pkg.type ~ '.edit', _('Edit metadata'), id=pkg.name, icon='pencil-square-o') }}
{{ h.build_nav_icon(pkg.type ~ '.resources', _('Resources'), id=pkg.name, icon='bars') }}
{% endblock %}

{% block secondary_content %}
Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/package/read.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{% block package_archive_notice %}
{% if is_activity_archive %}
<div class="alert alert-danger">
{% trans url=h.url_for('dataset.read', id=pkg.id) %}
{% trans url=h.url_for(pkg.type ~ '.read', id=pkg.id) %}
You're currently viewing an old version of this dataset. To see the
current version, click <a href="{{ url }}">here</a>.
{% endtrans %}
Expand Down
8 changes: 4 additions & 4 deletions ckan/templates/package/read_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
{% block content_action %}
{% if not is_activity_archive %}
{% if h.check_access('package_update', {'id':pkg.id }) %}
{% link_for _('Manage'), named_route='dataset.edit', id=pkg.name, class_='btn btn-default', icon='wrench' %}
{% link_for _('Manage'), named_route=pkg.type ~ '.edit', id=pkg.name, class_='btn btn-default', icon='wrench' %}
{% endif %}
{% endif %}
{% endblock %}

{% block content_primary_nav %}
{{ h.build_nav_icon('dataset.read', _('Dataset'), id=pkg.id if is_activity_archive else pkg.name, icon='sitemap') }}
{{ h.build_nav_icon('dataset.groups', _('Groups'), id=pkg.id if is_activity_archive else pkg.name, icon='users') }}
{{ h.build_nav_icon('dataset.activity', _('Activity Stream'), id=pkg.id if is_activity_archive else pkg.name, icon='clock-o') }}
{{ h.build_nav_icon(dataset_type ~ '.read', _('Dataset'), id=pkg.id if is_activity_archive else pkg.name, icon='sitemap') }}
{{ h.build_nav_icon(dataset_type ~ '.groups', _('Groups'), id=pkg.id if is_activity_archive else pkg.name, icon='users') }}
{{ h.build_nav_icon(dataset_type ~ '.activity', _('Activity Stream'), id=pkg.id if is_activity_archive else pkg.name, icon='clock-o') }}
{% endblock %}

{% block secondary_content %}
Expand Down
10 changes: 5 additions & 5 deletions ckan/templates/package/resource_edit_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
{% block breadcrumb_content %}
{{ super() }}
{% if res %}
<li>{% link_for h.resource_display_name(res)|truncate(30), named_route='resource.read', id=pkg.name, resource_id=res.id %}</li>
<li>{% link_for h.resource_display_name(res)|truncate(30), named_route=pkg.type ~ '_resource.read', id=pkg.name, resource_id=res.id %}</li>
<li{% block breadcrumb_edit_selected %} class="active"{% endblock %}><a href="">{{ _('Edit') }}</a></li>
{% endif %}
{% endblock %}

{% block content_action %}
{% link_for _('All resources'), named_route='dataset.resources', id=pkg.name, class_='btn btn-default', icon='arrow-left' %}
{% link_for _('All resources'), named_route=pkg.type ~ '.resources', id=pkg.name, class_='btn btn-default', icon='arrow-left' %}
{% if res %}
{% link_for _('View resource'), named_route='resource.read', id=pkg.name, resource_id=res.id, class_='btn btn-default', icon='eye' %}
{% link_for _('View resource'), named_route=pkg.type ~ '_resource.read', id=pkg.name, resource_id=res.id, class_='btn btn-default', icon='eye' %}
{% endif %}
{% endblock %}

{% block content_primary_nav %}
{{ h.build_nav_icon('resource.edit', _('Edit resource'), id=pkg.name, resource_id=res.id, icon='pencil-square-o') }}
{{ h.build_nav_icon(pkg.type ~ '_resource.edit', _('Edit resource'), id=pkg.name, resource_id=res.id, icon='pencil-square-o') }}
{% block inner_primary_nav %}{% endblock %}
{{ h.build_nav_icon('resource.views', _('Views'), id=pkg.name, resource_id=res.id, icon='bars') }}
{{ h.build_nav_icon(pkg.type ~ '_resource.views', _('Views'), id=pkg.name, resource_id=res.id, icon='bars') }}
{% endblock %}

{% block primary_content_inner %}
Expand Down
6 changes: 3 additions & 3 deletions ckan/templates/package/resource_read.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<ul>
{% block resource_actions_inner %}
{% if h.check_access('package_update', {'id':pkg.id }) and not is_activity_archive %}
<li>{% link_for _('Manage'), named_route='resource.edit', id=pkg.name, resource_id=res.id, class_='btn btn-default', icon='wrench' %}</li>
<li>{% link_for _('Manage'), named_route=pkg.type ~ '_resource.edit', id=pkg.name, resource_id=res.id, class_='btn btn-default', icon='wrench' %}</li>
{% endif %}
{% if res.url and h.is_url(res.url) %}
<li>
Expand Down Expand Up @@ -74,7 +74,7 @@
{% block package_archive_notice %}
{% if is_activity_archive %}
<div id="activity-archive-notice" class="alert alert-danger">
{% trans url=h.url_for('dataset.read', id=pkg.id) %}
{% trans url=h.url_for(pkg.type ~ '.read', id=pkg.id) %}
You're currently viewing an old version of this dataset. To see the
current version, click <a href="{{ url }}">here</a>.
{% endtrans %}
Expand All @@ -96,7 +96,7 @@
{% if not res.description and package.notes %}
<h3>{{ _('Dataset description:') }}</h3>
<blockquote>{{ h.markdown_extract(h.get_translated(package, 'notes')) }}</blockquote>
<p>{% trans dataset=package.title, url=h.url_for('dataset.read', id=package.id if is_activity_archive else package.name) %}Source: <a href="{{ url }}">{{ dataset }}</a>{% endtrans %}
<p>{% trans dataset=package.title, url=h.url_for(package.type ~ '.read', id=package.id if is_activity_archive else package.name) %}Source: <a href="{{ url }}">{{ dataset }}</a>{% endtrans %}
{% endif %}
</div>
{% endblock %}
Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/package/resource_views.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</a>
<ul class="dropdown-menu">
{% for option in h.get_allowed_view_types(resource, pkg_dict) %}
{% set url = h.url_for('resource.edit_view', id=pkg_dict.name, resource_id=resource.id, view_type=option[0]) %}
{% set url = h.url_for(pkg_dict.type ~ '_resource.edit_view', id=pkg_dict.name, resource_id=resource.id, view_type=option[0]) %}
<li><a href="{{ url }}"><i class="fa fa-{{ option[2] }}"></i> {{ option[1] }}</a></li>
{% endfor %}
</ul>
Expand Down
4 changes: 2 additions & 2 deletions ckan/templates/package/resources.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{% block subtitle %}{{ _('Resources') }} {{ g.template_title_delimiter }} {{ h.dataset_display_name(pkg) }}{% endblock %}

{% block page_primary_action %}
{% link_for _('Add new resource'), named_route='resource.new', id=pkg_dict.name, class_='btn btn-primary', icon='plus' %}
o {% link_for _('Add new resource'), named_route=pkg_dict.type ~ '_resource.new', id=pkg_dict.name, class_='btn btn-primary', icon='plus' %}
{% endblock %}

{% block primary_content_inner %}
Expand All @@ -17,7 +17,7 @@
{% endfor %}
</ul>
{% else %}
{% trans url=h.url_for('resource.new', id=pkg.name) %}
{% trans url=h.url_for(pkg.type ~ '_resource.new', id=pkg.name) %}
<p class="empty">This dataset has no data, <a href="{{ url }}">why not add some?</a></p>
{% endtrans %}
{% endif %}
Expand Down
Loading

0 comments on commit 6477467

Please sign in to comment.