Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
>>> root = getRootFolder()
>>> root['app'] = App()
Now we can look at the view::
>>> from zope.testbrowser.wsgi import Browser
>>> browser = Browser()
>>> browser.handleErrors = False
>>> browser.open('http://localhost/app/@@index')
>>> print(browser.contents)
Hello world
"""
import grok
class App(grok.Application, grok.Container):
pass
class Index(grok.View):
def render(self):
return "Hello world"
>>> from zope.app.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.
"""
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')
def message():
"""Message the mammoth has for the world."""
class IMammoth2(Interface):
name2 = schema.TextLine(title=u'Name')
age2 = schema.Int(title=u'Age')
def message2():
"""Message the mammoth has for the world."""
class MammothIndexes(grok.Indexes):
grok.site(Herd)
>>> nonsitecontainer['manfred'] = manfred
>>> ISite.providedBy(manfred)
True
>>> nonsitecontainer['herd'] = herd
>>> ISite.providedBy(herd)
True
"""
import grok
class Mammoth(grok.Model, grok.Site):
pass
class Herd(grok.Container, grok.Site):
pass
class NonSite(grok.Model):
pass
class NonSiteContainer(grok.Container):
pass
>>> sm.unregisterUtility(catalog, provided=ICatalog)
True
>>> from zope.app.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.Site):
pass
class IMammoth(Interface):
name = schema.TextLine(title=u'Name')
age = schema.Int(title=u'Age')
def message():
"""Message the mammoth has for the world."""
class MammothIndexes(grok.Indexes):
grok.site(Herd)
grok.context(IMammoth)
name = index.Field()
age = index.Field()
message = index.Text()
<h1>Hello, Ellie!</h1>
Also try traversing (an empty and therefore False in a Boolean sense) container
as a subitem of a container:
>>> herd['subherd'] = Herd()
>>> browser.open("http://localhost/herd/subherd/special")
>>> print(browser.contents)
special view
"""
import grok
class Herd(grok.Container):
pass
class Traverser(grok.Traverser):
grok.context(Herd)
def traverse(self, name):
if name == 'special':
return Special()
return None
class Mammoth(grok.Model):
def __init__(self, name):
self.name = name
class Special(grok.Model):
pass
>>> print browser.contents
We found Ellie!
"""
import grok
from zope import schema, interface, component
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 Zoo(grok.Site, grok.Container):
grok.local_utility(IntIds, provides=IIntIds)
grok.local_utility(Catalog, provides=ICatalog, setup=setup_catalog)
class IMammoth(interface.Interface):
name = schema.TextLine(title=u"Name")
size = schema.TextLine(title=u"Size")
class Mammoth(grok.Model):
grok.implements(IMammoth)
class Index(grok.View):
grok.context(Mammoth)
def render(self):
return 'Hi, my name is %s, and I\'m "%s"' % (self.context.name,
self.context.size)
from zope.interface import Interface
from zope import schema
import grok
from grok import index
class IHerd(Interface):
pass
class Herd(grok.Container, grok.Application):
grok.implements(IHerd)
class Herd2(grok.Container, grok.Application):
grok.implements(IHerd)
class IMammoth(Interface):
name = schema.TextLine(title=u'Name')
age = schema.Int(title=u'Age')
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()