views.py 9.15 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_ui_next import app
13
from validata_ui_next.util import flash_error, flash_info, flash_success, flash_warning, ValidataSource
14
from validata_ui_next.validate_helper import ValidatorHelper
15

Pierre Dittgen's avatar
Pierre Dittgen committed
16
from flask import Flask, jsonify, redirect, render_template, request, url_for
17 18
import tabulator

19
from io import BytesIO
20

21 22

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

    def stringify(val):
        """ Transform value into string """
        if val is None:
            return ''
        return str(val)

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

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


Pierre Dittgen's avatar
Pierre Dittgen committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
ERR_CODE_TO_CONTEXT = dict([
    # TODO: gets it from spec.json
    ('duplicate-header', 'head'),
    ('extra-value', 'body'),
    ('missing-value', 'body'),
    ('source-error', 'table'),
    ('schema-error', 'table'),
    ('non-matching-header', 'head'),
    ('blank-row', 'body'),
    ('blank-header', 'head'),
    ('enumerable-constraint', 'body'),
    ('http-error', 'table'),
    ('scheme-error', 'table'),
    ('type-or-format-error', 'body'),
    ('format-error', 'table'),
    ('extra-header', 'head'),
    ('pattern-constraint', 'body'),
    ('required-constraint', 'body'),
    ('missing-header', 'head'),
    ('maximum-length-constraint', 'body'),
    ('maximum-constraint', 'body'),
    ('minimum-length-constraint', 'body'),
    ('encoding-error', 'table'),
    ('io-error', 'table'),
    ('unique-constraint', 'body'),
    ('duplicate-row', 'body'),
    ('minimum-constraint', 'body'),

    # TODO: get it from validata_validate
81 82
    ('invalid-column-delimiter', 'table'),
    ('missing-headers', 'table'),
Pierre Dittgen's avatar
Pierre Dittgen committed
83 84 85 86 87

    # Custom checks fall in default case: body
])


88 89 90 91 92 93 94 95 96 97 98 99 100 101
def improve_messages(errors):
    """ Translates and improve error messages """

    def update_message(error, new_message):
        """ set a new error message """

        # Stores previous message if exists
        if 'message' in error:
            error['_original_message'] = error['message']
        error['message'] = new_message

    for error in errors:

        if error['code'] == 'blank-row':
Pierre Dittgen's avatar
Pierre Dittgen committed
102 103
            update_message(error, 'la ligne est vide')

104
        elif error['code'] == 'duplicate-row':
Pierre Dittgen's avatar
Pierre Dittgen committed
105 106 107 108 109 110 111 112 113
            msg_prefix = 'la ligne est identique '
            row_numbers = error['message-data']['row_numbers']
            if len(row_numbers) == 1:
                msg = msg_prefix + "à la ligne {}".format(row_numbers)
            else:
                idx = row_numbers.rfind(',')
                param_str = row_numbers[:idx] + ' et' + row_numbers[idx+1:]
                msg = msg_prefix + "aux lignes {}".format(param_str)
            update_message(error, msg)
114 115 116 117 118

    # Return updated error list
    return errors


119
def contextualize(errors):
Pierre Dittgen's avatar
Pierre Dittgen committed
120 121
    """ add context to errors """

122 123 124 125 126 127 128 129 130 131
    return [{**err, 'context': ERR_CODE_TO_CONTEXT.get(err['code'], 'body')} for err in errors]


def create_validata_report(goodtables_report):
    """ 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"
132
        - error messages are improved
133 134 135 136 137 138 139 140 141 142 143
    """
    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']
144 145 146 147
    # use _ instead of - to ease information picking in jinja2 template
    report['table']['row_count'] = report['table']['row-count']

    # Shortcut
148
    report['table']['col_count'] = len(report['table'].get('headers', []))
149 150 151 152 153

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

154 155 156
    # Provide better (french) messages
    errors = improve_messages(errors)

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    # 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)

    # and group body errors by row id
    rows = []
    current_row_id = 0
    for err in report['table']['errors']['body']:
        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': {}})
179 180 181 182 183 184 185

        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
186
    report['table']['errors']['by_rows'] = rows
Pierre Dittgen's avatar
Pierre Dittgen committed
187 188 189 190

    return report


191
def validate(schema_code, source: ValidataSource):
192 193
    """ Validate source and display report """

194 195 196
    goodtables_report = ValidatorHelper.validate(schema_code, **source.get_goodtables_source())

    source_data = extract_source_data(source)
197 198 199

    validata_report = create_validata_report(goodtables_report)

200
    # return jsonify(validata_report)
Pierre Dittgen's avatar
Pierre Dittgen committed
201

Pierre Dittgen's avatar
Pierre Dittgen committed
202 203
    # Complete report
    val_info = ValidatorHelper.schema_info(schema_code)
204
    return render_template('validation_report.html', title='Rapport de validation',
205
                           val_info=ValidatorHelper.schema_info(schema_code), report=validata_report,
206
                           source=source, source_type=source.type, source_data=source_data,
207
                           report_str=json.dumps(validata_report, sort_keys=True, indent=2),
Pierre Dittgen's avatar
Pierre Dittgen committed
208 209
                           breadcrumbs=[{'url': url_for('home'), 'title': 'Accueil'},
                                        {'url': url_for('scdl_validator', val_code=schema_code), 'title': val_info['title']}])
210 211


212 213 214 215 216 217 218 219
def bytes_data(f):
    """ Gets bytes data from Werkzeug FileStorage instance """
    iob = BytesIO()
    f.save(iob)
    iob.seek(0)
    return iob.getvalue()


220 221 222 223 224 225
# Routes


@app.route('/')
def home():
    """ Home page """
Pierre Dittgen's avatar
Pierre Dittgen committed
226
    validators = ValidatorHelper.schema_info_list()
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
    return render_template('home.html', title='Accueil', validators=validators)


@app.route('/about')
def about():
    """ Help -> About page """
    return render_template('about.html', title='À propos',
                           breadcrumbs=[{'url': url_for('home'), 'title': 'Accueil'}, ])


@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
253
        val_info = ValidatorHelper.schema_info(val_code)
254 255 256 257 258 259 260 261 262 263 264 265 266 267
        input_param = request.args.get('input')

        # First form display
        if input_param is None or input_param not in ('url', 'example'):
            return render_template('validator.html', title=val_info['title'],
                                   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))
268
            return validate(val_code, ValidataSource(url, url, 'url'))
269 270 271 272 273 274 275 276 277 278 279 280 281

    else:  # POST
        input_param = request.form.get('input')
        if input_param is None:
            flash_error('Source non définie')
            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))
282 283

            return validate(val_code, ValidataSource(bytes_data(f), f.filename, 'file'))
284 285

        return 'Bizarre, vous avez dit bizarre ?'