From 7f27166189c3f2d84e0dc0fc5eeb9fe787ea68e5 Mon Sep 17 00:00:00 2001 From: James Baster Date: Tue, 23 Apr 2019 11:08:36 +0100 Subject: [PATCH] OCDS - Group validation errors by type https://github.com/OpenDataServices/cove/issues/1159 --- cove_ocds/lib/views.py | 15 ++++ .../templates/cove_ocds/explore_base.html | 89 +++++++++++++++---- .../templates/cove_ocds/validation_table.html | 87 ++++++++++++++++++ cove_ocds/views.py | 4 +- 4 files changed, 176 insertions(+), 19 deletions(-) create mode 100644 cove_ocds/lib/views.py create mode 100644 cove_ocds/templates/cove_ocds/validation_table.html diff --git a/cove_ocds/lib/views.py b/cove_ocds/lib/views.py new file mode 100644 index 000000000..30f0b6d59 --- /dev/null +++ b/cove_ocds/lib/views.py @@ -0,0 +1,15 @@ +from collections import defaultdict +import json + + +def group_validation_errors(validation_errors): + validation_errors_grouped = defaultdict(list) + for error_json, values in validation_errors: + error = json.loads(error_json) + if error['message_type'] == 'required': + validation_errors_grouped['required'].append((error_json, values)) + elif error['message_type'] in ['format']: + validation_errors_grouped['format'].append((error_json, values)) + else: + validation_errors_grouped['other'].append((error_json, values)) + return validation_errors_grouped diff --git a/cove_ocds/templates/cove_ocds/explore_base.html b/cove_ocds/templates/cove_ocds/explore_base.html index fd151e04d..73395f15c 100644 --- a/cove_ocds/templates/cove_ocds/explore_base.html +++ b/cove_ocds/templates/cove_ocds/explore_base.html @@ -241,26 +241,79 @@

{% endif %} -{% if validation_errors %} - {% for error_json, values in validation_errors %} - {% with error=error_json|json_decode %} - {% cove_modal_errors className="validation-errors-"|concat:forloop.counter modalTitle=error.message errorList=values file_type=file_type full_table=True %} - {% endwith %} - {% endfor %} - - - -
-
-

- {% trans 'Structural Errors' %} -

+{% with validation_errors=validation_errors_grouped.required error_prefix='required-' %} + {% if validation_errors %} + {% for error_json, values in validation_errors %} + {% with error=error_json|json_decode %} + {% cove_modal_errors className="validation-errors-"|concat:error_prefix|concat:forloop.counter modalTitle=error.message errorList=values file_type=file_type full_table=True %} + {% endwith %} + {% endfor %} + + +
+
+

+ {% trans 'Structural Errors - Required Fields' %} +

+
+
+ {% blocktrans %}Some or all of your data is missing fields which are required by the OCDS schema.{% endblocktrans %} + {% include "cove_ocds/validation_table.html" %} +
-
- {% include "validation_table.html" %} + + {% endif %} +{% endwith %} + +{% with validation_errors=validation_errors_grouped.format error_prefix='format-' %} + {% if validation_errors %} + {% for error_json, values in validation_errors %} + {% with error=error_json|json_decode %} + {% cove_modal_errors className="validation-errors-"|concat:error_prefix|concat:forloop.counter modalTitle=error.message errorList=values file_type=file_type full_table=True %} + {% endwith %} + {% endfor %} + + +
+
+

+ {% trans 'Structural Errors - Format' %} +

+
+
+ {% blocktrans %}Some or all of your data includes fields which are incorrectly formatted.{% endblocktrans %} + {% include "cove_ocds/validation_table.html" %} +
-
-{% endif %} + + {% endif %} +{% endwith %} + +{% with validation_errors=validation_errors_grouped.other error_prefix='other-' %} + {% if validation_errors %} + {% for error_json, values in validation_errors %} + {% with error=error_json|json_decode %} + {% cove_modal_errors className="validation-errors-"|concat:error_prefix|concat:forloop.counter modalTitle=error.message errorList=values file_type=file_type full_table=True %} + {% endwith %} + {% endfor %} + + +
+
+

+ {% trans 'Structural Errors - Other' %} +

+
+
+ {% blocktrans %}Some or all of your data has validation errors.{% endblocktrans %} + {% include "cove_ocds/validation_table.html" %} +
+
+ + {% endif %} +{% endwith %} + + {% if structure_warnings %} diff --git a/cove_ocds/templates/cove_ocds/validation_table.html b/cove_ocds/templates/cove_ocds/validation_table.html new file mode 100644 index 000000000..2127110df --- /dev/null +++ b/cove_ocds/templates/cove_ocds/validation_table.html @@ -0,0 +1,87 @@ +{% load i18n %} +{% load cove_tags %} + + + + + + + + + {% if file_type == 'xlsx' or file_type == 'csv' %} + + {% endif %} + + + +{% for error_json, values in validation_errors %} +{% with error=error_json|json_decode %} + + {% if error.message_safe %} + + {% elif error.message_type in common_error_types %} + + {% else %} + + {% endif %} + + + + {% if file_type == 'xlsx' or file_type == 'csv' %} + + {% endif %} + +{% endwith %} +{% endfor %} + +
{% trans 'Error Description' %}{% trans 'Error Count' %}{% trans 'First 3 Examples' %}{% trans 'Location of first 3 errors' %}{% trans 'Spreadsheet Location of first 3 errors' %}
+

+ {{ error.message_safe | safe }} + {% if error.message_type in common_error_types %} + (more info) + {% endif %} +

+ {% if 'schema_title' in error %} +
+

{{ error.schema_title }}

+

{{ error.schema_description_safe | safe }}

+
+ {% endif %} +
{{error.message}} {{error.message}} + {% if values|length > 3 %} + {% if error_prefix %} + + {% else %} + + {% endif %} + {{values|length}} + + {% else %} + {{values|length}} + {% endif %} + +
    + {% for value in values|slice:":3" %} +
  • {{value.value}}
  • + {% endfor %} +
+
+
    + {% for value in values|slice:":3" %} +
  • + {% if value.line %} + {% trans 'Path:' %} {{value.path}} + {% trans 'Line:' %} {{value.line}} + {% else %} + {{ value.path }} + {% endif %} +
  • + {% endfor %} +
+
+
    + {% for value in values|slice:":3" %} +
  • Sheet: {{value.sheet}} Row: {{value.row_number}} {% if value.header %} Column: {{value.header}} {% endif %}
  • + {% endfor %} +
+
diff --git a/cove_ocds/views.py b/cove_ocds/views.py index 710bac490..720d12fdf 100644 --- a/cove_ocds/views.py +++ b/cove_ocds/views.py @@ -22,6 +22,7 @@ from libcove.lib.exceptions import CoveInputDataError from . lib.ocds_show_extra import add_extra_fields from cove.views import explore_data_context +from cove_ocds.lib.views import group_validation_errors logger = logging.getLogger(__name__) @@ -164,7 +165,8 @@ def explore_ocds(request, pk): context.update({ 'data_schema_version': db_data.data_schema_version, - 'first_render': not db_data.rendered + 'first_render': not db_data.rendered, + 'validation_errors_grouped': group_validation_errors(context['validation_errors']), }) schema_version = getattr(schema_ocds, 'version', None)