Skip to content

Commit

Permalink
add mini icons for resources
Browse files Browse the repository at this point in the history
  • Loading branch information
knudmoeller committed Oct 17, 2023
1 parent f721c83 commit 300be40
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
14 changes: 14 additions & 0 deletions ckanext/datasetsnippets/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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
163 changes: 163 additions & 0 deletions ckanext/datasetsnippets/mappings/resource_format_mappings.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
}
11 changes: 11 additions & 0 deletions ckanext/datasetsnippets/plugin.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
"""
Main module for ckanext-datasetsnippets
"""

import os

import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit
from ckan.plugins.toolkit import config

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)
Expand All @@ -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):
Expand All @@ -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
Expand Down
66 changes: 66 additions & 0 deletions ckanext/datasetsnippets/resource_mappings.py
Original file line number Diff line number Diff line change
@@ -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.
"""
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ <h3 class="title" style="cursor: pointer;">{{ h.truncate(title, truncate_title)
</div>
</div>
</div>

{% if package.resources %}
<div class="dp-dataset-metadatum dp-dataset-formats">
<div class="dp-dataset-metadatum__header">
{{ _('Formats') }}:
</div>
<div class="dp-resource-icon-list">

{% for resource in package.resources %}
<div class="dp-resource-icon-mini {{ h.berlin_css_class_for_format_string(resource.format) }}">
<div class="dp-resource-icon-label">
{{ resource.format | default('?', true) }}
</div>
</div>
{% endfor %}
</div>
</div>
{% endif %}
</div>
</article>
</li>
Expand Down

0 comments on commit 300be40

Please sign in to comment.