views.py 10.2 KB
Newer Older
1 2 3 4
#!/usr/bin/env python3
"""
    Routes
"""
5
import copy
6 7 8 9 10
import json
import os
from collections import OrderedDict
from pathlib import Path

11
from validata_validate import csv_helpers
12
from validata_validate.loaders import custom_loaders
13 14 15 16
from validata_ui import app
from validata_ui.util import flash_error, flash_info, flash_success, flash_warning, ValidataSource
from validata_ui.validate_helper import ValidatorHelper
from validata_ui import error_messages
17

18
from flask import Flask, jsonify, redirect, render_template, request, url_for
19 20
import tabulator

21
from io import BytesIO
22

23 24

def extract_source_data(source: ValidataSource, preview_rows_nb=5):
25
    """ Computes table preview """
26 27 28

    def stringify(val):
        """ Transform value into string """
Pierre Dittgen's avatar
Pierre Dittgen committed
29
        return '' if val is None else str(val)
30

31 32
    header = None
    rows = []
Pierre Dittgen's avatar
Pierre Dittgen committed
33
    nb_rows = 0
34 35 36

    delimiter = None
    if source.format == "csv":
37 38 39 40
        delimiter = csv_helpers.detect_dialect(source.data, format=source.format, scheme=source.scheme,
                                               custom_loaders=custom_loaders).delimiter
    with tabulator.Stream(source.data, format=source.format, scheme=source.scheme, custom_loaders=custom_loaders,
                          delimiter=delimiter) as stream:
41 42 43 44
        for row in stream:
            if header is None:
                header = row
            else:
45
                rows.append(list(map(stringify, row)))
Pierre Dittgen's avatar
Pierre Dittgen committed
46
                nb_rows += 1
47
    preview_rows_nb = min(preview_rows_nb, nb_rows)
48 49
    return {'header': header,
            'rows_nb': nb_rows,
50 51 52
            'data_rows': rows,
            'preview_rows_nb': preview_rows_nb,
            'preview_rows': rows[:preview_rows_nb]}
53 54


55
ERROR_MESSAGE_FUNC = {
Pierre Dittgen's avatar
Pierre Dittgen committed
56

57
    # Core checks
Pierre Dittgen's avatar
Pierre Dittgen committed
58
    # blank-header
59
    'blank-row': error_messages.blank_row,
Pierre Dittgen's avatar
Pierre Dittgen committed
60
    # duplicate-header
61 62 63
    'duplicate-row': error_messages.duplicate_row,
    'enumerable-constraint': error_messages.enumerable_constraint,
    'maximum-constraint': error_messages.maximum_constraint,
Pierre Dittgen's avatar
Pierre Dittgen committed
64 65 66 67 68
    'maximum-length-constraint': error_messages.maximum_length_constraint,
    'minimum-constraint': error_messages.minimum_constraint,
    'minimum-length-constraint': error_messages.minimum_length_constraint,
    # missing-value
    'pattern-constraint': error_messages.pattern_constraint,
69 70
    'required-constraint': error_messages.required_constraint,
    'type-or-format-error': error_messages.type_or_format_error,
Pierre Dittgen's avatar
Pierre Dittgen committed
71
    'unique-constraint': error_messages.unique_constraint,
72 73 74 75 76

    # Validata pre-checks
    'extra-headers': error_messages.extra_headers,
    'invalid-column-delimiter': error_messages.invalid_column_delimiter,
    'missing-headers': error_messages.missing_headers,
Pierre Dittgen's avatar
Pierre Dittgen committed
77
    'wrong-headers-order': error_messages.wrong_headers_order,
78 79 80 81 82 83 84

    # Validata custom checks
    'french-siret-value': error_messages.french_siret_value,
}


def improve_messages(errors, headers, schema):
85 86
    """ Translates and improve error messages """

87 88
    def error_message_default_func(error, headers, schema):
        """ Sets a new better error message """
Pierre Dittgen's avatar
Pierre Dittgen committed
89 90
        error['title'] = error['code']
        error['content'] = error.get('message', 'pas d\'information complémentaire')
91
        return error
92

93
    improved_errors = []
94 95 96

    for error in errors:

97 98
        improve_func = ERROR_MESSAGE_FUNC.get(error['code'], error_message_default_func)
        improved_errors.append(improve_func(error, headers, schema))
99

100
    return improved_errors
101 102


103
def contextualize(errors):
Pierre Dittgen's avatar
Pierre Dittgen committed
104 105
    """ add context to errors """

106 107
    def add_context(err):
        """ Adds context info based on row-nb presence """
108
        context = 'body' if 'row-number' in err and not err['row-number'] is None else 'table'
109 110 111
        return {**err, 'context': context}

    return list(map(add_context, errors))
112 113


114
def create_validata_report(goodtables_report, schema):
115 116 117 118 119 120
    """ Creates an error report easier to handle and display in templates:
        - only one table
        - errors are contextualized
        - error-counts is ok
        - errors are grouped by lines
        - errors are separated into "structure" and "body"
121
        - error messages are improved
122 123 124 125 126 127 128 129 130 131 132
    """
    report = copy.deepcopy(goodtables_report)

    # One table is enough
    del report['table-count']
    report['table'] = report['tables'][0]
    del report['tables']
    del report['table']['error-count']
    del report['table']['time']
    del report['table']['valid']
    del report['valid']
133 134 135
    # use _ instead of - to ease information picking in jinja2 template
    report['table']['row_count'] = report['table']['row-count']

Pierre Dittgen's avatar
Pierre Dittgen committed
136 137 138 139
    # Handy col_count info
    headers = report['table'].get('headers', [])
    report['table']['col_count'] = len(headers)

Pierre Dittgen's avatar
Pierre Dittgen committed
140
    # Computes column info
141
    schema_fields = schema.get('fields', [])
Pierre Dittgen's avatar
Pierre Dittgen committed
142 143 144 145
    fields_dict = {f['name']: (f.get('title', 'titre non défini'), f.get('description', '')) for f in schema_fields}
    report['table']['headers_title'] = [fields_dict[h][0] if h in fields_dict else 'colonne inconnue' for h in headers]
    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]
146 147 148 149 150

    # Add context to errors
    errors = contextualize(report['table']['errors'])
    del report['table']['errors']

151
    # Provide better (french) messages
152
    errors = improve_messages(errors, headers, schema)
153

154 155 156 157 158 159 160 161 162 163 164 165
    # Count errors
    report['error_count'] = len(errors)
    del report['error-count']

    # Then group them in 2 groups : structure and body
    report['table']['errors'] = {'structure': [], 'body': []}
    for err in errors:
        if err['context'] != 'body':
            report['table']['errors']['structure'].append(err)
        else:
            report['table']['errors']['body'].append(err)

Pierre Dittgen's avatar
Pierre Dittgen committed
166
    # Checks if there are structure errors different to invalid-column-delimiter
167 168 169
    report['table']['display_body_errors'] = all(err['code'] == 'invalid-column-delimiter'
                                                 for err in report['table']['errors']['structure'])

Pierre Dittgen's avatar
Pierre Dittgen committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    # Group body errors by row id
    rows = []
    current_row_id = 0
    for err in report['table']['errors']['body']:
        if not 'row-number' in err:
            print('ERR', err)
        row_id = err['row-number']
        del err['row-number']
        del err['context']
        if row_id != current_row_id:
            current_row_id = row_id
            rows.append({'row_id': current_row_id, 'errors': {}})

        column_id = err.get('column-number')
        if column_id is not None:
            del err['column-number']
            rows[-1]['errors'][column_id] = err
        else:
            rows[-1]['errors']['row'] = err
    report['table']['errors']['by_rows'] = rows
Pierre Dittgen's avatar
Pierre Dittgen committed
190 191 192 193

    return report


194
def validate(schema_code, source: ValidataSource):
195 196
    """ Validate source and display report """

197 198 199 200 201
    try:
        goodtables_report = ValidatorHelper.validate(schema_code, **source.get_goodtables_source())
    except tabulator.exceptions.FormatError:
        flash_error('Erreur : format de fichier non supporté')
        return redirect(url_for('scdl_validator', val_code=schema_code))
202 203

    source_data = extract_source_data(source)
204

205
    validata_report = create_validata_report(goodtables_report, ValidatorHelper.schema(schema_code))
206

207
    # return jsonify(validata_report)
Pierre Dittgen's avatar
Pierre Dittgen committed
208

Pierre Dittgen's avatar
Pierre Dittgen committed
209 210
    # Complete report
    val_info = ValidatorHelper.schema_info(schema_code)
211
    return render_template('validation_report.html', title='Rapport de validation',
212
                           val_info=ValidatorHelper.schema_info(schema_code), report=validata_report,
213
                           source=source, source_type=source.type, source_data=source_data,
214
                           report_str=json.dumps(validata_report, sort_keys=True, indent=2),
Pierre Dittgen's avatar
Pierre Dittgen committed
215
                           breadcrumbs=[{'url': url_for('home'), 'title': 'Accueil'},
Pierre Dittgen's avatar
Pierre Dittgen committed
216 217
                                        {'url': url_for('scdl_validator', val_code=schema_code),
                                         'title': val_info['title']}])
218 219


220 221 222 223 224 225 226 227
def bytes_data(f):
    """ Gets bytes data from Werkzeug FileStorage instance """
    iob = BytesIO()
    f.save(iob)
    iob.seek(0)
    return iob.getvalue()


228 229 230 231 232 233
# Routes


@app.route('/')
def home():
    """ Home page """
Pierre Dittgen's avatar
Pierre Dittgen committed
234
    validators = ValidatorHelper.schema_info_list()
Pierre Dittgen's avatar
Pierre Dittgen committed
235
    flash_warning('Ce service est fourni en mode beta - certains problèmes peuvent subsister - nous mettons tout en œuvre pour améliorer son fonctionnement en continu')
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    return render_template('home.html', title='Accueil', validators=validators)


@app.route('/validators')
def validators():
    """ No validators page """
    return redirect(url_for('home'))


@app.route('/validators/<val_code>', methods=['GET', 'POST'])
def scdl_validator(val_code):
    """ Validator page """

    if not ValidatorHelper.schema_exist(val_code):
        flash_error('Validateur [{}] inconnu'.format(val_code))
        return redirect(url_for('home'))

    if request.method == 'GET':

Pierre Dittgen's avatar
Pierre Dittgen committed
255
        val_info = ValidatorHelper.schema_info(val_code)
256 257 258 259
        input_param = request.args.get('input')

        # First form display
        if input_param is None or input_param not in ('url', 'example'):
260
            return render_template('validation_form.html', title=val_info['title'],
261 262 263 264 265 266 267 268 269
                                   val_info=val_info,
                                   breadcrumbs=[{'url': url_for('home'), 'title': 'Accueil'}, ])

        # Process URL
        else:
            url = request.args.get('url')
            if url is None or url == '':
                flash_error("Vous n'avez pas indiqué d'url à valider")
                return redirect(url_for('scdl_validator', val_code=val_code))
270
            return validate(val_code, ValidataSource(url, url, 'url'))
271 272 273 274

    else:  # POST
        input_param = request.form.get('input')
        if input_param is None:
275
            flash_error('Aucun fichier à valider')
276 277 278 279 280 281 282 283
            return redirect(url_for('scdl_validator', val_code=val_code))

        # File validation
        if input_param == 'file':
            f = request.files.get('file')
            if f is None:
                flash_warning("Vous n'avez pas indiqué de fichier à valider")
                return redirect(url_for('scdl_validator', val_code=val_code))
284 285

            return validate(val_code, ValidataSource(bytes_data(f), f.filename, 'file'))
286 287

        return 'Bizarre, vous avez dit bizarre ?'