add batch import of terms, taken from a local json file

This commit is contained in:
Frédéric Péters 2013-01-24 11:33:54 +01:00
parent 67033ef14d
commit 26e2860dfe
2 changed files with 50 additions and 0 deletions

View File

@ -36,4 +36,11 @@
name="schema_policy_dmsthesaurus"
/>
<browser:view
name="import_json"
for="collective.dms.thesaurus.dmsthesaurus.IDmsThesaurus"
class=".utils.ImportJson"
permission="cmf.ManagePortal"
/>
</configure>

View File

@ -0,0 +1,43 @@
import json
from zope import component
from zope.app.intid.interfaces import IIntIds
from z3c.relationfield import RelationValue
from Products.Five.browser import BrowserView
class ImportJson(BrowserView):
def __call__(self):
self.intids = component.getUtility(IIntIds)
path = '/tmp/thesaurus.json'
# XXX: replace with a path given in the query string, or load json
# from POST data
terms = json.load(file(path))
term_intids = {}
# 1st step; create object for terms
for term_id, term in terms.items():
try:
object = getattr(self.context, term_id)
except AttributeError:
self.context.invokeFactory('dmskeyword', term_id,
title=term.get('title'))
object = getattr(self.context, term_id)
try:
term_intids[term_id] = self.intids.getId(object)
except KeyError:
self.intids.register(object)
object.title = term.get('title')
object.note = term.get('note')
object.equivs = term.get('equivalents')
term_intids[term_id] = self.intids.getId(object)
# 2nd step; add relations
for term_id, term in terms.items():
object = getattr(self.context, term_id)
object.related = [RelationValue(term_intids[x]) for x in term.get('related', [])]
object.broader = [RelationValue(term_intids[x]) for x in term.get('parents', [])]
return 'OK'