-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,010 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from grokcore.component import * | ||
from grokcore.catalog.interfaces import IIndexDefinition | ||
from grokcore.catalog.index import IndexDefinition, Field, Text, Set, Value | ||
from grokcore.catalog.components import IndexesClass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# make this directory a package |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" | ||
Let's setup a site in which we manage a couple of objects: | ||
>>> herd = Herd() | ||
>>> getRootFolder()['herd'] = herd | ||
>>> from zope.site.hooks import setSite | ||
>>> setSite(herd) | ||
Now we add some indexable objects to the site: | ||
>>> herd['manfred'] = Mammoth('Manfred') | ||
>>> herd['ellie'] = Mammoth('Ellie') | ||
Then we are able to query the catalog: | ||
>>> from zope.catalog.interfaces import ICatalog | ||
>>> from zope.component import getUtility | ||
>>> catalog = getUtility(ICatalog) | ||
>>> for obj in catalog.searchResults(name=('Ellie', 'Ellie')): | ||
... print obj.name | ||
Ellie | ||
Nuke the catalog and intids in the end, so as not to confuse | ||
other tests:: | ||
>>> from zope import component | ||
>>> sm = herd.getSiteManager() | ||
>>> sm.unregisterUtility(catalog, provided=ICatalog) | ||
True | ||
>>> intids = component.getUtility(IIntIds) | ||
>>> sm.unregisterUtility(intids, provided=IIntIds) | ||
True | ||
""" | ||
|
||
import grok | ||
from zope import schema, interface | ||
from zope.intid import IntIds | ||
from zope.intid.interfaces import IIntIds | ||
from zope.catalog.catalog import Catalog | ||
from zope.catalog.interfaces import ICatalog | ||
from zope.catalog.field import FieldIndex | ||
|
||
def setup_catalog(catalog): | ||
catalog['name'] = FieldIndex('name', IMammoth) | ||
|
||
class IMammoth(interface.Interface): | ||
|
||
name = schema.TextLine() | ||
|
||
class Mammoth(grok.Model): | ||
grok.implements(IMammoth) | ||
|
||
def __init__(self, name): | ||
self.name = name | ||
|
||
class Herd(grok.Container, grok.Site): | ||
grok.local_utility(IntIds, provides=IIntIds) | ||
grok.local_utility(Catalog, provides=ICatalog, setup=setup_catalog) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
""" | ||
Grok allows you to set up catalog indexes in your application with a | ||
special indexes declaration. | ||
Let's set up a site in which we manage a couple of objects:: | ||
>>> herd = Herd() | ||
>>> getRootFolder()['herd'] = herd | ||
>>> from zope.site.hooks import setSite | ||
>>> setSite(herd) | ||
Now we add some indexable objects to the site:: | ||
>>> herd['alpha'] = Mammoth('Alpha', 13, 'Hello world!') | ||
>>> herd['beta'] = Mammoth('Beta', 14, 'Bye World!') | ||
We are able to query the catalog:: | ||
>>> from zope.catalog.interfaces import ICatalog | ||
>>> from zope.component import getUtility, queryUtility | ||
>>> catalog = getUtility(ICatalog) | ||
>>> for obj in catalog.searchResults(name=('Beta', 'Beta')): | ||
... print obj.name | ||
Beta | ||
Let's query the text index, which incidentally also indexes a method:: | ||
>>> def sortedResults(catalog, **kw): | ||
... result = list(catalog.searchResults(**kw)) | ||
... result.sort(key=lambda x:x.name) | ||
... return [item.name for item in result] | ||
>>> sortedResults(catalog, message='world') | ||
['Alpha', 'Beta'] | ||
>>> sortedResults(catalog, message='hello') | ||
['Alpha'] | ||
>>> sortedResults(catalog, message='bye') | ||
['Beta'] | ||
Note that another application that we did not register the | ||
indexes for won't have a catalog available:: | ||
>>> herd2 = Herd2() | ||
>>> getRootFolder()['herd2'] = herd2 | ||
>>> setSite(herd2) | ||
>>> queryUtility(ICatalog, default=None) is None | ||
True | ||
>>> setSite(herd) | ||
Nuke the catalog and intids in the end, so as not to confuse | ||
other tests:: | ||
>>> sm = herd.getSiteManager() | ||
>>> from zope.catalog.interfaces import ICatalog | ||
>>> sm.unregisterUtility(catalog, provided=ICatalog) | ||
True | ||
>>> from zope.intid.interfaces import IIntIds | ||
>>> from zope import component | ||
>>> intids = component.getUtility(IIntIds) | ||
>>> sm.unregisterUtility(intids, provided=IIntIds) | ||
True | ||
Unfortunately ftests don't have good isolation from each other yet. | ||
""" | ||
|
||
import grokcore.catalog | ||
import grokcore.site | ||
from grokcore.content import Container, Application | ||
from zope.interface import Interface, Attribute, implements | ||
|
||
|
||
class Herd(Container, Application): | ||
pass | ||
|
||
|
||
class Herd2(Container, Application): | ||
pass | ||
|
||
|
||
class IMammoth(Interface): | ||
age = Attribute('Age') | ||
name = Attribute('Name') | ||
|
||
def message(): | ||
"""Message the mammoth has for the world. | ||
""" | ||
|
||
|
||
class MammothIndexes(grokcore.catalog.Indexes): | ||
grokcore.catalog.site(Herd) | ||
grokcore.catalog.context(IMammoth) | ||
|
||
name = grokcore.catalog.Field() | ||
age = grokcore.catalog.Field() | ||
message = grokcore.catalog.Text() | ||
|
||
|
||
class Mammoth(Model): | ||
implements(IMammoth) | ||
|
||
def __init__(self, name, age, message): | ||
self.age = age | ||
self.name = name | ||
self._message = message | ||
|
||
def message(self): | ||
return self._message |
91 changes: 91 additions & 0 deletions
91
src/grokcore/catalog/ftests/catalog/indexes_app_interface.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
""" | ||
Grok allows you to set up catalog indexes in your application with a | ||
special indexes declaration. Here we see how we can register indexes for | ||
an interface instead of an application directly. | ||
Let's set up a site in which we manage a couple of objects:: | ||
>>> herd = Herd() | ||
>>> getRootFolder()['herd'] = herd | ||
>>> from zope.site.hooks import setSite | ||
>>> setSite(herd) | ||
We are able to find the catalog:: | ||
>>> from zope.catalog.interfaces import ICatalog | ||
>>> from zope.component import getUtility | ||
>>> catalog = getUtility(ICatalog) | ||
>>> catalog is not None | ||
True | ||
>>> catalog.get('name') is not None | ||
True | ||
Nuke the catalog and intids for this site, so as not to confuse | ||
other tests:: | ||
>>> sm = herd.getSiteManager() | ||
>>> from zope.catalog.interfaces import ICatalog | ||
>>> sm.unregisterUtility(catalog, provided=ICatalog) | ||
True | ||
>>> from zope.intid.interfaces import IIntIds | ||
>>> from zope import component | ||
>>> intids = component.getUtility(IIntIds) | ||
>>> sm.unregisterUtility(intids, provided=IIntIds) | ||
True | ||
Now let's create another application providing the same interface:: | ||
>>> herd2 = Herd2() | ||
>>> getRootFolder()['herd2'] = herd2 | ||
>>> setSite(herd2) | ||
>>> catalog = getUtility(ICatalog) | ||
>>> catalog is not None | ||
True | ||
>>> catalog.get('name') is not None | ||
True | ||
Nuke the catalog and intids in the end, so as not to confuse | ||
other tests:: | ||
>>> sm = herd2.getSiteManager() | ||
>>> sm.unregisterUtility(catalog, provided=ICatalog) | ||
True | ||
>>> intids = component.getUtility(IIntIds) | ||
>>> sm.unregisterUtility(intids, provided=IIntIds) | ||
True | ||
""" | ||
|
||
import grokcore.catalog | ||
from grokcore.content import Container, Application | ||
from zope.interface import Interface, implements | ||
from zope import schema | ||
|
||
|
||
class IHerd(Interface): | ||
pass | ||
|
||
|
||
class Herd(Container, Application): | ||
implements(IHerd) | ||
|
||
|
||
class Herd2(Container, Application): | ||
implements(IHerd) | ||
|
||
|
||
class IMammoth(Interface): | ||
name = Attribute("") | ||
age = Attribute("") | ||
|
||
def message(): | ||
"""Message the mammoth has for the world. | ||
""" | ||
|
||
|
||
class MammothIndexes(grok.Indexes): | ||
grok.site(IHerd) | ||
grok.context(IMammoth) | ||
|
||
name = index.Field() | ||
age = index.Field() | ||
message = index.Text() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
""" | ||
Grok allows you to set up catalog indexes in your application with a | ||
special indexes declaration. If you want to name the index differently | ||
from the attribute, you can do so, by passing an explicit `attribute` | ||
keyword argument to the field. | ||
Let's set up a site in which we manage a couple of objects:: | ||
>>> herd = Herd() | ||
>>> getRootFolder()['herd'] = herd | ||
>>> from zope.site.hooks import setSite | ||
>>> setSite(herd) | ||
Now we add some indexable objects to the site:: | ||
>>> herd['alpha'] = Mammoth('Alpha', 13) | ||
>>> herd['beta'] = Mammoth('Beta', 14) | ||
We are able to query the catalog:: | ||
>>> from zope.catalog.interfaces import ICatalog | ||
>>> from zope.component import getUtility, queryUtility | ||
>>> catalog = getUtility(ICatalog) | ||
>>> for obj in catalog.searchResults(how_old=(13, 13)): | ||
... print obj.name | ||
Alpha | ||
Nuke the catalog and intids in the end, so as not to confuse | ||
other tests:: | ||
>>> sm = herd.getSiteManager() | ||
>>> from zope.catalog.interfaces import ICatalog | ||
>>> sm.unregisterUtility(catalog, provided=ICatalog) | ||
True | ||
>>> from zope.intid.interfaces import IIntIds | ||
>>> from zope import component | ||
>>> intids = component.getUtility(IIntIds) | ||
>>> sm.unregisterUtility(intids, provided=IIntIds) | ||
True | ||
""" | ||
|
||
from zope.interface import Interface | ||
from zope import schema | ||
|
||
import grok | ||
from grok import index | ||
|
||
class Herd(grok.Container, grok.Application): | ||
pass | ||
|
||
class IMammoth(Interface): | ||
name = schema.TextLine(title=u'Name') | ||
age = schema.Int(title=u'Age') | ||
|
||
class MammothIndexes(grok.Indexes): | ||
grok.site(Herd) | ||
grok.context(IMammoth) | ||
|
||
name = index.Field() | ||
how_old = index.Field(attribute='age') | ||
|
||
class Mammoth(grok.Model): | ||
grok.implements(IMammoth) | ||
|
||
def __init__(self, name, age): | ||
self.name = name | ||
self.age = age |
Oops, something went wrong.