views.py 3.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
#!/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
from validata_ui_next.validate import ValidatorHelper

from flask import Flask, flash, redirect, render_template, request, url_for


def validate_url(schema_code, url):
    ValidatorHelper.validate_url(schema_code, url)
    return "TODO: valider l'url "+url
    # return render_template('validation_report.html')


def validate_file(schema_code, file):
    ValidatorHelper.validate_file(schema_code, file)
    return "TODO: valider le fichier "+file
    # return render_template('validation_report.html')


# Supplementary schema info
SCHEMA_ADDON_FILE = Path(__file__).parent / 'static/data/scdl-addon-info.json'
SCHEMA_ADDON_INFO = json.load(SCHEMA_ADDON_FILE.open('rt', encoding='utf-8'))


def suppl_schema_info(d):
    """ Add editorial info to schema info"""

    code = d['code']
    return {**d, **SCHEMA_ADDON_INFO[code]} if code in SCHEMA_ADDON_INFO else d

# Routes


@app.route('/')
def home():
    """ Home page """
    validators = list(map(suppl_schema_info, ValidatorHelper.schema_info_list()))
    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':

        val_info = suppl_schema_info(ValidatorHelper.schema_info(val_code))
        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))
            return validate_url(val_code, url)

    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))
            f.save(os.path.join('/tmp', f.filename))
            return validate_file(val_code, f.filename)

        return 'Bizarre, vous avez dit bizarre ?'