From 300be40c45fcff25e574a0734ae83efb634a8d4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knud=20M=C3=B6ller?= Date: Tue, 17 Oct 2023 11:21:35 +0200 Subject: [PATCH] add mini icons for resources --- CHANGELOG.md | 1 + ckanext/datasetsnippets/helpers.py | 14 ++ .../mappings/resource_format_mappings.json | 163 ++++++++++++++++++ ckanext/datasetsnippets/plugin.py | 11 ++ ckanext/datasetsnippets/resource_mappings.py | 66 +++++++ .../snippets/package_item.html | 18 ++ 6 files changed, 273 insertions(+) create mode 100644 ckanext/datasetsnippets/mappings/resource_format_mappings.json create mode 100644 ckanext/datasetsnippets/resource_mappings.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 41ec6ec..d4def5a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Development - Implement the [vertical "Asset Service"](http://styleguide.berlin.de/patterns/11-vertical_assetservice-page-startseite/11-vertical_assetservice-page-startseite.html) of the current berlin.de design system. +- Group various format strings (`CSV`, `.csv`, `zip:csv`) into abstract formats (`CSV`). Group formats (`CSV`, `XLSX`) into more general resource classes (`tabular data`). ## [0.1.2](https://github.com/berlinonline/ckanext-datasetsnippets/releases/tag/0.1.2) diff --git a/ckanext/datasetsnippets/helpers.py b/ckanext/datasetsnippets/helpers.py index 138919c..cd8be16 100644 --- a/ckanext/datasetsnippets/helpers.py +++ b/ckanext/datasetsnippets/helpers.py @@ -12,6 +12,7 @@ from urllib.parse import urlencode from ckanext.berlin_dataset_schema.schema import Schema +from ckanext.datasetsnippets.resource_mappings import ResourceMapping log = logging.getLogger(__name__) @@ -170,3 +171,16 @@ def description_for_facet(facet_name: str) -> str: description = definition.get('user_help_text', _("Beschreibung fehlt")) return description + +def css_class_for_format_string(format_string: str) -> str: + '''Helper function to generate a CSS class for a given resource format string.''' + mapping = ResourceMapping().format_string_mappings() + css_prefix = "dp-resource" + css_class = f"{css_prefix}-undefined" + category = mapping.get(format_string, None) + if category: + category_definition = ResourceMapping().category_mappings().get(category, None) + if category_definition: + code = category_definition.get('code', 'undefined') + css_class = f"{css_prefix}-{code}" + return css_class diff --git a/ckanext/datasetsnippets/mappings/resource_format_mappings.json b/ckanext/datasetsnippets/mappings/resource_format_mappings.json new file mode 100644 index 0000000..2d91c51 --- /dev/null +++ b/ckanext/datasetsnippets/mappings/resource_format_mappings.json @@ -0,0 +1,163 @@ +{ + "Textual Document": { + "code": "textual", + "types": { + "PDF": [ + "PDF", + ".pdf", + "zip:pdf" + ], + "DOCX": [ + "DOCX", + "DOC" + ], + "TXT": [ + "TXT" + ] + } + }, + "Tabular Data": { + "code": "tabular", + "types": { + "CSV": [ + "CSV", + ".csv", + "zip:csv" + ], + "XLSX": [ + "XLSX", + "xlsx", + "XLS", + ".xls" + ], + "ODS": [ + "ODS" + ] + } + }, + "Archive": { + "code": "archive", + "types": { + "ZIP": [ + "ZIP", + ".zip" + ] + } + }, + "Website": { + "code": "website", + "types": { + "HTML": [ + "HTML", + "Webseite", + "webseite", + "application/x-php" + ] + } + }, + "GIS Data": { + "code": "gis", + "types": { + "GEOJSON": [ + "GEOJSON-Datei", + "GeoJSON", + "gjson" + ], + "KML/KMZ": [ + "KML", + "kmz", + "zip:KMZ" + ], + "SHAPE": [ + "shape", + "zip:SHP", + "zip:shp", + "zip:dbf, zip:prj, zip:shp, zip:shx" + ], + "MapInfo TAB": [ + "zip:dat, zip:id, zip:map, zip:tab" + ], + "WFS": [ + "WFS" + ], + "WMS": [ + "WMS" + ], + "GPX": [ + "gpx" + ], + "GML": [ + "zip:gml, zip:xsd" + ] + } + }, + "Feed": { + "code": "feed", + "types": { + "Atom": [ + "Atom" + ], + "RSS": [ + "RSS" + ], + "JSON Feed": [ + "jrss" + ] + } + }, + "Tree-shaped Data": { + "code": "tree", + "types": { + "JSON": [ + "JSON" + ], + "XML": [ + "XML" + ] + } + }, + "Bitmap Image": { + "code": "bitmap", + "types": { + "TIF": [ + "JPG", + "JPEG", + "PNG", + "TIF" + ] + } + }, + "3D Data": { + "code": "3d", + "types": { + "DXF": [ + "zip:DXF", + "zip:dxf" + ] + } + }, + "Transport Data": { + "code": "transport", + "types": { + "GTFS": [ + "GTFS" + ] + } + }, + "API": { + "code": "api", + "types": { + "API": [ + "API" + ] + } + }, + "Generic Data": { + "code": "generic", + "types": { + "Protobuf": [ + "pbf" + ] + } + } +} \ No newline at end of file diff --git a/ckanext/datasetsnippets/plugin.py b/ckanext/datasetsnippets/plugin.py index 39ee535..c9162ae 100644 --- a/ckanext/datasetsnippets/plugin.py +++ b/ckanext/datasetsnippets/plugin.py @@ -1,6 +1,9 @@ """ Main module for ckanext-datasetsnippets """ + +import os + import ckan.plugins as plugins import ckan.plugins.toolkit as toolkit from ckan.plugins.toolkit import config @@ -8,6 +11,7 @@ import ckanext.datasetsnippets.helpers as theme_helpers from ckanext.datasetsnippets import snippet_blueprint import ckanext.datasetsnippets.auth as snippet_auth +from ckanext.datasetsnippets.resource_mappings import ResourceMapping class DatasetsnippetsPlugin(plugins.SingletonPlugin): plugins.implements(plugins.IConfigurer) @@ -23,6 +27,12 @@ def update_config(self, config_): toolkit.add_resource('fanstatic', 'datasetsnippets') config['datasetsnippets.path'] = "datensaetze" + path = os.path.abspath(__file__) + dir_path = os.path.dirname(path) + resource_mappings_path = os.path.join(dir_path, "mappings", "resource_format_mappings.json") + self.resource_mappings = ResourceMapping() + self.resource_mappings.load_mappings(resource_mappings_path) + # IBlueprint def get_blueprint(self): @@ -48,6 +58,7 @@ def get_helpers(self): 'berlin_label_for_sorting': theme_helpers.label_for_sorting , 'berlin_facet_plural_mapping': theme_helpers.facet_plural_mapping , 'berlin_description_for_facet': theme_helpers.description_for_facet , + 'berlin_css_class_for_format_string': theme_helpers.css_class_for_format_string , } # IAuthFunctions diff --git a/ckanext/datasetsnippets/resource_mappings.py b/ckanext/datasetsnippets/resource_mappings.py new file mode 100644 index 0000000..3aa2f15 --- /dev/null +++ b/ckanext/datasetsnippets/resource_mappings.py @@ -0,0 +1,66 @@ +# encoding: utf-8 +""" +Group various format strings (`CSV`, `.csv`, `zip:csv`) into abstract formats (`CSV`). Group formats (`CSV`, `XLSX`) into more general resource classes (`tabular data`). +""" + +import json +import logging +import ckan.plugins as plugins + +LOG = logging.getLogger(__name__) + +class ResourceMapping(plugins.SingletonPlugin): + """ + Class representing the resource mappings. + """ + + def load_mappings(self, mappings_path: str): + """ + Load the mappings. + """ + self._category_mappings = {} + try: + with open(mappings_path) as json_data: + self._category_mappings = json.load(json_data) + self._format_string_mappings = self.reverse_category_mapping(self._category_mappings) + LOG.info(self._category_mappings) + LOG.info(self._format_string_mappings) + except Exception: + raise MappingsError(f"Could not load mappings from {mappings_path}.") + + def unload_mappings(self): + """ + Unload the schema. + """ + del self._category_mappings + + def reverse_category_mapping(self, data: dict) -> dict: + reverse_mapping = {} + for category, definition in data.items(): + format_strings = [format_string for group in definition['types'].values() for format_string in group] + for format_string in format_strings: + reverse_mapping[format_string] = category + return reverse_mapping + + def category_mappings(self) -> dict: + """ + Return the loaded resource mappings (categories -> formats -> format strings). + """ + try: + return self._category_mappings + except AttributeError: + raise MappingsError("Resource mappings file not loaded yet") + + def format_string_mappings(self) -> dict: + """ + Return the loaded resource mappings (format_strings -> categories). + """ + try: + return self._format_string_mappings + except AttributeError: + raise MappingsError("Resource mappings file not loaded yet") + +class MappingsError(Exception): + """ + Errors when handling the mappings. + """ diff --git a/ckanext/datasetsnippets/templates/datasetsnippets/snippets/package_item.html b/ckanext/datasetsnippets/templates/datasetsnippets/snippets/package_item.html index 55e4fec..09d8808 100644 --- a/ckanext/datasetsnippets/templates/datasetsnippets/snippets/package_item.html +++ b/ckanext/datasetsnippets/templates/datasetsnippets/snippets/package_item.html @@ -69,6 +69,24 @@

{{ h.truncate(title, truncate_title) + + {% if package.resources %} +
+
+ {{ _('Formats') }}: +
+
+ + {% for resource in package.resources %} +
+
+ {{ resource.format | default('?', true) }} +
+
+ {% endfor %} +
+
+ {% endif %}