Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backport scheming draft_fields_required=false support #1

Open
wants to merge 7 commits into
base: twdh-patches-2.10.5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/8308.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Publish buttons for draft datasets, consistent edit UI and error handling
2 changes: 1 addition & 1 deletion ckan/lib/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def resource_template(self) -> str:
return 'package/resource_read.html'

def package_form(self) -> str:
return 'package/new_package_form.html'
return 'package/snippets/package_form.html'

def resource_form(self) -> str:
return 'package/snippets/resource_form.html'
Expand Down
27 changes: 3 additions & 24 deletions ckan/templates/package/new_package_form.html
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
{% extends 'package/snippets/package_form.html' %}

{% block stages %}
{% if form_style != 'edit' %}
{{ super() }}
{% endif %}
{% endblock %}
{# this template exists for backwards compatibility with old
IDatasetForm plugins.

{% block save_button_text %}
{% if form_style != 'edit' %}
{{ super() }}
{% else %}
{{ h.humanize_entity_type('package', pkg_dict.type, 'update label') or _('Update Dataset') }}
{% endif %}
{% endblock %}

{% block cancel_button %}
{% if form_style != 'edit' %}
{{ super() }}
{% endif %}
{% endblock %}

{% block delete_button %}
{% if form_style == 'edit' and h.check_access('package_delete', {'id': pkg_dict.id}) %}
{{ super() }}
{% endif %}
{% endblock %}
Please extend package/snippets/package_form.html instead. #}
1 change: 1 addition & 0 deletions ckan/templates/package/new_resource.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

{% block secondary_content %}
{% snippet 'package/snippets/resource_help.html' %}
{% snippet 'package/snippets/resources.html', pkg=pkg %}
{% endblock %}

{% block scripts %}
Expand Down
4 changes: 2 additions & 2 deletions ckan/templates/package/read.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ <h1>
{% block page_heading %}
{{ h.dataset_display_name(pkg) }}
{% if pkg.state.startswith('draft') %}
[{{ _('Draft') }}]
<span class="badge bg-info">{{ _('Draft') }}</span>
{% endif %}
{% if pkg.state == 'deleted' %}
[{{ _('Deleted') }}]
<span class="badge bg-info">{{ _('Deleted') }}</span>
{% endif %}
{% endblock %}
</h1>
Expand Down
16 changes: 13 additions & 3 deletions ckan/templates/package/snippets/package_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
<form id="dataset-edit" class="dataset-form" method="post" action="{{ action }}" data-module="basic-form" novalidate>
{{ h.csrf_input() }}
{% block stages %}
{{ h.snippet('package/snippets/stages.html', stages=stage, dataset_type=dataset_type) }}
{% if form_style != 'edit' %}
twdbben marked this conversation as resolved.
Show resolved Hide resolved
{% snippet 'package/snippets/stages.html', stages=stage, dataset_type=dataset_type %}
{% endif %}
{% endblock %}

<input type="hidden" name="_ckan_phase" value="dataset_new_1" />
Expand Down Expand Up @@ -36,12 +38,20 @@
</p>
{% endblock %}
{% block delete_button %}
{% if h.check_access('package_delete', {'id': data.id}) and not data.state == 'deleted' %}
{% if form_style == 'edit' and h.check_access('package_delete', {'id': data.id}) and not data.state == 'deleted' %}
<a class="btn btn-danger pull-left" href="{% url_for dataset_type ~ '.delete', id=data.id %}" data-module="confirm-action" data-module-content="{{ h.humanize_entity_type('package', dataset_type, 'delete confirmation') or _('Are you sure you want to delete this dataset?') }}">{% block delete_button_text %}{{ _('Delete') }}{% endblock %}</a>
{% endif %}
{% endblock %}
{% block save_button %}
<button class="btn btn-primary" type="submit" name="save">{% block save_button_text %}{{ _('Next: Add Data') }}{% endblock %}</button>
<button class="btn btn-primary" type="submit" name="save">
{%- block save_button_text -%}
{%- if form_style == 'edit' -%}
{{ h.humanize_entity_type('package', pkg_dict.type, 'update label') or _('Update Dataset') }}
{%- else -%}
{{ _('Next: Add Data') }}
{%- endif -%}
{%- endblock -%}
</button>
{% endblock %}
{{ form.required_message() }}
</div>
Expand Down
6 changes: 6 additions & 0 deletions ckan/templates/package/snippets/resources.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ <h2 class="module-heading"><i class="fa fa-copy"></i> {{ _("Resources") }}</h2>
</section>
{% endblock %}
{% endif %}

{% if can_edit and active and not is_activity_archive %}
<div class="module-content">
{% link_for _('Add new resource'), named_route=pkg.type ~ '_resource.new', id=pkg.name, class_='btn btn-light btn-sm', icon='plus' %}
</div>
{% endif %}
10 changes: 5 additions & 5 deletions ckan/tests/controllers/test_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ def test_first_page_creates_draft_package(self, app, user):

def test_resource_required(self, app, user):
url = url_for("dataset.new")
name = "one-resource-required"
env = {"Authorization": user["token"]}
response = app.post(url, extra_environ=env, data={
name = "resource-data-required"
headers = {"Authorization": user["token"]}
response = app.post(url, headers=headers, data={
"name": name,
"save": "",
"_ckan_phase": 1
Expand All @@ -153,9 +153,9 @@ def test_resource_required(self, app, user):
response = app.post(location, extra_environ=env, data={
"id": "",
"url": "",
"save": "go-metadata",
"save": "again",
})
assert "You must add at least one data resource" in response
assert "No resource data entered" in response

def test_complete_package_with_one_resource(self, app, user):
url = url_for("dataset.new")
Expand Down
52 changes: 26 additions & 26 deletions ckan/views/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import cgi
import json
import logging
from typing import Any, cast, Optional, Union
from typing import Any, Optional, Union, cast

from werkzeug.wrappers.response import Response as WerkzeugResponse
import flask
Expand All @@ -25,7 +25,7 @@
_get_pkg_template, _get_package_type, _setup_template_variables
)

from ckan.types import Context, Response
from ckan.types import Context, Response, ErrorDict

Blueprint = flask.Blueprint
NotFound = logic.NotFound
Expand Down Expand Up @@ -213,35 +213,31 @@ def post(self, package_type: str, id: str) -> Union[str, Response]:
data_provided = True
break

if not data_provided and save_action != u"go-dataset-complete":
if save_action == u'go-dataset':
if not data_provided and save_action != "go-dataset-complete":
if save_action == 'again':
errors: dict[str, Any] = {}
error_summary = {_('Error'): _('No resource data entered')}
return self.get(package_type, id, data, errors, error_summary)
if save_action == 'go-dataset':
# go to final stage of adddataset
return h.redirect_to(u'{}.edit'.format(package_type), id=id)
# see if we have added any resources
try:
data_dict = get_action(u'package_show')(context, {u'id': id})
get_action('package_patch')(
logic.fresh_context(context),
{'id': id, 'state': 'active'}
)
except NotAuthorized:
return base.abort(403, _(u'Unauthorized to update dataset'))
except NotFound:
return base.abort(
404,
_(u'The dataset {id} could not be found.').format(id=id)
)
if not len(data_dict[u'resources']):
# no data so keep on page
msg = _(u'You must add at least one data resource')
# On new templates do not use flash message

errors: dict[str, Any] = {}
error_summary = {_(u'Error'): msg}
except ValidationError as e:
errors = cast(
"list[ErrorDict]", e.error_dict.get('resources', [{}]))[-1]
error_summary = e.error_summary
return self.get(package_type, id, data, errors, error_summary)

# XXX race condition if another user edits/deletes
data_dict = get_action(u'package_show')(context, {u'id': id})
get_action(u'package_update')(
cast(Context, dict(context, allow_state_change=True)),
dict(data_dict, state=u'active')
)
return h.redirect_to(u'{}.read'.format(package_type), id=id)

data[u'package_id'] = id
Expand All @@ -266,12 +262,16 @@ def post(self, package_type: str, id: str) -> Union[str, Response]:
404, _(u'The dataset {id} could not be found.').format(id=id)
)
if save_action == u'go-metadata':
# XXX race condition if another user edits/deletes
data_dict = get_action(u'package_show')(context, {u'id': id})
get_action(u'package_update')(
cast(Context, dict(context, allow_state_change=True)),
dict(data_dict, state=u'active')
)
try:
get_action('package_patch')(
logic.fresh_context(context),
{'id': id, 'state': 'active'}
)
except ValidationError as e:
errors = cast(
"list[ErrorDict]", e.error_dict.get('resources', [{}]))[-1]
error_summary = e.error_summary
return self.get(package_type, id, data, errors, error_summary)
return h.redirect_to(u'{}.read'.format(package_type), id=id)
elif save_action == u'go-dataset':
# go to first stage of add dataset
Expand Down
Loading