Skip to content

Commit

Permalink
[#2664] first cut of tag errors needs input
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Jul 13, 2012
1 parent 6a8e74d commit f9bc5ef
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
35 changes: 35 additions & 0 deletions ckan/lib/helpers.py
Expand Up @@ -1208,6 +1208,39 @@ def get_pkg_dict_extra(pkg_dict, key, default=None):

return default

def format_error_dict(errors):
formatted_errors = []

for error in errors:
if isinstance(error, dict):
if 'name' in error:
formatted_errors.append(', '.join(error['name']))
return formatted_errors

def format_errors(errors):
formatted_errors = {}

def unpack(packed_errors):
unpacked_errors = []
if isinstance(packed_errors, dict):
if 'name' in packed_errors:
unpacked_errors=packed_errors['name']
else:
unpacked_errors.append(packed_errors)
elif isinstance(packed_errors, list):
if len(packed_errors) and isinstance(packed_errors[0], dict):
unpacked_errors = [', '.join(items['name']) for items in packed_errors]
else:
unpacked_errors = packed_errors
else:
unpacked_errors.append(packed_errors)

return ', '.join(unpacked_errors)

for error in errors:
formatted_errors[error] = unpack(errors[error])

return formatted_errors

# these are the functions that will end up in `h` template helpers
# if config option restrict_template_vars is true
Expand Down Expand Up @@ -1281,6 +1314,8 @@ def get_pkg_dict_extra(pkg_dict, key, default=None):
'dashboard_activity_stream',
'escape_js',
'get_pkg_dict_extra',
'format_error_dict',
'format_errors',
# imported into ckan.lib.helpers
'literal',
'link_to',
Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/macros/form.html
Expand Up @@ -237,7 +237,7 @@
<div class="error-explanation alert alert-{{ type }}{{ " " ~ classes | join(' ') }}">
<p>{{ _('The form contains invalid entries:') }}</p>
<ul>
{% for key, error in errors.items() %}
{% for key, error in h.format_errors(errors).items() %}
<li data-field-label="{{ key }}">{{ key }}: {{ error }}</li>
{% endfor %}
</ul>
Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/package/snippets/package_basic_fields.html
Expand Up @@ -11,7 +11,7 @@

{{ form.markdown('notes', id='field-notes', label=_('Description'), placeholder=_('eg. Some useful notes about the data'), value=data.notes, error=errors.notes) }}

{{ form.input('tag_string', id='field-tags', label=_('Tags'), placeholder=_('eg. economy, mental health, government'), value=data.tag_string, error=errors.tags, classes=['control-full']) }}
{{ form.input('tag_string', id='field-tags', label=_('Tags'), placeholder=_('eg. economy, mental health, government'), value=data.tag_string, error=h.format_error_dict(errors.tags), classes=['control-full']) }}

<div class="control-group">
{% set error = errors.license_id %}
Expand Down
4 changes: 2 additions & 2 deletions ckan/templates/package/snippets/package_form.html
@@ -1,4 +1,4 @@
{% import 'macros/form.html' as form %}
{% import 'macros/form.html' as form with context %}
{% set action = c.form_action or '' %}

{# This provides a full page that renders a form for adding a dataset. It can
Expand All @@ -11,7 +11,7 @@
<input type="hidden" name="_ckan_phase" value="dataset_new_1" />
{# pkg_name used in 3 stage edit #}
<input type="hidden" name="pkg_name" value="{{ data.name }}" />
{% block errors %}{{ form.errors(error_summary) }}{% endblock %}
{% block errors %}{{ form.errors(errors) }}{% endblock %}

{% block basic_fields %}
{% snippet 'package/snippets/package_basic_fields.html', data=data, errors=errors, licences=c.licences, groups_available=c.groups_available %}
Expand Down

0 comments on commit f9bc5ef

Please sign in to comment.