def resource_schema_details(ctx): schemas = load_catalog() resource = ctx['resource'] authorize_validation = is_table_schema(schemas, resource.schema['name']) validation_url = None if authorize_validation: schema_url = None if 'version' in resource.schema: schema_url = get_schema_url( schemas, resource.schema['name'], resource.schema['version'], ) validation_url = validata_url(resource, schema_url) documentation_url = f"https://schema.data.gouv.fr/{resource.schema['name']}/latest.html" return theme.render( 'dataset/resource/schema-button.html', resource=resource, documentation_url=documentation_url, validation_url=validation_url, authorize_validation=authorize_validation )
def show(post): others = Post.objects(id__ne=post.id).published() older = others(published__lt=post.published).order_by('-published') newer = others(published__gt=post.published).order_by('published') return theme.render('post/display.html', post=post, previous_post=older.first(), next_post=newer.first())
def get(self): """ An OEmbed compliant API endpoint See: http://oembed.com/ Support datasets and reuses URLs """ args = oembed_parser.parse_args() if args['format'] != 'json': return {'message': 'Only JSON format is supported'}, 501 url = args['url'] # Fix flask not detecting URL with https://domain:443/ if 'https:' in url and ':443/' in url: url = url.replace(':443/', '/') with current_app.test_request_context(url) as ctx: if not ctx.request.endpoint: return {'message': 'Unknown URL "{0}"'.format(url)}, 404 endpoint = ctx.request.endpoint.replace('_redirect', '') view_args = ctx.request.view_args if endpoint not in self.ROUTES: return { 'message': 'The URL "{0}" does not support oembed'.format(url) }, 404 param, prefix = self.ROUTES[endpoint] item = view_args[param] if isinstance(item, Exception): if isinstance(item, HTTPException): return { 'message': 'An error occured on URL "{0}": {1}'.format( url, str(item)) }, item.code raise item width = maxwidth = 1000 height = maxheight = 200 params = { 'width': width, 'height': height, 'item': item, 'type': prefix } params[param] = item html = theme.render('oembed.html', **params) return { 'type': 'rich', 'version': '1.0', 'html': html, 'width': width, 'height': height, 'maxwidth': maxwidth, 'maxheight': maxheight, }
def home(): context = { 'spd_datasets': Dataset.objects.filter(badges__kind='spd'), 'recent_datasets': Dataset.objects.visible(), 'recent_reuses': Reuse.objects(featured=True).visible(), 'last_post': Post.objects.published().first() } processor = theme.current.get_processor('home') context = processor(context) return theme.render('home.html', **context)
def show_page(slug): content, gh_url = get_page_content(slug) page = frontmatter.loads(content) reuses = [get_object(Reuse, r) for r in page.get('reuses') or []] datasets = [get_object(Dataset, d) for d in page.get('datasets') or []] reuses = [r for r in reuses if r is not None] datasets = [d for d in datasets if d is not None] return theme.render('page.html', page=page, reuses=reuses, datasets=datasets, gh_url=gh_url)
def render_territory(territory): if not current_app.config.get('ACTIVATE_TERRITORIES'): return abort(404) is_present_territory = territory.valid_at(date.today()) # Retrieve the present territory if not presently valid. present_territory = None if not is_present_territory: present_territory = GeoZone.objects.valid_at(date.today()).get( level=territory.level, ancestors__contains=territory.id) # Only display dynamic datasets for present territories. base_datasets = [] if is_present_territory: DATASETS = TERRITORY_DATASETS[territory.level_code] base_dataset_classes = sorted(DATASETS.values(), key=lambda a: a.order) base_datasets = [ base_dataset_class(territory) for base_dataset_class in base_dataset_classes ] territories = [territory] # Deal with territories with ancestors. for ancestor_object in territory.ancestors_objects: territories.append(ancestor_object) # Retrieve all datasets then split between those optionally owned # by an org for that zone and others. We need to know if the current # user has datasets for that zone in order to display a custom # message to ease the conversion. datasets = Dataset.objects(spatial__zones__in=territories).visible() # Retrieving datasets from old regions. territory_datasets = [] other_datasets = [] if datasets: for dataset in datasets: if (dataset.organization and territory.id == dataset.organization.zone): territory_datasets.append(dataset) else: other_datasets.append(dataset) context = { 'territory': territory, 'present_territory': present_territory, 'base_datasets': base_datasets, 'other_datasets': other_datasets, 'territory_datasets': territory_datasets } template = 'territories/{level_name}.html'.format( level_name=territory.level_name) return theme.render(template, **context)
def test_oembed_for_dataset_with_organization(self, api): '''It should fetch a dataset in the oembed format with org.''' organization = OrganizationFactory() dataset = DatasetFactory(organization=organization) url = url_for('api.oembed', url=dataset.external_url) response = api.get(url) assert200(response) assert_cors(response) card = theme.render('dataset/card.html', dataset=dataset) assert card in response.json['html']
def test_oembed_for_org(self, api): '''It should fetch an organization in the oembed format.''' org = OrganizationFactory() url = url_for('api.oembed', url=org.external_url) response = api.get(url) assert200(response) assert_cors(response) assert 'html' in response.json assert 'width' in response.json assert 'maxwidth' in response.json assert 'height' in response.json assert 'maxheight' in response.json assert response.json['type'] == 'rich' assert response.json['version'] == '1.0' card = theme.render('organization/card.html', organization=org) assert card in response.json['html']
def test_oembed_for_reuse(self, api): '''It should fetch a reuse in the oembed format.''' reuse = ReuseFactory() url = url_for('api.oembed', url=reuse.external_url) response = api.get(url) assert200(response) assert_cors(response) assert 'html' in response.json assert 'width' in response.json assert 'maxwidth' in response.json assert 'height' in response.json assert 'maxheight' in response.json assert response.json['type'] == 'rich' assert response.json['version'] == '1.0' card = theme.render('reuse/card.html', reuse=reuse) assert card in response.json['html']
def test_oembed_for_dataset(self, api): '''It should fetch a dataset in the oembed format.''' dataset = DatasetFactory() url = url_for('api.oembed', url=dataset.external_url) response = api.get(url) assert200(response) assert_cors(response) assert 'html' in response.json assert 'width' in response.json assert 'maxwidth' in response.json assert 'height' in response.json assert 'maxheight' in response.json assert response.json['type'] == 'rich' assert response.json['version'] == '1.0' card = theme.render('dataset/card.html', dataset=dataset) assert card in response.json['html']
def render_home(): if not current_app.config.get('ACTIVATE_TERRITORIES'): return abort(404) highest_level = current_app.config['HANDLED_LEVELS'][-1] regions = GeoZone.objects(level=highest_level).valid_at(date.today()) regions = sorted(regions, key=lambda zone: unicodedata.normalize('NFD', zone.name). encode('ascii', 'ignore')) return theme.render( 'territories/home.html', **{ 'geojson': { 'type': 'FeatureCollection', 'features': [region.toGeoJSON() for region in regions] }, 'regions': regions })
def test_oembed_for_dataset_redirect_link(self, api): '''It should fetch an oembed dataset using the redirect link.''' dataset = DatasetFactory() redirect_url = url_for('datasets.show_redirect', dataset=dataset, _external=True) url = url_for('api.oembed', url=redirect_url) response = api.get(url) assert200(response) assert_cors(response) assert 'html' in response.json assert 'width' in response.json assert 'maxwidth' in response.json assert 'height' in response.json assert 'maxheight' in response.json assert response.json['type'] == 'rich' assert response.json['version'] == '1.0' card = theme.render('dataset/card.html', dataset=dataset) assert card in response.json['html']
def render(self, context=None, **kwargs): context = context or self.get_context() context.update(kwargs) return theme.render(self.get_template_name(), **context)
def page_not_found(error): return theme.render('errors/404.html', error=error), 404
def get(self): """ The returned payload is a list of OEmbed formatted responses. See: http://oembed.com/ The `references` are composed by a keyword (`kind`) followed by the `id` each of those separated by commas. E.g: dataset-5369992aa3a729239d205183, territory-fr:departement:33@1860-07-01:emploi_dep Only datasets and territories are supported for now. """ args = oembeds_parser.parse_args() references = args['references'].split(',') result = [] for item_reference in references: try: item_kind, item_id = item_reference.split('-', 1) except ValueError: return api.abort(400, 'Invalid ID.') if item_kind == 'dataset': try: item = Dataset.objects.get(id=item_id) except (db.ValidationError, Dataset.DoesNotExist): return api.abort(400, 'Unknown dataset ID.') elif (item_kind == 'territory' and current_app.config.get('ACTIVATE_TERRITORIES')): try: country, level, code, kind = item_id.split(':') except ValueError: return api.abort(400, 'Invalid territory ID.') geoid = ':'.join((country, level, code)) zone = GeoZone.objects.resolve(geoid) if not zone: return api.abort(400, 'Unknown territory identifier.') if level in TERRITORY_DATASETS: if kind in TERRITORY_DATASETS[level]: item = TERRITORY_DATASETS[level][kind](zone) else: return api.abort(400, 'Unknown territory dataset id.') else: return api.abort(400, 'Unknown kind of territory.') else: return api.abort(400, 'Invalid object type.') width = maxwidth = 1000 height = maxheight = 200 html = theme.render( 'embed-dataset.html', **{ 'width': width, 'height': height, 'item': item, 'item_reference': item_reference, }) result.append({ 'type': 'rich', 'version': '1.0', 'html': html, 'width': width, 'height': height, 'maxwidth': maxwidth, 'maxheight': maxheight, }) return result
def map(): return theme.render('site/map.html')
def page_deleted(error): return theme.render('errors/410.html', error=error), 410
def forbidden(error): return theme.render('errors/403.html', error=error), 403
def internal_error(error): return theme.render('errors/500.html', error=error, sentry_event_id=last_event_id()), 500
def dataset_apis(ctx): dataset = ctx['dataset'] return theme.render('dataset-apis.html', apis=dataset.extras.get(APIGOUVFR_EXTRAS_KEY))
def suivi(): try: return theme.render('suivi.html') except TemplateNotFound: abort(404)
def terms(): content = get_terms_content() return theme.render('terms.html', terms=content)
def validation_error(error): error_label = '{0.__class__.__name__}({0})'.format(error) log.error('Uncaught error: %s', error_label, exc_info=True) return theme.render('errors/400.html', error=error, sentry_event_id=last_event_id()), 400