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

from validata_ui_next import app
from validata_ui_next.util import flash_error, flash_info, flash_success, flash_warning
12
from validata_ui_next.validate_helper import ValidatorHelper
13

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


def compute_source_preview(source, max_rows=5):
    """ Computes table preview """
    header = None
    rows = []
Pierre Dittgen's avatar
Pierre Dittgen committed
22
    nb_rows = 0
23 24 25 26 27
    with tabulator.Stream(source) as stream:
        for row in stream:
            if header is None:
                header = row
            else:
Pierre Dittgen's avatar
Pierre Dittgen committed
28 29 30 31
                nb_rows += 1
                if len(rows) <= max_rows:
                    rows.append(row)
    return {'header': header, 'data_rows': rows, 'total_rows_nb': nb_rows, 'rows_nb': len(rows)}
32 33


Pierre Dittgen's avatar
Pierre Dittgen committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 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
    ('wrong-column-delimiter', 'table')

    # Custom checks fall in default case: body
])


def contextualize(report):
    """ add context to errors """
    errors = report['tables'][0].get('errors')
    if errors is None:
        return report

    errors = [{**err, 'context': ERR_CODE_TO_CONTEXT.get(err['code'], 'body')} for err in errors]
    report['tables'][0]['errors'] = errors

    return report


81 82 83 84
def validate(schema_code, source, source_type):
    """ Validate source and display report """

    report = ValidatorHelper.validate(schema_code, source, source_type)
Pierre Dittgen's avatar
Pierre Dittgen committed
85 86
    report = contextualize(report)

87 88
    preview = compute_source_preview(source)

Pierre Dittgen's avatar
Pierre Dittgen committed
89 90
    # Complete report
    val_info = ValidatorHelper.schema_info(schema_code)
91 92 93
    return render_template('validation_report.html', title='Rapport de validation',
                           val_info=ValidatorHelper.schema_info(schema_code), report=report,
                           source=source, source_type=source_type, preview=preview,
Pierre Dittgen's avatar
Pierre Dittgen committed
94 95 96
                           report_str=json.dumps(report, sort_keys=True, indent=2),
                           breadcrumbs=[{'url': url_for('home'), 'title': 'Accueil'},
                                        {'url': url_for('scdl_validator', val_code=schema_code), 'title': val_info['title']}])
97 98 99 100 101 102 103 104


# Routes


@app.route('/')
def home():
    """ Home page """
Pierre Dittgen's avatar
Pierre Dittgen committed
105
    validators = ValidatorHelper.schema_info_list()
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    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
132
        val_info = ValidatorHelper.schema_info(val_code)
133 134 135 136 137 138 139 140 141 142 143 144 145 146
        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))
Pierre Dittgen's avatar
Pierre Dittgen committed
147
            return validate(val_code, url, 'url')
148 149 150 151 152 153 154 155 156 157 158 159 160

    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))
Pierre Dittgen's avatar
Pierre Dittgen committed
161 162 163
            fpath = os.path.join('/tmp', f.filename)
            f.save(fpath)
            return validate(val_code, fpath, 'file')
164 165

        return 'Bizarre, vous avez dit bizarre ?'