Commit 659dddec authored by Christophe Benz's avatar Christophe Benz

Rationalise URL building

parent 406fe564
...@@ -3,7 +3,6 @@ import logging ...@@ -3,7 +3,6 @@ import logging
import os import os
import re import re
from pathlib import Path from pathlib import Path
from urllib.parse import quote_plus
import cachecontrol import cachecontrol
import flask import flask
...@@ -89,12 +88,6 @@ if config.MATOMO_AUTH_TOKEN and config.MATOMO_BASE_URL and config.MATOMO_SITE_ID ...@@ -89,12 +88,6 @@ if config.MATOMO_AUTH_TOKEN and config.MATOMO_BASE_URL and config.MATOMO_SITE_ID
blueprint = flask.Blueprint('filters', __name__) blueprint = flask.Blueprint('filters', __name__)
@jinja2.contextfilter
@blueprint.app_template_filter()
def urlencode(context, value):
return quote_plus(value)
@jinja2.contextfilter @jinja2.contextfilter
@blueprint.app_template_filter() @blueprint.app_template_filter()
def commonmark2html(context, value): def commonmark2html(context, value):
......
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
{% if source.type == 'url' %} {% if source.type == 'url' %}
<p class="hidden-print"> <p class="hidden-print">
<a class="btn btn-outline-secondary" href="{{ pdf_report_url }}&url={{source.url|urlencode}}" target="_blank"> <a class="btn btn-outline-secondary" href="{{ pdf_report_url }}" target="_blank">
Télécharger en PDF Télécharger en PDF
</a> </a>
</p> </p>
......
...@@ -11,7 +11,7 @@ import tempfile ...@@ -11,7 +11,7 @@ import tempfile
from datetime import datetime from datetime import datetime
from operator import itemgetter from operator import itemgetter
from pathlib import Path from pathlib import Path
from urllib.parse import quote_plus, urlencode from urllib.parse import urlencode, urljoin
import requests import requests
import tableschema import tableschema
...@@ -321,8 +321,11 @@ def get_badge_url_and_message(badge): ...@@ -321,8 +321,11 @@ 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={}'.format( badge_url = "{}?{}".format(
config.SHIELDS_IO_BASE_URL, quote_plus(msg), color), msg) urljoin(config.SHIELDS_IO_BASE_URL, '/static/v1.svg'),
urlencode({"label": "Validata", "message": msg, "color": color}),
)
return (badge_url, msg)
def validate(schema_instance: SchemaInstance, source: ValidataResource): def validate(schema_instance: SchemaInstance, source: ValidataResource):
...@@ -384,7 +387,11 @@ def validate(schema_instance: SchemaInstance, source: ValidataResource): ...@@ -384,7 +387,11 @@ def validate(schema_instance: SchemaInstance, source: ValidataResource):
# Display report to the user # Display report to the user
validator_form_url = compute_validation_form_url(schema_instance.request_parameters()) validator_form_url = compute_validation_form_url(schema_instance.request_parameters())
schema_info = compute_schema_info(schema_instance.schema, schema_instance.url) schema_info = compute_schema_info(schema_instance.schema, schema_instance.url)
pdf_report_url = url_for('pdf_report') + '?' + urlencode(schema_instance.request_parameters()) pdf_report_url = "{}?{}".format(url_for('pdf_report'),
urlencode({
**schema_instance.request_parameters(),
"url": source.url,
}))
return render_template('validation_report.html', return render_template('validation_report.html',
badge_msg=badge_msg, badge_msg=badge_msg,
...@@ -471,7 +478,7 @@ def pdf_report(): ...@@ -471,7 +478,7 @@ def pdf_report():
'url': url_param, 'url': url_param,
**schema_instance.request_parameters() **schema_instance.request_parameters()
} }
validation_url = base_url + '?' + urlencode(parameter_dict) validation_url = "{}?{}".format(base_url, urlencode(parameter_dict))
# Create temp file to save validation report # Create temp file to save validation report
with tempfile.NamedTemporaryFile(prefix='validata_{}_report_'.format(datetime.now().timestamp()), suffix='.pdf') as tmpfile: with tempfile.NamedTemporaryFile(prefix='validata_{}_report_'.format(datetime.now().timestamp()), suffix='.pdf') as tmpfile:
...@@ -521,8 +528,7 @@ def compute_schema_info(table_schema: tableschema.Schema, schema_url): ...@@ -521,8 +528,7 @@ def compute_schema_info(table_schema: tableschema.Schema, schema_url):
def compute_validation_form_url(request_parameters: dict): def compute_validation_form_url(request_parameters: dict):
"""Computes validation form url with schema URL parameter""" """Computes validation form url with schema URL parameter"""
url = url_for('custom_validator') url = url_for('custom_validator')
param_list = ['{}={}'.format(k, quote_plus(v)) for k, v in request_parameters.items()] return "{}?{}".format(url, urlencode(request_parameters))
return "{}?{}".format(url, '&'.join(param_list))
@app.route('/table-schema', methods=['GET', 'POST']) @app.route('/table-schema', methods=['GET', 'POST'])
......
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