Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
import grok
from zope import schema
from zope.interface import Interface, implements
class IMammoth(Interface):
name = schema.TextLine(title=u"Name")
class Mammoth(grok.Model):
implements(IMammoth)
name = u'Manfred'
class Index(grok.View):
def render(self):
return "%s, the Mammoth reports: %s" % (self.context.name,
self.context.report)
class Edit(grok.EditForm):
def update(self):
self.context.report = ("The form's update() was called and my name "
"was %s." % self.context.name)
class EditRedirect(grok.EditForm):
def update(self):
# redirect upon form submit so that no changes are ever saved
if 'form.name' in self.request:
pass
@grok.implementer(IMarker)
class Corridors(grok.Container):
pass
class IMammothSkin(IDefaultBrowserLayer):
grok.skin('mammothskin')
class Third(grok.View):
grok.context(IMarker)
def render(self):
return self.application_url('third', skin=IMammothSkin)
class Fourth(grok.View):
grok.context(IMarker)
def render(self):
return self.application_url(
'fourth', skin=IMammothSkin, data={'key': 'value'})
class PercentPageTemplateFileFactory(grok.GlobalUtility):
"""Glue class suggested by doc/minitutorials/template-languages.txt."""
grok.implements(grok.interfaces.ITemplateFileFactory)
grok.name('pct')
def __call__(self, filename, _prefix=None):
return PercentPageTemplate(filename=filename, _prefix=_prefix)
class Bear(grok.Model):
def __init__(self, name):
self.name = name
class Index(grok.View):
def namespace(self):
return { 'bear_name': self.context.name }
index = PercentPageTemplate(filename='language_file.txt',
_prefix=os.path.dirname(__file__))
>>> browser.handleErrors = False
>>> browser.open('http://localhost/manfred')
>>> browser.url
'http://localhost/manfred/another'
"""
import grok
class Mammoth(grok.Model):
pass
class Index(grok.View):
def render(self):
self.redirect(self.url('another'))
class Another(grok.View):
def render(self):
return "Another view"
class Zoo(grok.Container):
pass
class IMammoth(Interface):
name = schema.TextLine(title=u"Name")
size = schema.TextLine(title=u"Size", default=u"Quite normal")
class Mammoth(grok.Model):
implements(IMammoth)
def __init__(self, name='', size=''):
self.name = name
self.size = size
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)
class AddMammoth(grok.AddForm):
grok.context(Zoo)
form_fields = grok.AutoFields(Mammoth)
@grok.action('Add entry')
def add(self, **data):
# pass data into Mammoth constructor
self.context['manfred'] = manfred = Mammoth(**data)
self.redirect(self.url(manfred))
>>> print browser.contents
<h1>Hello, world!</h1>
<span>Blue</span>
<span>Blue</span>
"""
import grok
class Mammoth(grok.Model):
teeth = u"Blue"
class Index(grok.View):
pass
index = grok.PageTemplate("""\
self.name = name
def getMammoth(self, name):
return Mammoth(name)
def traverse(self, name):
return self.getMammoth(name)
class Mammoth(grok.Model):
def __init__(self, name):
self.name = name
grok.context(Mammoth)
class Index(grok.View):
pass
index = grok.PageTemplate("""\
Traceback (most recent call last):
urllib.error.HTTPError: HTTP Error 500: Internal Server Error
>>> browser.contents
'It is gone!'
"""
import grok
from zope.interface import Interface
class CaveWasRobbedError(Exception):
pass
class Cave(grok.View):
"""Home of Grok.
"""
grok.context(Interface)
fire = 'It is gone!'
def render(self):
raise CaveWasRobbedError("EVERYTHING GONE! GROK ANGRY!")
class CaveErrorView(grok.View):
"""Default view for the CaveWasRobbedError.
"""
grok.name("index")
grok.context(CaveWasRobbedError)
grok.context(zope.interface.Interface)
grok.require(EditPermission)
def render(self):
return 'Let\'s make it even prettier.'
class EraseCavePainting(grok.View):
grok.context(zope.interface.Interface)
grok.require(ErasePermission)
def render(self):
return 'Oops, mistake, let\'s erase it.'
class ApproveCavePainting(grok.View):
grok.context(zope.interface.Interface)
grok.require(ApprovePermission)
def render(self):
return 'Painting owners cannot approve their paintings.'
import grok
class Sample(grok.Application, grok.Container):
pass
class Index(grok.View):
pass