Skip to content

Commit

Permalink
Sanitize blueprints to Flask syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
pdelboca committed Feb 28, 2023
1 parent 339cdd9 commit 6090ad8
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 51 deletions.
4 changes: 2 additions & 2 deletions ckanext/harvest/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def package_list_for_source(source_id):
query = logic.get_action('package_search')(context, search_dict)

base_url = h.url_for(
'{0}_read'.format(DATASET_TYPE_NAME),
'{0}.read'.format(DATASET_TYPE_NAME),
id=harvest_source['name']
)

Expand Down Expand Up @@ -124,7 +124,7 @@ def link_for_harvest_object(id=None, guid=None, text=None):
obj = logic.get_action('harvest_object_show')(context, {'id': guid, 'attr': 'guid'})
id = obj.id

url = h.url_for('harvest_object_show', id=id)
url = h.url_for('harvest.object_show', id=id)
text = text or guid or id
link = '<a href="{url}">{text}</a>'.format(url=url, text=text)

Expand Down
2 changes: 1 addition & 1 deletion ckanext/harvest/logic/action/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ def get_mail_extra_vars(context, source_id, status):
errors = job_errors + obj_errors

site_url = config.get('ckan.site_url')
job_url = toolkit.url_for('harvest_job_show', source=source['id'], id=last_job['id'])
job_url = toolkit.url_for('harvest.job_show', source=source['id'], id=last_job['id'])
full_job_url = urljoin(site_url, job_url)
extra_vars = {
'organization': organization,
Expand Down
21 changes: 0 additions & 21 deletions ckanext/harvest/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,27 +281,6 @@ def update_config(self, config):
p.toolkit.add_resource('assets', 'ckanext-harvest')
p.toolkit.add_resource('public/ckanext/harvest/javascript', 'harvest-extra-field')

if p.toolkit.check_ckan_version(min_version='2.9.0'):
mappings = config.get('ckan.legacy_route_mappings') or {}
if mappings and isinstance(mappings, string_types):
mappings = json.loads(mappings)

mappings.update({
'harvest_read': 'harvest.read',
'harvest_edit': 'harvest.edit',
})
bp_routes = [
"delete", "refresh", "admin", "about",
"clear", "job_list", "job_show_last", "job_show",
"job_abort", "object_show"
]
mappings.update({
'harvest_' + route: 'harvester.' + route
for route in bp_routes
})
# https://github.com/ckan/ckan/pull/4521
config['ckan.legacy_route_mappings'] = json.dumps(mappings)

# IActions

def get_actions(self):
Expand Down
2 changes: 1 addition & 1 deletion ckanext/harvest/templates/snippets/add_source_button.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% set authorized_user = h.check_access('harvest_source_create') %}

{% if authorized_user %}
<a href="{{ h.url_for('{0}_new'.format(dataset_type)) }}" class="btn btn-primary">
<a href="{{ h.url_for('{0}.new'.format(dataset_type)) }}" class="btn btn-primary">
<i class="fa fa-plus-square icon-plus-sign-alt"></i>
{{ _('Add Harvest Source') }}
</a>
Expand Down
4 changes: 2 additions & 2 deletions ckanext/harvest/templates/snippets/source_item.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
{% set truncate_title = truncate_title or 80 %}
{% set title = source.title or source.name %}
{% set source_type = h.get_pkg_dict_extra(source, 'source_type') %}
{% set url = h.url_for('harvest_admin', id=source.name) if within_organization else h.url_for('harvest_read', id=source.name) %}
{% set url = h.url_for('harvest_admin', id=source.name) if within_organization else h.url_for('harvest.read', id=source.name) %}

<li class="{{ item_class or "dataset-item" }}">
<div class="dataset-content">
Expand Down Expand Up @@ -47,7 +47,7 @@ <h3 class="dataset-heading">
{{ _('Datasets') }}: {{ source.status.total_datasets }}
{% endif %}
{% if not within_organization and source.organization %}
&mdash; {{ _('Organization') }}: {{ h.link_to(source.organization.title or source.organization.name, h.url_for('organization_read', id=source.organization.name)) }}</a>
&mdash; {{ _('Organization') }}: {{ h.link_to(source.organization.title or source.organization.name, h.url_for('organization.read', id=source.organization.name)) }}</a>
{% endif %}
</p>

Expand Down
2 changes: 1 addition & 1 deletion ckanext/harvest/templates/source/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ <h1>{{ _('Last Harvest Job') }}</h1>
{% if harvest_source.status and harvest_source.status.last_job %}
{% snippet "snippets/job_details.html", job=harvest_source.status.last_job %}
<div class="form-actions">
<a href="{{ h.url_for('harvest_job_show_last', source=harvest_source.name) }}" class="btn pull-right btn-default">
<a href="{{ h.url_for('harvester.job_show_last', source=harvest_source.name) }}" class="btn pull-right btn-default">
<i class="fa fa-briefcase icon-briefcase"></i>
{{ _('View full job report') }}
</a>
Expand Down
8 changes: 4 additions & 4 deletions ckanext/harvest/templates/source/admin_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@
<a class="btn btn-default disabled" rel="tooltip" title="There already is an unrun job for this source"><i class="fa fa-lg fa-refresh icon-refresh icon-large"></i> Reharvest</a>
{% else %}
{% set locale = h.dump_json({'content': _('This will re-run the harvesting for this source. Any updates at the source will overwrite the local datasets. Sources with a large number of datasets may take a significant amount of time to finish harvesting. Please confirm you would like us to start reharvesting.')}) %}
<a href="{{ h.url_for('harvest_refresh', id=harvest_source.id) }}" class="btn btn-default" data-module="confirm-action" data-module-i18n="{{ locale }}"
<a href="{{ h.url_for('harvester.refresh', id=harvest_source.id) }}" class="btn btn-default" data-module="confirm-action" data-module-i18n="{{ locale }}"
title="{{ _('Start a new harvesting job for this harvest source now') }}">
<i class="fa fa-refresh icon-refresh"></i>
{{ _('Reharvest') }}
</a>
{% endif %}
{% if harvest_source.status and harvest_source.status.last_job and (harvest_source.status.last_job.status == 'Running') %}
<a href="{{ h.url_for('harvest_job_abort', source=harvest_source.name, id=harvest_source.status.last_job.id) }}" class="btn btn-default" title="Stop this Job">
<a href="{{ h.url_for('harvester.job_abort', source=harvest_source.name, id=harvest_source.status.last_job.id) }}" class="btn btn-default" title="Stop this Job">
<i class="fa fa-ban icon-ban-circle"></i>
{{ _('Stop') }}
</a>
{% endif %}
{% set locale = h.dump_json({'content': _('Warning: This will remove all datasets for this source, as well as all previous job reports. Are you sure you want to continue?')}) %}
<a href="{{ h.url_for('harvest_clear', id=harvest_source.id) }}" class="btn btn-default" data-module="confirm-action" data-module-i18n="{{ locale }}"
<a href="{{ h.url_for('harvester.clear', id=harvest_source.id) }}" class="btn btn-default" data-module="confirm-action" data-module-i18n="{{ locale }}"
title="{{ _('Delete all harvest jobs and existing datasets from this source') }}">
{{ _('Clear') }}
</a>
<a href="{{ h.url_for('{0}_read'.format(c.dataset_type), id=harvest_source.id) }}" class="btn btn-default">
<a href="{{ h.url_for('{0}.read'.format(c.dataset_type), id=harvest_source.id) }}" class="btn btn-default">
<i class="fa fa-eye eye-open"></i>
{{ _('View harvest source') }}
</a>
Expand Down
2 changes: 1 addition & 1 deletion ckanext/harvest/templates/source/job/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h1 class="results">{{ _('Harvest Jobs') }}</h1>
<li class="dataset-item">
<div class="dataset-content">
<h3 class="dataset-heading">
<a href="{{ h.url_for('harvest_job_show', source=harvest_source.name, id=job.id) }}">
<a href="{{ h.url_for('harvester.job_show', source=harvest_source.name, id=job.id) }}">
{{ _('Job: ') }} {{ job.id }}
</a>
{% if job.status != 'Finished' %}
Expand Down
4 changes: 2 additions & 2 deletions ckanext/harvest/templates/source/job/read.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<div class="module-content">

<p class="pull-right">
{{ h.nav_link(_('Back to job list'), named_route='harvest_job_list', source=harvest_source.name, class_='btn btn-default', icon='arrow-left')}}
{{ h.nav_link(_('Back to job list'), named_route='harvester.job_list', source=harvest_source.name, class_='btn btn-default', icon='arrow-left')}}
</p>

<h1>{{ _('Job Report') }}</h1>
Expand Down Expand Up @@ -69,7 +69,7 @@ <h3>{{ _('Document Errors') }}
{{ _('Remote content') }}
</a>
{% endif %}
<a href="{{ h.url_for('harvest_object_show', id=harvest_object_id) }}" class="btn btn-small">
<a href="{{ h.url_for('harvest.object_show', id=harvest_object_id) }}" class="btn btn-small">
{{ _('Local content') }}
</a>

Expand Down
2 changes: 1 addition & 1 deletion ckanext/harvest/templates/source/new_source_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{{ form.input('title', id='field-title', label=_('Title'), placeholder=_('eg. A descriptive title'), value=data.title, error=errors.title, classes=['control-full'], attrs={'data-module': 'slug-preview-target'}) }}

{% set prefix = 'harvest' %}
{% set domain = h.url_for('{0}_read'.format(c.dataset_type), id='', qualified=true) %}
{% set domain = h.url_for('{0}.read'.format(c.dataset_type), id='', qualified=true) %}
{% set domain = domain|replace("http://", "")|replace("https://", "") %}
{% set attrs = {'data-module': 'slug-preview-slug', 'data-module-prefix': domain, 'data-module-placeholder': '<harvest-source>'} %}

Expand Down
4 changes: 2 additions & 2 deletions ckanext/harvest/templates/source/read_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h1 class="heading">{{ harvest_source.title }}</h1>
{% if harvest_source.notes %}
<p>
{{ h.markdown_extract(harvest_source.notes, 180) }}
{{ h.nav_link(_('read more'), named_route='{0}_about'.format(c.dataset_type), id=harvest_source.name) }}
{{ h.nav_link(_('read more'), named_route='harvester.about', id=harvest_source.name) }}
</p>
{% else %}
<p class="empty">{{ _('There is no description for this harvest source') }}</p>
Expand All @@ -29,7 +29,7 @@ <h1 class="heading">{{ harvest_source.title }}</h1>
{% block content_action %}
<div class="content_action">
{% if h.check_access('harvest_source_update', {'id':harvest_source.id }) %}
{{ h.nav_link(_('Admin'), named_route='{0}_admin'.format(c.dataset_type), id=harvest_source.name, class_='btn btn-primary', icon='wrench')}}
{{ h.nav_link(_('Admin'), named_route='harvester.admin', id=harvest_source.name, class_='btn btn-primary', icon='wrench')}}
{% endif %}
</div>
{% endblock %}
Expand Down
2 changes: 1 addition & 1 deletion ckanext/harvest/templates/source/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@

{% block secondary_content %}
{% for facet in c.facet_titles %}
{{ h.snippet('snippets/facet_list.html', title=c.facet_titles[facet], name=facet, alternative_url=h.url_for('{0}_search'.format(c.dataset_type))) }}
{{ h.snippet('snippets/facet_list.html', title=c.facet_titles[facet], name=facet, alternative_url=h.url_for('{0}.search'.format(c.dataset_type))) }}
{% endfor %}
{% endblock %}
16 changes: 8 additions & 8 deletions ckanext/harvest/tests/test_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_index_page_is_rendered(self, app):

def test_new_form_is_rendered(self, app):

url = url_for('harvest_new')
url = url_for('harvest.new')
sysadmin = factories.Sysadmin()
env = {"REMOTE_USER": sysadmin['name'].encode('ascii')}

Expand All @@ -40,7 +40,7 @@ def test_edit_form_is_rendered(self, app):

source = harvest_factories.HarvestSource()

url = url_for('harvest_edit', id=source['id'])
url = url_for('harvest.edit', id=source['id'])
sysadmin = factories.Sysadmin()
env = {"REMOTE_USER": sysadmin['name'].encode('ascii')}

Expand All @@ -52,7 +52,7 @@ def test_source_page_rendered(self, app):

source = harvest_factories.HarvestSource()

url = url_for('harvest_read', id=source['name'])
url = url_for('harvest.read', id=source['name'])
sysadmin = factories.Sysadmin()
env = {"REMOTE_USER": sysadmin['name'].encode('ascii')}

Expand All @@ -68,7 +68,7 @@ def test_admin_page_rendered(self, app):
sysadmin = factories.Sysadmin()
env = {"REMOTE_USER": sysadmin['name'].encode('ascii')}

url = url_for('harvest_admin', id=source_obj.id)
url = url_for('harvester.admin', id=source_obj.id)

response = app.get(url, extra_environ=env)

Expand All @@ -80,7 +80,7 @@ def test_about_page_rendered(self, app):

source = harvest_factories.HarvestSource()

url = url_for('harvest_about', id=source['name'])
url = url_for('harvester.about', id=source['name'])
sysadmin = factories.Sysadmin()
env = {"REMOTE_USER": sysadmin['name'].encode('ascii')}

Expand All @@ -95,7 +95,7 @@ def test_job_page_rendered(self, app):
sysadmin = factories.Sysadmin()
env = {"REMOTE_USER": sysadmin['name'].encode('ascii')}

url = url_for('harvest_job_list', source=job['source_id'])
url = url_for('harvester.job_list', source=job['source_id'])

response = app.get(url, extra_environ=env)

Expand All @@ -108,7 +108,7 @@ def test_job_show_last_page_rendered(self, app):
sysadmin = factories.Sysadmin()
env = {"REMOTE_USER": sysadmin['name'].encode('ascii')}

url = url_for('harvest_job_show_last', source=job['source_id'])
url = url_for('harvester.job_show_last', source=job['source_id'])

response = app.get(url, extra_environ=env)

Expand All @@ -119,7 +119,7 @@ def test_job_show_page_rendered(self, app):
job = harvest_factories.HarvestJob()

url = url_for(
'harvest_job_show', source=job['source_id'], id=job['id'])
'harvester.job_show', source=job['source_id'], id=job['id'])
sysadmin = factories.Sysadmin()
env = {"REMOTE_USER": sysadmin['name'].encode('ascii')}

Expand Down
8 changes: 4 additions & 4 deletions ckanext/harvest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ def job_abort_view(source, id):
return tk.abort(500, msg)

return h.redirect_to(
h.url_for('{0}_admin'.format(DATASET_TYPE_NAME), id=source))
h.url_for('{0}.admin'.format(DATASET_TYPE_NAME), id=source))


def refresh_view(id):
Expand Down Expand Up @@ -674,7 +674,7 @@ def refresh_view(id):
h.flash_error(msg)

return h.redirect_to(
h.url_for('{0}_admin'.format(DATASET_TYPE_NAME), id=id))
h.url_for('{0}.admin'.format(DATASET_TYPE_NAME), id=id))


def clear_view(id):
Expand All @@ -691,7 +691,7 @@ def clear_view(id):
h.flash_error(msg)

return h.redirect_to(
h.url_for('{0}_admin'.format(DATASET_TYPE_NAME), id=id))
h.url_for('{0}.admin'.format(DATASET_TYPE_NAME), id=id))


def delete_view(id):
Expand All @@ -712,7 +712,7 @@ def delete_view(id):
h.flash_success(_('Harvesting source successfully inactivated'))

return h.redirect_to(
h.url_for('{0}_admin'.format(DATASET_TYPE_NAME), id=id))
h.url_for('{0}.admin'.format(DATASET_TYPE_NAME), id=id))
except tk.ObjectNotFound:
return tk.abort(404, _('Harvest source not found'))
except tk.NotAuthorized:
Expand Down
2 changes: 2 additions & 0 deletions ckanext/harvest/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import ckanext.harvest.utils as utils

# IDatasetForm provides a "harvest" blueprint for the package type harvest.
# We name the extension blueprint "harvester" to avoid clashing of names.
harvester = Blueprint("harvester", __name__)


Expand Down

0 comments on commit 6090ad8

Please sign in to comment.