Commit 33d578b4 authored by Pierre Dittgen's avatar Pierre Dittgen

Message adjustments

parent 7a46f296
...@@ -4,6 +4,6 @@ ...@@ -4,6 +4,6 @@
Customize h2 display in popovers Customize h2 display in popovers
*/ */
div.popover h2 { div.popover h2 {
font-size: 1.4em; font-size: 1.0em;
font-weight: bold; font-weight: bold;
} }
\ No newline at end of file
...@@ -10,10 +10,11 @@ from datetime import datetime ...@@ -10,10 +10,11 @@ from datetime import datetime
from io import BytesIO from io import BytesIO
from pathlib import Path from pathlib import Path
from commonmark import commonmark
from flask import Flask, jsonify, redirect, render_template, request, url_for from flask import Flask, jsonify, redirect, render_template, request, url_for
import tabulator import tabulator
from validata_ui import app, error_messages from validata_ui import app
from validata_ui.util import (ValidataSource, flash_error, flash_info, from validata_ui.util import (ValidataSource, flash_error, flash_info,
flash_success, flash_warning) flash_success, flash_warning)
from validata_ui.validate_helper import ValidatorHelper from validata_ui.validate_helper import ValidatorHelper
...@@ -52,34 +53,39 @@ def extract_source_data(source: ValidataSource, preview_rows_nb=5): ...@@ -52,34 +53,39 @@ def extract_source_data(source: ValidataSource, preview_rows_nb=5):
'preview_rows': rows[:preview_rows_nb]} 'preview_rows': rows[:preview_rows_nb]}
def improve_messages(errors, headers, schema): def improve_errors(errors):
""" Translates and improve error messages """ """Add context to errors, converts markdown content to HTML"""
def error_message_default_func(error, headers, schema): def improve_err(err):
""" Sets a new better error message """ """Adds context info based on row-nb presence and converts content to HTML"""
error['title'] = error['code']
error['content'] = error.get('message', 'pas d\'information complémentaire')
return error
improved_errors = [] # Context
update_keys = {
'context': 'body' if 'row-number' in err and not err['row-number'] is None else 'table',
}
for error in errors: # markdown to HTML (with default values for 'title' and 'content')
improve_func = ERROR_MESSAGE_FUNC.get(error['code'], error_message_default_func) # Set default title if no title
improved_errors.append(improve_func(error, headers, schema)) if not 'title' in err:
update_keys['title'] = '[{}]'.format(err['code'])
return improved_errors # Convert message to markdown only if no content
# => for pre-checks errors
if 'message' in err and not 'content' in err:
update_keys['message'] = commonmark(err['message'])
# Else, default message
elif not 'message' in err or err['message'] is None:
update_keys['message'] = '[{}]'.format(err['code'])
def contextualize(errors): # Message content
""" add context to errors """ md_content = '*content soon available*' if not 'content' in err else err['content']
update_keys['content'] = commonmark(md_content)
def add_context(err): return {**err, **update_keys}
""" Adds context info based on row-nb presence """
context = 'body' if 'row-number' in err and not err['row-number'] is None else 'table'
return {**err, 'context': context}
return list(map(add_context, errors)) return list(map(improve_err, errors))
def create_validata_report(goodtables_report, schema): def create_validata_report(goodtables_report, schema):
...@@ -115,12 +121,9 @@ def create_validata_report(goodtables_report, schema): ...@@ -115,12 +121,9 @@ def create_validata_report(goodtables_report, schema):
report['table']['headers_description'] = [fields_dict[h][1] report['table']['headers_description'] = [fields_dict[h][1]
if h in fields_dict else 'Cette colonne n\'est pas définie dans le schema' for h in headers] if h in fields_dict else 'Cette colonne n\'est pas définie dans le schema' for h in headers]
# Add context to errors
errors = contextualize(report['table']['errors'])
del report['table']['errors']
# Provide better (french) messages # Provide better (french) messages
errors = improve_messages(errors, headers, schema) errors = improve_errors(report['table']['errors'])
del report['table']['errors']
# Count errors # Count errors
report['error_count'] = len(errors) report['error_count'] = len(errors)
...@@ -157,7 +160,7 @@ def create_validata_report(goodtables_report, schema): ...@@ -157,7 +160,7 @@ def create_validata_report(goodtables_report, schema):
rows[-1]['errors'][column_id] = err rows[-1]['errors'][column_id] = err
else: else:
rows[-1]['errors']['row'] = err rows[-1]['errors']['row'] = err
report['table']['errors']['by_rows'] = rows report['table']['errors']['body_by_rows'] = rows
return report return report
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment