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

Configure app with environment

parent 722eeabe
# Example config file. Copy and customize this file to ".env". Don't commit ".env".
# For production deployment, see http://flask.pocoo.org/docs/1.0/tutorial/deploy/#configure-the-secret-key
SECRET_KEY="dev"
# Comment the two following lines to disable "badge" generation.
BADGE_CONFIG_URL="https://git.opendatafrance.net/validata/validata-badge/raw/master/badge_conf.toml"
SHIELDS_IO_BASE_URL="https://img.shields.io/"
__pycache__/ __pycache__/
validata_ui.egg-info/ validata_ui.egg-info/
.vscode .env
.vscode/
...@@ -20,6 +20,18 @@ Install the project dependencies: ...@@ -20,6 +20,18 @@ Install the project dependencies:
pip install -e . pip install -e .
``` ```
## Configuration
```bash
cp .env.example .env
```
Customize the configuration variables in `.env` file.
Do not commit `.env`.
See also: https://github.com/theskumar/python-dotenv
## Development ## Development
Start the web server... Start the web server...
......
...@@ -29,6 +29,7 @@ setup( ...@@ -29,6 +29,7 @@ setup(
'ezodf', 'ezodf',
'flask', 'flask',
'lxml', 'lxml',
'python-dotenv',
'requests', 'requests',
'toml', 'toml',
......
import os
from pathlib import Path from pathlib import Path
from urllib.parse import quote_plus from urllib.parse import quote_plus
...@@ -5,7 +6,10 @@ import flask ...@@ -5,7 +6,10 @@ import flask
import jinja2 import jinja2
import validata_core 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 settings
schemas_config = validata_core.get_schemas_config() schemas_config = validata_core.get_schemas_config()
...@@ -13,17 +17,19 @@ ValidatorHelper.init(schemas_config) ...@@ -13,17 +17,19 @@ ValidatorHelper.init(schemas_config)
# Flask things # Flask things
app = flask.Flask(__name__) app = flask.Flask(__name__)
app.secret_key = 'MyPr3ci0u5$€cr€t' app.secret_key = config.SECRET_KEY
# Jinja2 url_quote_plus custom filter # Jinja2 url_quote_plus custom filter
# https://stackoverflow.com/questions/12288454/how-to-import-custom-jinja2-filters-from-another-file-and-using-flask # https://stackoverflow.com/questions/12288454/how-to-import-custom-jinja2-filters-from-another-file-and-using-flask
blueprint = flask.Blueprint('filters', __name__) blueprint = flask.Blueprint('filters', __name__)
@jinja2.contextfilter @jinja2.contextfilter
@blueprint.app_template_filter() @blueprint.app_template_filter()
def urlencode(context, value): def urlencode(context, value):
return quote_plus(value) return quote_plus(value)
# Let this import after app initialisation # Keep this import after app initialisation (to avoid cyclic imports)
import validata_ui.views from . import views # isort:skip
import logging
import os
import requests
import toml
from dotenv import load_dotenv
log = logging.getLogger(__name__)
load_dotenv()
SECRET_KEY = os.environ.get("SECRET_KEY") or None
BADGE_CONFIG_URL = os.environ.get("BADGE_CONFIG_URL") or None
BADGE_CONFIG = None
if BADGE_CONFIG_URL is None:
log.warning("BADGE_CONFIG_URL environment variable is not set, disable badge feature")
else:
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)
else:
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('/'):
SHIELDS_IO_BASE_URL += '/'
...@@ -5,7 +5,6 @@ import copy ...@@ -5,7 +5,6 @@ import copy
import itertools import itertools
import json import json
import logging import logging
import os
import subprocess import subprocess
import tempfile import tempfile
from datetime import datetime from datetime import datetime
...@@ -14,45 +13,24 @@ from operator import itemgetter ...@@ -14,45 +13,24 @@ from operator import itemgetter
from pathlib import Path from pathlib import Path
from urllib.parse import quote_plus from urllib.parse import quote_plus
import requests
import toml
from backports.datetime_fromisoformat import MonkeyPatch from backports.datetime_fromisoformat import MonkeyPatch
from commonmark import commonmark from commonmark import commonmark
from flask import make_response, redirect, render_template, request, url_for 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 import compute_badge, csv_helpers, messages
from validata_core.loaders import custom_loaders from validata_core.loaders import custom_loaders
import tabulator from . import app, config
from validata_ui import app from .ui_util import flash_error, flash_warning
from validata_ui.ui_util import flash_error, flash_warning from .validata_util import ValidataSource
from validata_ui.validata_util import ValidataSource from .validate_helper import ValidatorHelper
from validata_ui.validate_helper import ValidatorHelper
MonkeyPatch.patch_fromisoformat() MonkeyPatch.patch_fromisoformat()
log = logging.getLogger(__name__) 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")
else:
req = requests.get(badge_config_url)
if not req.ok:
log.warning("Can't retrieve badge config from [%s]", badge_config_url)
else:
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 = 'https://img.shields.io/'
elif not shields_io_url.endswith('/'):
shields_io_url += '/'
def extract_source_data(source: ValidataSource, preview_rows_nb=5): def extract_source_data(source: ValidataSource, preview_rows_nb=5):
""" Computes table preview """ """ Computes table preview """
...@@ -252,8 +230,8 @@ def get_badge_url_and_message(badge): ...@@ -252,8 +230,8 @@ def get_badge_url_and_message(badge):
"""Gets badge url from badge information""" """Gets badge url from badge information"""
msg, color = compute_badge_message_and_color(badge) msg, color = compute_badge_message_and_color(badge)
return ('{}static/v1.svg?label=Validata&message={}&color={}' return ('{}static/v1.svg?label=Validata&message={}&color={}'.format(
.format(shields_io_url, quote_plus(msg), color), msg) config.SHIELDS_IO_BASE_URL, quote_plus(msg), color), msg)
def validate(schema_code, source: ValidataSource): def validate(schema_code, source: ValidataSource):
...@@ -276,7 +254,7 @@ 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)) return redirect(url_for('scdl_validator', val_code=schema_code))
# Computes badge from report and badge configuration # 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) 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'] source_errors = [err for err in validata_core_report['tables'][0]['errors'] if err['code'] == 'source-error']
......
Markdown is supported
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