Commit 048de191 authored by Christophe Benz's avatar Christophe Benz
Browse files

Configure app with environment

parent 722eeabe
# Example config file. Copy and customize this file to ".env". Don't commit ".env".
# For production deployment, see
# Comment the two following lines to disable "badge" generation.
......@@ -20,6 +20,18 @@ Install the project dependencies:
pip install -e .
## Configuration
cp .env.example .env
Customize the configuration variables in `.env` file.
Do not commit `.env`.
See also:
## Development
Start the web server...
......@@ -29,6 +29,7 @@ setup(
import os
from pathlib import Path
from urllib.parse import quote_plus
......@@ -5,7 +6,10 @@ import flask
import jinja2
import validata_core
from validata_ui.validate_helper import ValidatorHelper
# Let this import after app initialisation
from . import config
from .validate_helper import ValidatorHelper
# Schemas settings
schemas_config = validata_core.get_schemas_config()
......@@ -13,17 +17,19 @@ ValidatorHelper.init(schemas_config)
# Flask things
app = flask.Flask(__name__)
app.secret_key = 'MyPr3ci0u5$€cr€t'
app.secret_key = config.SECRET_KEY
# Jinja2 url_quote_plus custom filter
blueprint = flask.Blueprint('filters', __name__)
def urlencode(context, value):
return quote_plus(value)
# Let this import after app initialisation
import validata_ui.views
# Keep this import after app initialisation (to avoid cyclic imports)
from . import views # isort:skip
import logging
import os
import requests
import toml
from dotenv import load_dotenv
log = logging.getLogger(__name__)
SECRET_KEY = os.environ.get("SECRET_KEY") or None
BADGE_CONFIG_URL = os.environ.get("BADGE_CONFIG_URL") or None
log.warning("BADGE_CONFIG_URL environment variable is not set, disable badge feature")
response = requests.get(BADGE_CONFIG_URL)
if not response.ok:
log.warning("Can't retrieve badge config from [%s], disable badge feature", BADGE_CONFIG_URL)
BADGE_CONFIG = toml.loads(response.text)
SHIELDS_IO_BASE_URL = os.environ.get("SHIELDS_IO_BASE_URL") or None
if not SHIELDS_IO_BASE_URL.endswith('/'):
......@@ -5,7 +5,6 @@ import copy
import itertools
import json
import logging
import os
import subprocess
import tempfile
from datetime import datetime
......@@ -14,45 +13,24 @@ from operator import itemgetter
from pathlib import Path
from urllib.parse import quote_plus
import requests
import toml
from backports.datetime_fromisoformat import MonkeyPatch
from commonmark import commonmark
from flask import make_response, redirect, render_template, request, url_for
import tabulator
from validata_core import compute_badge, csv_helpers, messages
from validata_core.loaders import custom_loaders
import tabulator
from validata_ui import app
from validata_ui.ui_util import flash_error, flash_warning
from validata_ui.validata_util import ValidataSource
from validata_ui.validate_helper import ValidatorHelper
from . import app, config
from .ui_util import flash_error, flash_warning
from .validata_util import ValidataSource
from .validate_helper import ValidatorHelper
log = logging.getLogger(__name__)
# Get badge configuration file url from environment variable
badge_config = None
badge_config_url = os.environ.get('BADGE_CONFIG_URL')
if badge_config_url is None:
log.warning("BADGE_CONFIG_URL environment variable is not set")
req = requests.get(badge_config_url)
if not req.ok:
log.warning("Can't retrieve badge config from [%s]", badge_config_url)
badge_config = toml.loads(req.text)
# Gets shields io url from environment variable
shields_io_url = os.environ.get('SHIELDS_IO_URL')
if shields_io_url is None:
shields_io_url = ''
elif not shields_io_url.endswith('/'):
shields_io_url += '/'
def extract_source_data(source: ValidataSource, preview_rows_nb=5):
""" Computes table preview """
......@@ -252,8 +230,8 @@ def get_badge_url_and_message(badge):
"""Gets badge url from badge information"""
msg, color = compute_badge_message_and_color(badge)
return ('{}static/v1.svg?label=Validata&message={}&color={}'
.format(shields_io_url, quote_plus(msg), color), msg)
return ('{}static/v1.svg?label=Validata&message={}&color={}'.format(
config.SHIELDS_IO_BASE_URL, quote_plus(msg), color), msg)
def validate(schema_code, source: ValidataSource):
......@@ -276,7 +254,7 @@ def validate(schema_code, source: ValidataSource):
return redirect(url_for('scdl_validator', val_code=schema_code))
# Computes badge from report and badge configuration
badge = compute_badge(validata_core_report, badge_config)
badge = compute_badge(validata_core_report, config.BADGE_CONFIG)
badge_url, badge_msg = get_badge_url_and_message(badge)
source_errors = [err for err in validata_core_report['tables'][0]['errors'] if err['code'] == 'source-error']
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment