Commit 6be6987b authored by Pierre Dittgen's avatar Pierre Dittgen

Rework badge generation

parent fee5452d
Pipeline #1039 failed with stage
in 21 seconds
...@@ -5,8 +5,8 @@ LOG_LEVEL="INFO" ...@@ -5,8 +5,8 @@ LOG_LEVEL="INFO"
# For production deployment, see http://flask.pocoo.org/docs/1.0/tutorial/deploy/#configure-the-secret-key # For production deployment, see http://flask.pocoo.org/docs/1.0/tutorial/deploy/#configure-the-secret-key
SECRET_KEY="dev" SECRET_KEY="dev"
# Comment the two following lines to disable "badge" generation. # Service used to produce SVG badge
BADGE_CONFIG_URL="https://git.opendatafrance.net/validata/validata-badge/raw/master/badge_conf.toml" # Comment the following line to disable badge display
SHIELDS_IO_BASE_URL="https://img.shields.io/" SHIELDS_IO_BASE_URL="https://img.shields.io/"
# Validata API endpoint # Validata API endpoint
......
...@@ -28,18 +28,6 @@ API_VALIDATE_ENDPOINT = os.environ.get("API_VALIDATE_ENDPOINT") or None ...@@ -28,18 +28,6 @@ API_VALIDATE_ENDPOINT = os.environ.get("API_VALIDATE_ENDPOINT") or None
if API_VALIDATE_ENDPOINT is None: if API_VALIDATE_ENDPOINT is None:
log.error("API_VALIDATE_ENDPOINT environment variable is not set, validation is not possible") log.error("API_VALIDATE_ENDPOINT environment variable is not set, validation is not possible")
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 SHIELDS_IO_BASE_URL = os.environ.get("SHIELDS_IO_BASE_URL") or None
if SHIELDS_IO_BASE_URL and not SHIELDS_IO_BASE_URL.endswith('/'): if SHIELDS_IO_BASE_URL and not SHIELDS_IO_BASE_URL.endswith('/'):
SHIELDS_IO_BASE_URL += '/' SHIELDS_IO_BASE_URL += '/'
......
...@@ -31,7 +31,9 @@ ...@@ -31,7 +31,9 @@
{% endif %} {% endif %}
<h1 class="my-4">{{ title }}</h1> <h1 class="my-4">{{ title }}</h1>
<p>Validation effectuée {{ validation_date }}</p> <p>Validation effectuée {{ validation_date }}</p>
{% if display_badge %}
<p><img src="{{ badge_url }}" alt="{{ badge_msg }}" title="{{ badge_msg }}"/></p> <p><img src="{{ badge_url }}" alt="{{ badge_msg }}" title="{{ badge_msg }}"/></p>
{% endif %}
{% if print_mode %} {% if print_mode %}
</div> </div>
<div class="col-md-7"> <div class="col-md-7">
......
...@@ -20,7 +20,7 @@ from backports.datetime_fromisoformat import MonkeyPatch ...@@ -20,7 +20,7 @@ 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
from validata_core import compute_badge, messages from validata_core import messages
from . import app, config, schema_catalog_map, schema_from_url from . import app, config, schema_catalog_map, schema_from_url
from .ui_util import flash_error, flash_warning from .ui_util import flash_error, flash_warning
...@@ -34,13 +34,13 @@ log = logging.getLogger(__name__) ...@@ -34,13 +34,13 @@ log = logging.getLogger(__name__)
class SchemaInstance(): class SchemaInstance():
"""Handly class to handle schema information""" """Handly class to handle schema information"""
def __init__(self, url=None, name=None, ref=None, spec=None, versions=None, doc_url=None): def __init__(self, url=None, name=None, ref=None, schema=None, versions=None, doc_url=None):
"""This function is not intended to be called directly """This function is not intended to be called directly
but via from_parameters() static method!""" but via from_parameters() static method!"""
self.url = url self.url = url
self.name = name self.name = name
self.ref = ref self.ref = ref
self.spec = spec self.schema = schema
self.versions = versions self.versions = versions
self.doc_url = doc_url self.doc_url = doc_url
...@@ -88,7 +88,7 @@ class SchemaInstance(): ...@@ -88,7 +88,7 @@ class SchemaInstance():
return None return None
return SchemaInstance(url=schema_url, name=schema_name, ref=schema_ref, return SchemaInstance(url=schema_url, name=schema_name, ref=schema_ref,
spec=schema_from_url(schema_url), versions=versions, doc_url=doc_url) schema=schema_from_url(schema_url), versions=versions, doc_url=doc_url)
def request_parameters(self): def request_parameters(self):
if self.name: if self.name:
...@@ -338,10 +338,13 @@ def validate(schema_instance: SchemaInstance, source: ValidataResource): ...@@ -338,10 +338,13 @@ def validate(schema_instance: SchemaInstance, source: ValidataResource):
json_response = req.json() json_response = req.json()
validata_core_report = json_response['report'] validata_core_report = json_response['report']
badge_info = json_response.get('badge')
# Computes badge from report and badge configuration # Computes badge from report and badge configuration
badge = compute_badge(validata_core_report, config.BADGE_CONFIG) badge_url, badge_msg = None, None
badge_url, badge_msg = get_badge_url_and_message(badge) display_badge = badge_info and config.SHIELDS_IO_BASE_URL
if display_badge:
badge_url, badge_msg = get_badge_url_and_message(badge_info)
source_errors = [ source_errors = [
err err
...@@ -361,11 +364,11 @@ def validate(schema_instance: SchemaInstance, source: ValidataResource): ...@@ -361,11 +364,11 @@ def validate(schema_instance: SchemaInstance, source: ValidataResource):
report_datetime = datetime.fromisoformat(validata_core_report['date']).astimezone() report_datetime = datetime.fromisoformat(validata_core_report['date']).astimezone()
# Enhance validata_core_report # Enhance validata_core_report
validata_report = create_validata_ui_report(validata_core_report, schema_instance.spec) validata_report = create_validata_ui_report(validata_core_report, schema_instance.schema.descriptor)
# Display report to the user # Display report to the user
validator_form_url = compute_validation_form_url(schema_instance) validator_form_url = compute_validation_form_url(schema_instance)
schema_info, validator_title = compute_schema_info(schema_instance.spec, schema_instance.url) schema_info, validator_title = compute_schema_info(schema_instance.schema, schema_instance.url)
pdf_report_url = url_for('pdf_report')+'?'+urlencode(schema_instance.request_parameters()) pdf_report_url = url_for('pdf_report')+'?'+urlencode(schema_instance.request_parameters())
return render_template('validation_report.html', title='Rapport de validation', return render_template('validation_report.html', title='Rapport de validation',
schema_info=schema_info, report=validata_report, schema_info=schema_info, report=validata_report,
...@@ -375,7 +378,7 @@ def validate(schema_instance: SchemaInstance, source: ValidataResource): ...@@ -375,7 +378,7 @@ def validate(schema_instance: SchemaInstance, source: ValidataResource):
schema_current_version=schema_instance.ref, schema_current_version=schema_instance.ref,
doc_url=schema_instance.doc_url, doc_url=schema_instance.doc_url,
print_mode=request.args.get('print', 'false') == 'true', print_mode=request.args.get('print', 'false') == 'true',
badge_url=badge_url, badge_msg=badge_msg, display_badge=display_badge, badge_url=badge_url, badge_msg=badge_msg,
report_str=json.dumps(validata_report, sort_keys=True, indent=2), report_str=json.dumps(validata_report, sort_keys=True, indent=2),
breadcrumbs=[{'url': url_for('home'), 'title': 'Accueil'}, breadcrumbs=[{'url': url_for('home'), 'title': 'Accueil'},
{'url': validator_form_url, 'title': validator_title}, {'url': validator_form_url, 'title': validator_title},
...@@ -528,7 +531,7 @@ def custom_validator(): ...@@ -528,7 +531,7 @@ def custom_validator():
if input_param is None: if input_param is None:
schema_versions = schema_instance.versions schema_versions = schema_instance.versions
schema_info, title = compute_schema_info(schema_instance.spec, schema_instance.url) schema_info, title = compute_schema_info(schema_instance.schema, schema_instance.url)
return render_template('validation_form.html', title=title, return render_template('validation_form.html', title=title,
schema_info=schema_info, schema_versions=schema_versions, schema_info=schema_info, schema_versions=schema_versions,
schema_current_version=schema_instance.ref, schema_current_version=schema_instance.ref,
......
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