From ae9cda74c2d759b3ee446395b9fb27f25512f7dc Mon Sep 17 00:00:00 2001 From: Michail Alexakis Date: Sun, 7 Jun 2015 20:00:17 -0400 Subject: [PATCH] Deduce tags from INSPIRE keywords when importing a dataset. This addresses #135 (partially, covers only the import case). --- .../lib/metadata/schemata/thesaurus.py | 4 +++ .../lib/metadata/types/inspire_metadata.py | 27 +++++++++++++++++-- .../lib/metadata/types/thesaurus.py | 10 ++++++- 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/ckanext/publicamundi/lib/metadata/schemata/thesaurus.py b/ckanext/publicamundi/lib/metadata/schemata/thesaurus.py index 8155b0e..216d025 100644 --- a/ckanext/publicamundi/lib/metadata/schemata/thesaurus.py +++ b/ckanext/publicamundi/lib/metadata/schemata/thesaurus.py @@ -54,6 +54,10 @@ class IThesaurusTerms(IObject): min_length = 1, max_length = 8) + def iter_terms(): + '''Provide an iterator on terms. + ''' + @zope.interface.invariant def check_terms(obj): unexpected_terms = [] diff --git a/ckanext/publicamundi/lib/metadata/types/inspire_metadata.py b/ckanext/publicamundi/lib/metadata/types/inspire_metadata.py index 575052e..82610f2 100644 --- a/ckanext/publicamundi/lib/metadata/types/inspire_metadata.py +++ b/ckanext/publicamundi/lib/metadata/types/inspire_metadata.py @@ -88,10 +88,13 @@ class InspireMetadata(BaseMetadata): responsible_party = list def deduce_basic_fields(self): + '''Deduce basic (i.e. core CKAN) fields from self. + ''' + data = super(InspireMetadata, self).deduce_basic_fields() - data['notes'] = self.abstract - + # id (applicable only for imported/harvested) + identifier = None try: identifier = uuid.UUID(self.identifier) @@ -100,6 +103,26 @@ def deduce_basic_fields(self): else: data['id'] = str(identifier) + # notes + + data['notes'] = self.abstract + + # Todo version + + # tags: Collect free keywords and thesauri terms (see #135) + + tags = [] + + for kw in self.free_keywords: + tags.append(dict(name=kw.value, display_name=kw.value)) + + for thes_name, thes_terms in self.keywords.items(): + for term in thes_terms.iter_terms(): + tags.append(dict(name=term.value, display_name=term.title)) + + if tags: + data['tags'] = tags + return data # XML serialization diff --git a/ckanext/publicamundi/lib/metadata/types/thesaurus.py b/ckanext/publicamundi/lib/metadata/types/thesaurus.py index eeef5ed..3a90e1c 100644 --- a/ckanext/publicamundi/lib/metadata/types/thesaurus.py +++ b/ckanext/publicamundi/lib/metadata/types/thesaurus.py @@ -62,8 +62,16 @@ class ThesaurusTerms(Object): zope.interface.implements(IThesaurusTerms) - # Fixme: Maybe point here to a factory for named Thesaurus objects thesaurus = Thesaurus terms = list + def iter_terms(self): + vocabulary = self.thesaurus.vocabulary.by_value + for t in self.terms: + yield vocabulary.get(t) + + __iter__ = iter_terms + + +