Question: Is it possible to register a single custom indexer to multiple adapters, and then, when the custom indexer is called, for it to detect which adapter caused it to be called?
Background: We built a Plone add-on with a Dexterity type with a few simple fields and then a jsonRepr field that stores all the object's information in JSON. This allows us to import any arbitrary "object" without having to create a new Dexterity type for each type of object. We'd like to be able to create custom indexes based on fields within the JSON field without having to write any new code specific to those new fields.
Scenario: Imagine that after importing some new objects, we display a list of all the JSON keys and let the (admin) user choose which need to be indexed.
I know how to use plone.indexer to create "virtual" field indexes on fields within the JSON field, like this:
@indexer(IOurDataObject)
def our_zipcode(object):
metadata = json.loads(object.jsonRepr or u'{}')
return metadata.get('zip_code', u'')
But that approach still means that I have to create a new @indexer for each "virtual" field that we need. So...
I'm imagining a solution where I create a generic custom indexer that gets mapped to any "virtual" field in the jsonRepr field that we need indexed. Something like this
@indexer(IOurDataObject)
def our_indexer(object):
metadata = json.loads(object.jsonRepr or u'{}')
#
# ??? detect which adapter called this and use that to choose the "desired_field" to pull out of the JSON
#
return metadata.get(desired_field, u'')
I just don't know how to perform that important mystery step in the middle.
Yes, I'd still need to manually put lines for each new index into my configure.zcml, but I suspect I can solve that hurdle by registering the adapters in Python code. That's a separate challenge and I think I pretty much have a handle on that part.
Finally, please feel free to tell me I'm crazy and that I should be solving this in another fashion. I'm all ears!
Thanks in advance!