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

Message adjustments

parent 7a46f296
......@@ -4,6 +4,6 @@
Customize h2 display in popovers
*/
div.popover h2 {
font-size: 1.4em;
font-size: 1.0em;
font-weight: bold;
}
\ No newline at end of file
......@@ -10,10 +10,11 @@ from datetime import datetime
from io import BytesIO
from pathlib import Path
from commonmark import commonmark
from flask import Flask, jsonify, redirect, render_template, request, url_for
import tabulator
from validata_ui import app, error_messages
from validata_ui import app
from validata_ui.util import (ValidataSource, flash_error, flash_info,
flash_success, flash_warning)
from validata_ui.validate_helper import ValidatorHelper
......@@ -52,34 +53,39 @@ def extract_source_data(source: ValidataSource, preview_rows_nb=5):
'preview_rows': rows[:preview_rows_nb]}
def improve_messages(errors, headers, schema):
""" Translates and improve error messages """
def improve_errors(errors):
"""Add context to errors, converts markdown content to HTML"""
def error_message_default_func(error, headers, schema):
""" Sets a new better error message """
error['title'] = error['code']
error['content'] = error.get('message', 'pas d\'information complémentaire')
return error
def improve_err(err):
"""Adds context info based on row-nb presence and converts content to HTML"""
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)
improved_errors.append(improve_func(error, headers, schema))
# Set default title if no title
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):
""" add context to errors """
# Message content
md_content = '*content soon available*' if not 'content' in err else err['content']
update_keys['content'] = commonmark(md_content)
def add_context(err):
""" 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 {**err, **update_keys}
return list(map(add_context, errors))
return list(map(improve_err, errors))
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]
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
errors = improve_messages(errors, headers, schema)
errors = improve_errors(report['table']['errors'])
del report['table']['errors']
# Count errors
report['error_count'] = len(errors)
......@@ -157,7 +160,7 @@ def create_validata_report(goodtables_report, schema):
rows[-1]['errors'][column_id] = err
else:
rows[-1]['errors']['row'] = err
report['table']['errors']['by_rows'] = rows
report['table']['errors']['body_by_rows'] = rows
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