When Adding 'Page 2' as relatedItem of 'Page 1', it (Page 1) shows up in the 'relateditemes viewlet'.
If you also wanted 'Page 1' to show up as 'relatedItem' of 'Page 2' automatically (for all relatinons), what would be the best approach ( I can think of quite a few ways of doing this (customise the viewlet, customise the indexing, event handler)
def list_referers(self, context = None):
""" Return a list of backreference relationvalues
"""
catalog = getUtility(ICatalog)
intids = getUtility(IIntIds)
context = context and context or self.context
rel_query = { 'to_id' : intids.getId(aq_inner(context)) }
rel_items = list(catalog.findRelations(rel_query))
return rel_items
def get_brains_for_relation_ids(context, relationvalues, sort_key=None, direction='from', depth=0, portal_type=[], language='', keep_original_order=None):
p_catalog = getToolByName(context, 'portal_catalog')
results = []
query = {}
if portal_type:
query['Type'] = portal_type
if language:
query['Language'] = language
#log.info('%s Relationvalues ids: %s' % (get_linenumber(), [relationvalue.to_path.split('/')[-1] for relationvalue in relationvalues]))
for relationvalue in relationvalues:
r_path = direction == 'from' and relationvalue.from_path or relationvalue.to_path
for search_depth in range(0, depth+1):
query['path'] = {'query': r_path, 'depth': search_depth}
brains = p_catalog(**query)
if len(brains) > 0:
#log.info('%s brains ids: %s' % (get_linenumber(), [result.getId for result in brains]))
if keep_original_order:
for brain in brains:
if not brain in results:
results.append(brain)
else:
results = results and list(set(results + list(brains))) or list(brains)
#log.info('%s results ids: %s' % (get_linenumber(), [result.getId for result in results]))
if results:
results = sort_key and sorted(results, key=lambda match: match[sort_key]) or results
return results
<monkey:patch description="Fix circular reference when field is RelationValue"
class="plone.app.content.utils"
original="custom_json_handler"
replacement=".monkey_patches.pac_utils_custom_json_handler"
/>
def pac_utils_custom_json_handler(obj):
if obj == Missing.Value:
return None
if type(obj) in (datetime.datetime, datetime.date):
return obj.isoformat()
if type(obj) == DateTime:
dt = DateTime(obj)
return dt.ISO()
if type(obj) == set:
return list(obj)
# 20170418 fixed Circular reference
# Should this return the actual object?
if type(obj) == RelationValue:
return obj.to_path.split('/')[-1]
return obj