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

12
from commonmark import commonmark
Pierre Dittgen's avatar
Pierre Dittgen committed
13
from flask import Flask, jsonify, redirect, render_template, request, url_for
14

15
16
17
18
19
20
21
import tabulator
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
from validata_validate import csv_helpers
from validata_validate.loaders import custom_loaders
22
23
24
25
26
27
28
29
30
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

from flask import Flask, jsonify, redirect, render_template, request, url_for
import tabulator

from io import BytesIO
31

32
33

def extract_source_data(source: ValidataSource, preview_rows_nb=5):
34
    """ Computes table preview """
35
36
37

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

40
41
    header = None
    rows = []
Pierre Dittgen's avatar
Pierre Dittgen committed
42
    nb_rows = 0
43

44
    options = {}
45
    if source.format == "csv":
46
47
        options['delimiter'] = csv_helpers.detect_dialect(source.data, format=source.format, scheme=source.scheme,
                                                          custom_loaders=custom_loaders).delimiter
48
    with tabulator.Stream(source.data, format=source.format, scheme=source.scheme, custom_loaders=custom_loaders,
49
                          **options) as stream:
50
51
        for row in stream:
            if header is None:
52
                header = ['' if v is None else v for v in row]
53
            else:
54
                rows.append(list(map(stringify, row)))
Pierre Dittgen's avatar
Pierre Dittgen committed
55
                nb_rows += 1
56
    preview_rows_nb = min(preview_rows_nb, nb_rows)
57
58
    return {'header': header,
            'rows_nb': nb_rows,
59
60
61
            'data_rows': rows,
            'preview_rows_nb': preview_rows_nb,
            'preview_rows': rows[:preview_rows_nb]}
62
63


64
65
def improve_messages(errors, headers, schema):
    """ Translates and improve error messages """
66

67
68
69
70
71
    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
72

73
    improved_errors = []
Pierre Dittgen's avatar
Pierre Dittgen committed
74

75
    for error in errors:
Pierre Dittgen's avatar
Pierre Dittgen committed
76

77
78
        improve_func = ERROR_MESSAGE_FUNC.get(error['code'], error_message_default_func)
        improved_errors.append(improve_func(error, headers, schema))
Pierre Dittgen's avatar
Pierre Dittgen committed
79

80
    return improved_errors
Pierre Dittgen's avatar
Pierre Dittgen committed
81
82


83
84
def contextualize(errors):
    """ add context to errors """
85

86
87
88
89
    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}
90

91
    return list(map(add_context, errors))
92
93


94
def create_validata_report(goodtables_report, schema):
95
96
97
98
99
100
    """ 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"
101
        - error messages are improved
102
103
104
105
106
107
108
109
110
111
112
    """
    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']
113
114
115
    # 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
116
117
118
119
    # Handy col_count info
    headers = report['table'].get('headers', [])
    report['table']['col_count'] = len(headers)

Pierre Dittgen's avatar
Pierre Dittgen committed
120
    # Computes column info
121
    schema_fields = schema.get('fields', [])
Pierre Dittgen's avatar
Pierre Dittgen committed
122
123
124
125
    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]
126

127
128
    # Add context to errors
    errors = contextualize(report['table']['errors'])
129
130
    del report['table']['errors']

131
132
133
    # Provide better (french) messages
    errors = improve_messages(errors, headers, schema)

134
135
136
137
138
139
140
141
142
143
144
145
    # 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
146
    # Checks if there are structure errors different to invalid-column-delimiter
147
148
149
    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
150
151
152
153
    # Group body errors by row id
    rows = []
    current_row_id = 0
    for err in report['table']['errors']['body']:
154
155
        if not 'row-number' in err:
            print('ERR', err)
Pierre Dittgen's avatar
Pierre Dittgen committed
156
157
158
159
160
161
162
163
164
165
166
167
168
        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
169
    report['table']['errors']['by_rows'] = rows
Pierre Dittgen's avatar
Pierre Dittgen committed
170
171
172
173

    return report


174
def validate(schema_code, source: ValidataSource):
175
176
    """ Validate source and display report """

Pierre Dittgen's avatar
Pierre Dittgen committed
177
178
179
180
181
    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))
182
183

    source_data = extract_source_data(source)
184

185
    validata_report = create_validata_report(goodtables_report, ValidatorHelper.schema(schema_code))
186

187
    # return jsonify(validata_report)
Pierre Dittgen's avatar
Pierre Dittgen committed
188

Pierre Dittgen's avatar
Pierre Dittgen committed
189
190
    # Complete report
    val_info = ValidatorHelper.schema_info(schema_code)
191
    return render_template('validation_report.html', title='Rapport de validation',
192
                           val_info=ValidatorHelper.schema_info(schema_code), report=validata_report,
193
                           source=source, source_type=source.type, source_data=source_data,
194
                           report_str=json.dumps(validata_report, sort_keys=True, indent=2),
Pierre Dittgen's avatar
Pierre Dittgen committed
195
                           breadcrumbs=[{'url': url_for('home'), 'title': 'Accueil'},
Pierre Dittgen's avatar
Pierre Dittgen committed
196
197
                                        {'url': url_for('scdl_validator', val_code=schema_code),
                                         'title': val_info['title']}])
198
199


200
201
202
203
204
205
206
207
def bytes_data(f):
    """ Gets bytes data from Werkzeug FileStorage instance """
    iob = BytesIO()
    f.save(iob)
    iob.seek(0)
    return iob.getvalue()


208
209
210
211
212
213
# Routes


@app.route('/')
def home():
    """ Home page """
Pierre Dittgen's avatar
Pierre Dittgen committed
214
    validators = ValidatorHelper.schema_info_list()
Pierre Dittgen's avatar
Pierre Dittgen committed
215
    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')
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
    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
235
        val_info = ValidatorHelper.schema_info(val_code)
236
237
238
239
        input_param = request.args.get('input')

        # First form display
        if input_param is None or input_param not in ('url', 'example'):
240
            return render_template('validation_form.html', title=val_info['title'],
241
242
243
244
245
246
247
248
249
                                   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))
250
            return validate(val_code, ValidataSource(url, url, 'url'))
251
252
253
254

    else:  # POST
        input_param = request.form.get('input')
        if input_param is None:
Pierre Dittgen's avatar
Pierre Dittgen committed
255
            flash_error('Aucun fichier à valider')
256
257
258
259
260
261
262
263
            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))
264
265

            return validate(val_code, ValidataSource(bytes_data(f), f.filename, 'file'))
266
267

        return 'Bizarre, vous avez dit bizarre ?'