Skip to content

Commit

Permalink
Added generic indexer registration
Browse files Browse the repository at this point in the history
Fields dct_title and dct_description will be indexed for any DCAT-CT
Similiarity check for foaf:Agent is active for harvesting
Renaming of some files to be more consistent
  • Loading branch information
volkerjaenisch committed Feb 9, 2018
1 parent 1ec67b7 commit d9b15b0
Show file tree
Hide file tree
Showing 34 changed files with 309 additions and 117 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
79 changes: 79 additions & 0 deletions src/pkan/dcatapde/api/foaf_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# -*- coding: utf-8 -*-
"""Work with FOAFAgents."""

from pkan.dcatapde.constants import CT_FOAF_AGENT
from pkan.dcatapde.content.foaf_agent import FOAFAgent
from pkan.dcatapde.content.foaf_agent import IFOAFAgent
from plone import api
from zope.schema import getValidationErrors


# Data Cleaning Methods
def clean_foafagent(**data):
"""Clean foafagent."""
test_obj = FOAFAgent()

# test object must have an id
test_obj.id = 'test'
test_obj.title = 'test'

for attr in data:
setattr(test_obj, attr, data[attr])

errors = getValidationErrors(IFOAFAgent, test_obj)

return data, errors


def find_foaf_agent(data):
"""Find a given on a search pattern equivalent to an create dataset"""
catalog = api.portal.get_tool('portal_catalog')

search_data = data.copy()
# strip the data of legacy fields like title and description
for key in ['title', 'description']:
try:
del search_data[key]
except KeyError:
pass

# add the portal type
search_data['portal_type'] = FOAFAgent.portal_type

results = catalog.searchResults(**search_data)

if len(results) > 1:
result = results[0]
elif len(results) == 0:
result = None
else:
result = results

return result


# Add Methods
def add_foafagent(context, **data):
"""Add a new foafagent."""
context = get_foafagent_context()

data, errors = clean_foafagent(**data)

# no such agent exits create a new one
agent = api.content.create(container=context, type=CT_FOAF_AGENT, **data)

return agent


# Get Methods
def get_foafagent_context():
"""Get content for an foafagent."""
# fix: context should not be the site
context = api.portal.get()
return context


# Delete Methods


# Related Methods
49 changes: 0 additions & 49 deletions src/pkan/dcatapde/api/foafagent.py

This file was deleted.

2 changes: 2 additions & 0 deletions src/pkan/dcatapde/browser/rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def __call__(self):
(self.context, target),
interface=IMarshallSource,
)
if not marshaller:
return
marshaller.marshall()

self.request.response.setHeader(
Expand Down
8 changes: 8 additions & 0 deletions src/pkan/dcatapde/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@
title="pkan.dcatapde (uninstall)"
/>

<genericsetup:importStep
name="pkan.dcatapde.catalog"
handler="pkan.dcatapde.setuphandlers.catalog_setup"
title="PKAN Catalog Setup"
description="Setup Indexes for the CTs in the Catalog">
<depends name="plone.app.registry" />
</genericsetup:importStep>

<utility
factory=".setuphandlers.HiddenProfiles"
name="pkan.dcatapde-hiddenprofiles"
Expand Down
1 change: 1 addition & 0 deletions src/pkan/dcatapde/content/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class DCATMixin(object):

_namespace = None
_ns_class = None
_index_fields = ['dct_title', 'dct_description']

@property
def namespace_class(self):
Expand Down
7 changes: 5 additions & 2 deletions src/pkan/dcatapde/content/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@
/>


<!-- FOAFagent -->
<!-- FOAFAgent -->
<!-- ========= -->
<adapter
factory=".base.NameFromDCTTitle"
for=".foaf_agent.IFOAFagent"
for=".foaf_agent.IFOAFAgent"
provides=".base.INameFromDCTTitle"
/>

Expand All @@ -117,6 +117,9 @@
provides="plone.dexterity.interfaces.IDexterityFactory"
/>

<adapter
factory=".foaf_agent.dct_title" name="dct_title"
/>

<!-- SKOSConcept -->
<!-- =========== -->
Expand Down
4 changes: 3 additions & 1 deletion src/pkan/dcatapde/content/dcat_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ class IDCATCatalog(model.Schema):
class DCATCatalog(Container, DCATMixin):
"""DCATCatalog Content Type."""

portal_type = constants.CT_DCAT_CATALOG
content_schema = IDCATCatalog
_namespace = 'dcat'
_ns_class = 'catalog'

Expand All @@ -200,7 +202,7 @@ def __init__(self):
self.portal_type = constants.CT_DCAT_CATALOG

def __call__(self, *args, **kw):
from pkan.dcatapde.api.catalog import clean_catalog
from pkan.dcatapde.api.dcat_catalog import clean_catalog

data, errors = clean_catalog(**kw)

Expand Down
4 changes: 3 additions & 1 deletion src/pkan/dcatapde/content/dcat_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ class IDCATDataset(model.Schema):
class DCATDataset(Container, DCATMixin):
"""DCATDataset Content Type."""

portal_type = constants.CT_DCAT_DATASET
content_schema = IDCATDataset
_namespace = 'dcat'
_ns_class = 'dataset'

Expand Down Expand Up @@ -234,7 +236,7 @@ def __init__(self):

def __call__(self, *args, **kw):
# Fix: get context and maybe change it
from pkan.dcatapde.api.dataset import clean_dataset
from pkan.dcatapde.api.dcat_dataset import clean_dataset
data, errors = clean_dataset(**kw)

return super(
Expand Down
4 changes: 3 additions & 1 deletion src/pkan/dcatapde/content/dcat_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ class IDCATDistribution(model.Schema):
class DCATDistribution(Container, DCATMixin):
"""DCATDistribution Content Type."""

portal_type = constants.CT_DCAT_DISTRIBUTION
content_schema = IDCATDistribution
_namespace = 'dcat'
_ns_class = 'distribution'

Expand Down Expand Up @@ -156,7 +158,7 @@ def __init__(self):

def __call__(self, *args, **kw):
# Fix: get context and maybe change it
from pkan.dcatapde.api.distribution import clean_distribution
from pkan.dcatapde.api.dcat_distribution import clean_distribution
data, errors = clean_distribution(**kw)

return super(
Expand Down
1 change: 1 addition & 0 deletions src/pkan/dcatapde/content/dct_licensedocument.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class DCTLicenseDocument(Item, DCATMixin):
"""DCTLicenseDocument Content Type."""

portal_type = constants.CT_DCT_LICENSEDOCUMENT
content_schema = IDCTLicenseDocument
_namespace = 'dct'
_ns_class = 'licensedocument'

Expand Down
1 change: 1 addition & 0 deletions src/pkan/dcatapde/content/dct_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class DCTLocation(Item, DCATMixin):
"""DCTLocation Content Type."""

portal_type = constants.CT_DCT_LOCATION
content_schema = IDCTLocation
_namespace = 'dct'
_ns_class = 'location'

Expand Down
1 change: 1 addition & 0 deletions src/pkan/dcatapde/content/dct_mediatypeorextent.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class DCTMediaTypeOrExtent(Item, DCATMixin):
"""DCTMediatypeorextent Content Type."""

portal_type = constants.CT_DCT_MEDIATYPEOREXTENT
content_schema = IDCTMediaTypeOrExtent
_namespace = 'dct'
_ns_class = 'mediatypeorextent'

Expand Down
1 change: 1 addition & 0 deletions src/pkan/dcatapde/content/dct_standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class DCTStandard(Item, DCATMixin):
"""DCTStandard Content Type."""

portal_type = constants.CT_DCT_STANDARD
content_schema = IDCTStandard
_namespace = 'dct'
_ns_class = 'standard'

Expand Down
37 changes: 30 additions & 7 deletions src/pkan/dcatapde/content/foaf_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
from pkan.dcatapde import constants
from pkan.dcatapde import i18n
from pkan.dcatapde.content.base import DCATMixin
from pkan.dcatapde.content.util import I18NField2Unique
from plone.dexterity.content import Item
from plone.dexterity.factory import DexterityFactory
from plone.indexer import indexer
from plone.supermodel import model
from ps.zope.i18nfield.field import I18NText
from ps.zope.i18nfield.field import I18NTextLine
from ps.zope.i18nfield.fieldproperty import I18NTextProperty
from zope.interface import implementer


class IFOAFagent(model.Schema):
"""Marker interface and Dexterity Python Schema for FOAFagent."""
class IFOAFAgent(model.Schema):
"""Marker interface and Dexterity Python Schema for FOAFAgent."""

# Mandatory
# -------------------------------------------------------------------------
Expand All @@ -29,16 +31,17 @@ class IFOAFagent(model.Schema):
)


@implementer(IFOAFagent)
class FOAFagent(Item, DCATMixin):
@implementer(IFOAFAgent)
class FOAFAgent(Item, DCATMixin):
"""FOAFAgent Content Type."""

portal_type = constants.CT_FOAF_AGENT
content_schema = IFOAFAgent
_namespace = 'foaf'
_ns_class = 'agent'

dct_title = I18NTextProperty(IFOAFagent['dct_title'])
dct_description = I18NTextProperty(IFOAFagent['dct_description'])
dct_title = I18NTextProperty(IFOAFAgent['dct_title'])
dct_description = I18NTextProperty(IFOAFAgent['dct_description'])

def Title(self):
return unicode(self.dct_title)
Expand All @@ -55,10 +58,30 @@ def __init__(self):

def __call__(self, *args, **kw):
# Fix: get context and maybe change it
from pkan.dcatapde.api.foafagent import clean_foafagent
from pkan.dcatapde.api.foaf_agent import clean_foafagent
data, errors = clean_foafagent(**kw)

return super(
FOAFAgentDefaultFactory,
self,
).__call__(*args, **data)


@indexer(IFOAFAgent)
def dct_title(obj):
result = I18NField2Unique(obj.dct_title)

if result is None:
return ''

return result


@indexer(IFOAFAgent)
def dct_description(obj):
result = I18NField2Unique(obj.dct_description)

if result is None:
return ''

return result
1 change: 1 addition & 0 deletions src/pkan/dcatapde/content/skos_concept.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class SKOSConcept(Item, DCATMixin):
"""SKOSConcept Content Type."""

portal_type = constants.CT_SKOS_CONCEPT
content_schema = ISKOSConcept
_namespace = 'skos'
_ns_class = 'concept'

Expand Down
13 changes: 13 additions & 0 deletions src/pkan/dcatapde/content/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*-
"""Utils"""


def I18NField2Unique(obj):
"""Expresses an i18Nfield as unique as possible as a string"""
langs = obj.keys()
langs.sort()
result = []
for lang in langs:
result.append(lang + ':' + obj[lang])

return ' '.join(result)
Loading

0 comments on commit d9b15b0

Please sign in to comment.