Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
class LadyViewlet(grok.Viewlet):
grok.context(Interface)
grok.viewletmanager(Pot)
grok.view(FireView)
def render(self):
return 'Lady Viewlet'
class NamedViewletManager(grok.ViewletManager):
grok.context(Interface)
grok.name('managerwithname')
class NamedViewlet(grok.Viewlet):
grok.context(Interface)
grok.name('viewletwithname')
grok.viewletmanager(NamedViewletManager)
def render(self):
return "NamedViewlet"
pass
class Ellie(grok.View):
grok.context(Herd)
grok.name('ellie')
def render(self):
return "Hi, it's me, the Ellie view!"
class Mammoth(grok.Model):
def __init__(self, name):
self.name = name
class MammothIndex(grok.View):
grok.context(Mammoth)
grok.name('index')
def render(self):
return "Hello " + self.context.name.title()
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
class SpecialIndex(grok.View):
grok.context(Special)
grok.name('index')
def render(self):
return "special view"
grok.context(Mammoth)
class Index(grok.View):
pass
index = grok.PageTemplate("""\
def render(self):
return "Gold"
class FredViewlet(grok.Viewlet):
grok.order(20)
grok.viewletmanager(CaveManager)
def render(self):
return "Fred"
class OrderView2(grok.View):
grok.template('orderview')
class CaveManager2(grok.ViewletManager):
grok.view(OrderView2)
grok.name('cave')
def sort(self, viewlets):
# Alphabetical-by-name, reversed.
return sorted(viewlets, reverse=True)
class NoExplicitOrderCaveViewlet(grok.Viewlet):
grok.name('cave')
grok.viewletmanager(CaveManager2)
def render(self):
return "Cave"
class NoExplicitOrderBarneyViewlet(grok.Viewlet):
grok.name('barney')
grok.viewletmanager(CaveManager2)
Let's make it even prettier.
>>> browser.open("http://localhost/@@erasecavepainting")
>>> print(browser.contents)
Oops, mistake, let's erase it.
>>> browser.open("http://localhost/@@approvecavepainting")
Traceback (most recent call last):
urllib.error.HTTPError: HTTP Error 401: Unauthorized
"""
import grok
import zope.interface
class ViewPermission(grok.Permission):
grok.name('paint.ViewPainting')
class EditPermission(grok.Permission):
grok.name('paint.EditPainting')
class ErasePermission(grok.Permission):
grok.name('paint.ErasePainting')
class ApprovePermission(grok.Permission):
grok.name('paint.ApprovePainting')
class PaintingOwner(grok.Role):
grok.name('paint.PaintingOwner')
grok.title('Painting Owner')
grok.permissions(
'paint.ViewPainting', 'paint.EditPainting', 'paint.ErasePainting')
class Blog(grok.Container, grok.Site):
class fields:
title = schema.TextLine(title=u'Title', default=u'')
tagline = schema.TextLine(title=u'Tagline', default=u'')
def __init__(self):
super(Blog, self).__init__()
self['entries'] = Entries()
class Entries(grok.Container):
pass
class BlogIndex(grok.View):
grok.context(Blog)
grok.name('index')
def entries(self):
return lastEntries(10)
class BlogEdit(grok.EditForm):
grok.context(Blog)
grok.name('edit')
@grok.action('Save changes')
def edit(self, **data):
self.applyChanges(**data)
self.redirect(self.url(self.context))
class EntriesIndex(grok.View):
grok.context(Entries)
grok.name('index')
class MethodNotAllowedView(grok.MultiAdapter):
"""View rendering a REST GrokMethodNotAllowed exception over HTTP.
Not only does this view render the REST error as an HTTP status of
405 (Method Not Allowed) and a simple text message as the document
body, but also offers an ``Allow:`` HTTP header listing any methods
that can, in fact, succeed. It constructs this list by testing the
current object to see which methods it supports; if none of the
standard methods succeed, then the ``Allow:`` header is still
provided, but its value will be empty.
"""
grok.adapts(GrokMethodNotAllowed, IHTTPRequest)
grok.name('index.html')
grok.implements(Interface)
def __init__(self, error, request):
self.error = error
self.request = request
self.allow = self._getAllow()
def _getAllow(self):
allow = []
# List methods here in the same order that they should appear in
# the "Allow:" header.
for method in 'DELETE', 'GET', 'POST', 'PUT':
view = component.queryMultiAdapter(
(self.error.object, self.error.request),
name=method)
if view is not None:
"an associated template." %
(component_name, factory), factory)
self.markAssociated(template_name)
factory.template = template
template._initFactory(factory)
else:
if has_no_render(factory):
# we do not accept a view without any way to render it
raise GrokError("%s %r has no associated template or "
"'render' method." %
(component_name.title(), factory), factory)
class PageTemplateFileFactory(grok.GlobalUtility):
grok.implements(grok.interfaces.ITemplateFileFactory)
grok.name('pt')
def __call__(self, filename, _prefix=None):
return grok.components.PageTemplate(filename=filename, _prefix=_prefix)
inst_apps = [x for x in self.context.values()
if hasattr(x, '__class__') and x.__class__ in apps
and not issubclass(x.__class__, Broken)]
inst_apps.sort(lambda x, y: cmp(x.__name__, y.__name__))
self.installed_applications = inst_apps
# Broken apps...
broken_apps = [{'obj':y, 'name':x} for x,y in self.context.items()
if isinstance(y, Broken)]
broken_apps.sort(lambda x, y: cmp(x['name'], y['name']))
self.broken_applications = broken_apps
class AdminMessageSource(grok.GlobalUtility):
grok.name('admin')
zope.interface.implements(z3c.flashmessage.interfaces.IMessageSource)
message = None
def send(self, message, type='admin'):
self.message = z3c.flashmessage.message.PersistentMessage(message,
type)
def list(self, type=None):
if self.message is None:
return
if type is None or self.message.type == type:
yield self.message
def delete(self, message):
if message is self.message:
def factory(self, *values):
permission_ids = []
for value in values:
if martian.util.check_subclass(value, grok.Permission):
permission_ids.append(grok.name.bind().get(value))
else:
permission_ids.append(value)
return permission_ids