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"
# 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"
# Service used to produce SVG badge
# Comment the following line to disable badge display
SHIELDS_IO_BASE_URL="https://img.shields.io/"
# Validata API endpoint
......
......@@ -28,18 +28,6 @@ API_VALIDATE_ENDPOINT = os.environ.get("API_VALIDATE_ENDPOINT") or None
if API_VALIDATE_ENDPOINT is None:
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
if SHIELDS_IO_BASE_URL and not SHIELDS_IO_BASE_URL.endswith('/'):
SHIELDS_IO_BASE_URL += '/'
......
......@@ -31,7 +31,9 @@
{% endif %}
<h1 class="my-4">{{ title }}</h1>
<p>Validation effectuée {{ validation_date }}</p>
{% if display_badge %}
<p><img src="{{ badge_url }}" alt="{{ badge_msg }}" title="{{ badge_msg }}"/></p>
{% endif %}
{% if print_mode %}
</div>
<div class="col-md-7">
......
......@@ -20,7 +20,7 @@ from backports.datetime_fromisoformat import MonkeyPatch
from commonmark import commonmark
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 .ui_util import flash_error, flash_warning
......@@ -34,13 +34,13 @@ log = logging.getLogger(__name__)
class SchemaInstance():
"""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
but via from_parameters() static method!"""
self.url = url
self.name = name
self.ref = ref
self.spec = spec
self.schema = schema
self.versions = versions
self.doc_url = doc_url
......@@ -88,7 +88,7 @@ class SchemaInstance():
return None
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):
if self.name:
......@@ -338,10 +338,13 @@ def validate(schema_instance: SchemaInstance, source: ValidataResource):
json_response = req.json()
validata_core_report = json_response['report']
badge_info = json_response.get('badge')
# Computes badge from report and badge configuration
badge = compute_badge(validata_core_report, config.BADGE_CONFIG)
badge_url, badge_msg = get_badge_url_and_message(badge)
badge_url, badge_msg = None, None
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 = [
err
......@@ -361,11 +364,11 @@ def validate(schema_instance: SchemaInstance, source: ValidataResource):
report_datetime = datetime.fromisoformat(validata_core_report['date']).astimezone()
# 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
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())
return render_template('validation_report.html', title='Rapport de validation',
schema_info=schema_info, report=validata_report,
......@@ -375,7 +378,7 @@ def validate(schema_instance: SchemaInstance, source: ValidataResource):
schema_current_version=schema_instance.ref,
doc_url=schema_instance.doc_url,
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),
breadcrumbs=[{'url': url_for('home'), 'title': 'Accueil'},
{'url': validator_form_url, 'title': validator_title},
......@@ -528,7 +531,7 @@ def custom_validator():
if input_param is None:
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,
schema_info=schema_info, schema_versions=schema_versions,
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