add an export to json view (#3358)

This commit is contained in:
Frédéric Péters 2013-08-22 16:10:28 +02:00
parent dc4586e3ca
commit 9a30d5d538
2 changed files with 52 additions and 2 deletions

View File

@ -44,6 +44,13 @@
permission="cmf.ManagePortal"
/>
<browser:view
name="json"
for="collective.dms.thesaurus.dmsthesaurus.IDmsThesaurus"
class=".utils.JsonView"
permission="cmf.ManagePortal"
/>
<plone:behavior
title="Thesaurus Keyword SearchableText indexer behavior"
description="Enables the dynamic SearchableText indexer for Thesaurus Keyword"

View File

@ -9,6 +9,10 @@ from zope.schema.interfaces import IVocabularyFactory
from zope.event import notify
from zope.lifecycleevent import ObjectAddedEvent, ObjectModifiedEvent
from plone.app.textfield.value import RichTextValue
from plone import api
from Products.CMFCore.utils import getToolByName
class ImportJson(BrowserView):
@ -30,8 +34,18 @@ class ImportJson(BrowserView):
object = getattr(self.context, term_id)
notify(ObjectAddedEvent(object))
object.title = term.get('title')
object.historical_note = term.get('historical_note')
object.scope_note = term.get('scope_note')
object.historical_note = None
if term.get('historical_note'):
object.historical_note = RichTextValue(
raw=term.get('historical_note'),
mimeType='text/html',
outputMimeType='text/x-html-safe')
object.scope_note = None
if term.get('scope_note'):
object.scope_note = RichTextValue(
raw=term.get('scope_note'),
mimeType='text/html',
outputMimeType='text/x-html-safe')
object.equivs = term.get('equivalents')
# 2nd step; add relations
@ -51,3 +65,32 @@ def get_thesaurus_object(context):
except IndexError:
return None
return thesaurus
class JsonView(BrowserView):
def __call__(self):
portal_catalog = api.portal.get_tool('portal_catalog')
folder_path = '/'.join(self.context.getPhysicalPath())
query = {'path': {'query' : folder_path},
'portal_type': 'dmskeyword',
'sort_on': 'sortable_title',
'sort_order': 'ascending'}
results = portal_catalog.searchResults(query)
keywords = {}
for brain in results:
keyword = brain.getObject()
keywords[keyword.id] = {
'title': keyword.title,
'equivalents': keyword.equivs,
'historical_note': keyword.historical_note,
'scope_note': keyword.scope_note,
'related': list(keyword.related or []),
'parents': list(keyword.broader or []),
}
if isinstance(keyword.historical_note, RichTextValue):
keywords[keyword.id]['historical_note'] = keyword.historical_note.raw
if isinstance(keyword.scope_note, RichTextValue):
keywords[keyword.id]['scope_note'] = keyword.scope_note.raw
self.request.response.setHeader('Content-type', 'application/json')
return json.dumps(keywords)