Problem with plone.indexer and Dexterity

雨燕双飞 提交于 2019-12-22 04:42:08

问题


I wish to enable a special index, called Sectors, for a attribute ('sectors') of my Dexterity based custom content-type.

In my schema, inside types/mycontent.py I have:

class IMyContent(form.Schema):
    """
    My Content
    """
    sectors = schema.Set(
            title=_(u"Sectors"),
            description=_(u"Select some sectors"),
            value_type=schema.Choice(vocabulary=vocs.sectors),
            required=True,
        )

    (...)

I then define the index in this way, inside indexers.py

from plone.indexer.decorator import indexer
from zr.content.types.mycontent import IMyContent

@indexer(IMyContent)
def Sectors(obj):
    """Indexer for Sectors attribute.
    """
    d = getattr(obj, "sectors", u"")
    return d if d else None

Finally in the root package configure.zcml:

<adapter name="Sectors" factory=".indexers.Sectors"/>

However, it does not seem to work. Even after reinstalling the product, I don't see the index in portal_catalog and catalog brain object do not seem to have it either.

What am I doing wrong?


回答1:


You aren't defining the catalogue index. This will just make the indexer available to be added. You require a catalog.xml in your GenericSetup profile with:

<?xml version="1.0"?>
<object name="portal_catalog" meta_type="Plone Catalog Tool">
 <index name="Sectors" meta_type="KeywordIndex">
  <indexed_attr value="Sectors"/>
 </index>
</object>



回答2:


The accepted solution may be slightly obscure, so here are a couple of clarifications:

1) Do NOT edit your global generic setup.

Unless you're doing something extremely weird, you'll have setup your site as a series of plone extensions, and have a folder structure like:

app.plugin/
app.plugin/app/
app.plugin/app/configure.zcml
app.plugin/app/profiles/
app.plugin/app/profiles/default
app.plugin/app/profiles/default/types
app.plugin/app/profiles/default/types/Folder.xml
app.plugin/app/profiles/default/types/app.mydexteritytype.xml
app.plugin/app/profiles/default/types.xml
app.plugin/app/profiles/default/portlets.xml
app.plugin/app/profiles/default/catalog.xml <---- ADD THIS

2) You don't have to have an xml block (as per the accepted solution) in catalog.xml, you can just create the index from the frontend ZMI. However, if you do this, it'll get blown away the next time you install your plugins. So you probably do want to.

3) After installing your catalog.xml, browse to the ZMI interface to portal_catalog and check that under the 'indexes' tab your index exists. If it doesn't you've messed up.

4) To build the index, you need to go the 'advanced' tab and choose rebuild.

5) The indexer greedily consumes exceptions and does not raise them (especially important for AttributeError; you may not index some values you want to be indexing), so if you want to make sure your indexer is actually running, try adding a log or print statement in it:

@indexer(IMyDexterityType)
def dummy_indexer(obj, **kw):
    try:
        print('indexed: %r' % obj)
        return obj.title
    except Exception as e:
        print('index fail: %r' % e)
    return ''

If nothing else you should see some output like:

2013-08-12 16:42:28 INFO GenericSetup.archetypetool Archetype tool imported.
2013-08-12 16:42:28 INFO GenericSetup.resourceregistry Stylesheet registry imported.
2013-08-12 16:42:28 INFO GenericSetup.resourceregistry Javascript registry imported.
indexed: <MyDexterityType at /Plone/test/cat-document-0>
indexed: <MyDexterityType at /Plone/test/hello>

6) grok.global_adapter() as mentioned in some of the documentation (http://developer.plone.org/reference_manuals/external/plone.app.dexterity/advanced/catalog-indexing-strategies.html?highlight=custom%20indexing#creating-custom-indexers) is about registering virtual properties, and does not mitigate the need to setup your catalog.xml.

Finally, someone's put a working example up on github here, which is extremely useful:

https://github.com/aclark4life/event_days_indexer



来源:https://stackoverflow.com/questions/6651174/problem-with-plone-indexer-and-dexterity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!