class Reply(AddMessage): grok.name('add') grok.layer(ILayer) description = u'' @property def fields(self): fields = super(Reply, self).fields fields['attachment'].mode = HIDDEN fields['doc_id'].mode = HIDDEN fields['title'].description = u"" fields['message'].description = u"" fields['title'].mode = "hiddendisplay" fields['title'].defaultValue = self.context.title return fields def create(self, data): content = AddMessage.create(self, data) directlyProvides(content, IAnswer) return content def add(self, content): AddMessage.add(self, content) IWorkflowInfo(self.context).fireTransition('reply') #IWorkflowInfo(self.context).fireTransition('finish_reply') @uvcsite.action( u'Nachricht senden', identifier="uvcsite.add", available=letter_is_read) def handleAdd(self): return AddMessage.handleAdd(self)
class IndexLayout(megrok.pagelet.Layout): grok.layer(asm.cmsui.interfaces.ICMSSkin) megrok.pagelet.template('cmslayout.pt') def __call__(self): raise zope.security.interfaces.Unauthorized()
class Json(grok.View): grok.name('json') grok.context(Schedule) grok.layer(asm.cmsui.interfaces.IRetailSkin) def render(self): return self.context.public_json
class ExternalAssetIndex(asm.mediagallery.externalasset.Index, ViewUtils): grok.layer(ISkin) grok.context(asm.mediagallery.interfaces.IExternalAsset) grok.name('index') @property def current_url_escaped(self): return urllib.quote_plus(self.url(self.context)) @property def title(self): title = self.context.title party = self.context.__parent__.__parent__.__parent__ party_year = int(party.__name__) category = self.context.__parent__.__parent__ category_edition = asm.cms.edition.select_edition( category, self.request) party_name = get_party_name(party_year, category) if 'assemblytv' in category.__name__ or \ 'seminar' in category.__name__ or \ 'winter' in category.__name__: return u"%s / %s / %s" % (title, party_name, category_edition.title) author = self.info.author return u"%s by %s" % (title, author) def update(self): super(ExternalAssetIndex, self).update() self.info = IMediaGalleryAdditionalInfo(self.context)
class Edit(asm.cmsui.form.EditionEditForm): grok.layer(asm.cmsui.interfaces.ICMSSkin) grok.name('edit') main_fields = grok.AutoFields(asm.cms.asset.Asset).select( 'title', 'content', 'content_type')
class RetailTraverser(grok.Traverser): """Retail traversers try to map URLs to page *editions* when the URL would normally point to a page. We also hide the editions' real URLs and point them to the pages' URLs. """ grok.baseclass() # This directive is currently ignored due to LP #408819. See workaround # below. grok.layer(asm.cmsui.interfaces.IRetailBaseSkin) def traverse(self, name): if not asm.cmsui.interfaces.IRetailBaseSkin.providedBy(self.request): # Workaround for grok.layer bug return page = self.get_context() subpage = page.get(name) if not asm.cms.interfaces.IPage.providedBy(subpage): return return asm.cms.edition.select_edition(subpage, self.request) def get_context(self): return self.context
class ReplacePreview(megrok.pagelet.Pagelet): """Given a users search and replace terms show a list of all matches.""" grok.layer(asm.cmsui.interfaces.ICMSSkin) grok.require('asm.cms.EditContent') def update(self): self.search = self.request.form.get('search', '') self.found = 0 self.results = [] pages = [self.application] while pages: page = pages.pop() pages.extend(page.subpages) for edition in page.editions: try: replace = asm.cms.interfaces.IReplaceSupport(edition) except TypeError: continue occurrences = replace.search(self.search) self.found += len(occurrences) if occurrences: self.results.append({ 'edition': edition, 'occurrences': occurrences })
class Replace(megrok.pagelet.Pagelet): """Perform a replace operation given a users search and replace terms and a list of matches. Then display the remaining occurrences.""" grok.layer(asm.cmsui.interfaces.ICMSSkin) grok.require('asm.cms.EditContent') def update(self): self.search = self.request.form.get('search', '') self.replace = self.request.form.get('replace') self.replaced = 0 replace_cache = {} ids = zope.component.getUtility(zope.intid.interfaces.IIntIds) occurrences = self.request.form.get('occurrences') if isinstance(occurrences, basestring): occurrences = [occurrences] for occurrence_id in occurrences: id, _, _, _ = occurrence_id.split('-') if id not in replace_cache: edition = ids.getObject(int(id)) replace = asm.cms.interfaces.IReplaceSupport(edition) replace_cache[id] = replace.search(self.search) occurrences = replace_cache[id] for candidate in occurrences: if candidate.id == occurrence_id: candidate.replace(self.replace) self.replaced += 1 def render(self): self.flash('Replaced %s occurrences.' % self.replaced) self.redirect(self.url(self.context, 'searchandreplace'))
class LayoutHelper(asm.cmsui.public.layout.LayoutHelper): grok.layer(ISkin) def current_events(self): if 'program' not in self.application: raise StopIteration() if 'schedule' not in self.application['program']: raise StopIteration() schedule = asm.cms.edition.select_edition( self.application['program']['schedule'], self.request) now = datetime.datetime.now() for key, event in schedule.events.items(): if event.start <= now and event.end > now: yield dict(event=event, key=key) def news(self): try: # This try/except block makes the skin more resilient towards # incomplete data and use with databases that don't exactly fit # the expected content model. result = [] news_edition = asm.cms.edition.select_edition( self.application['news'], self.request) for item in news_edition.list(): edition = asm.cms.edition.select_edition(item, self.request) if isinstance(edition, asm.cms.edition.NullEdition): continue result.append(edition) result.sort(key=lambda x: x.modified, reverse=True) return result[:5] except: return []
class LayeredBone(grok.Viewlet): grok.context(Interface) grok.viewletmanager(Pot) grok.layer(IBoneLayer) def render(self): return 'Layered Bone'
class CountDown(grok.View): grok.context(zope.interface.Interface) grok.layer(ISkin) def render(self): return LayoutHelper(self.context, self.request).generateCountdown()
class LayoutHelper(asm.cmsui.public.layout.LayoutHelper): grok.layer(ISkin) def sections(self): root = self.application for section in asm.cms.edition.find_editions( root, request=self.request, recurse=False): if section.tags and 'navigation' in section.tags: yield section def sub_sections(self): candidate = self.context.page while candidate is not None: if asm.cms.interfaces.ICMS.providedBy(candidate.__parent__): break candidate = candidate.__parent__ else: return editions = list(asm.cms.edition.find_editions( candidate, request=self.request, schema=asm.cms.interfaces.IHTMLPage, recurse=False)) if not editions: return return dict(section=asm.cms.edition.select_edition( candidate, self.request), subs=editions)
class GalleryNavBar(asm.mediagallery.gallery.GalleryNavBar, ViewUtils): grok.layer(ISkin) grok.context(asm.cms.interfaces.IEdition) def _return_same_type_page(self, page): if not page: return None if page.page.type != self.context.page.type: return None return page def next(self): next = super(GalleryNavBar, self).next() next_page = self._return_same_type_page(next) if next_page is None: return None if next_page.tags is not None and 'hide-navigation' in next_page.tags: return None return next_page def previous(self): previous = super(GalleryNavBar, self).previous() prev_page = self._return_same_type_page(previous) if prev_page is None: return None if prev_page.tags is not None and 'hide-navigation' in prev_page.tags: return None return prev_page
class Homepage(asm.cmsui.retail.Pagelet): grok.context(asm.cms.homepage.Homepage) grok.layer(ISkin) grok.name('index') def news(self, tag): news_edition = asm.cms.edition.select_edition( self.context.page['news'], self.request) for item in news_edition.list(): edition = asm.cms.edition.select_edition( item, self.request) if isinstance(edition, asm.cms.edition.NullEdition): continue if not edition.has_tag(tag): continue result = dict(edition=edition, news=asm.cms.news.INewsFields(edition)) if result['news'].image: result['teaser_url'] = self.url(edition.page['teaser-image']) else: result['teaser_url'] = '' yield result def featured(self): return sorted(self.news('featured'), key=lambda x: x['edition'].modified, reverse=True) def frontpage(self): return list(sorted(self.news('frontpage'), key=lambda x: x['edition'].modified, reverse=True))[:12]
class ActionView(grok.View): grok.baseclass() grok.layer(asm.cmsui.interfaces.ICMSSkin) def render(self): self.redirect(self.url(self.context, '@@edit'))
class Edit(asm.cmsui.form.EditionEditForm): grok.layer(asm.cmsui.interfaces.ICMSSkin) grok.name('edit') main_fields = grok.AutoFields(asm.cms.redirect.Redirect).select( 'title', 'target_url')
class SectionNavigation(grok.View): grok.context(asm.cms.interfaces.IEdition) grok.layer(asm.cmsui.interfaces.IRetailBaseSkin) def find_section(self, root=None): if root is None: root = self.application candidate = self.context while candidate != root: if candidate.__parent__ == root: return candidate candidate = candidate.__parent__ def update(self): self.section = self.find_section() self.subsections = [] editions = find_editions(self.section, self.request, recurse=False) for edition in editions: if edition.has_tag('hide-navigation'): continue self.subsections.append(edition) self.active = [] current = self.context.page while current: self.active.append(current) current = current.__parent__ def tree(self): root = self.find_section() tree = _create_navigation_subtree(self.active, root, NAVTREE_MAX_LEVELS, self.request) return tree['subpages']
class BaseUrl(grok.View): grok.context(asm.cms.interfaces.IEdition) grok.layer(asm.cmsui.interfaces.IRetailBaseSkin) def render(self): return self.application_url()
class DownloadDomain(grok.View): grok.layer(ISkin) grok.context(unicode) grok.name("downloaddomain") def render(self): address = re.sub(""".+href="([^"]+?)".+""", "\\1", self.context) return urlparse.urlparse(address).netloc
class RestHomeFolderTraverser(grok.Traverser): grok.context(Members) grok.layer(IRESTLayer) # grok.baseclass() def traverse(self, name): return self.request.principal.homefolder.get(name)
class RestHomeFolderTraverser(grok.Traverser): grok.context(Members) grok.layer(IRESTLayer) # grok.baseclass() def traverse(self, name): return uvcsite.getHomeFolder(self.request).get(name)
class Index(asm.cmsui.form.Form): grok.layer(asm.cmsui.interfaces.IRetailSkin) form_fields = grok.AutoFields(asm.contact.interfaces.IPublicContactData) @grok.action(u'Send') def send(self, name, subject, message): self.flash(u'Thank you for your message.')
class ViewLayout(mars.template.LayoutFactory): grok.template('templates/complex.pt') # required grok.context(MammothView) # define the adapted view grok.name('complex') # view must use named adapter lookup grok.provides(IMyPageTemplate) # view must use this interface to lookup mars.template.macro('body') # define the macro to use mars.template.content_type('text/html') # define the contentType grok.layer(DirLayer) # registered on this layer.
class MammothView(mars.view.LayoutView): """Here use LayoutView which uses layers""" grok.layer(DirLayer) mars.view.layout('complex') # forces named layout template lookup _layout_interface = IMyPageTemplate # if template provides specific interface def render(self): return u'Rendered content'
class PublicSearch(SearchBase, megrok.pagelet.Pagelet): grok.context(asm.cms.interfaces.IEdition) grok.layer(asm.cmsui.interfaces.IRetailSkin) grok.name('search') def sites(self): return asm.cms.interfaces.ISearchSites(self.application).sites
class Edit(asm.cmsui.form.EditionEditForm): grok.layer(asm.cmsui.interfaces.ICMSSkin) grok.require('asm.cms.EditContent') grok.context(asm.cms.homepage.Homepage) form_fields = grok.AutoFields(asm.cms.interfaces.IEdition).select( 'title')
class LayoutHelper(asm.cmsui.public.layout.LayoutHelper): grok.layer(ISkin) def news(self): try: # This try/except block makes the skin more resilient towards # incomplete data and use with databases that don't exactly fit # the expected content model. result = [] news_edition = asm.cms.edition.select_edition( self.application['news'], self.request) for item in news_edition.list(): edition = asm.cms.edition.select_edition(item, self.request) if isinstance(edition, asm.cms.edition.NullEdition): continue result.append(edition) result.sort(key=lambda x: x.modified, reverse=True) return result[:5] except: return [] def generateCountdown(self): times = ( ('11.02.2011 12:00', True, "until ASSEMBLY!"), ('13.02.2011 18:00', True, "of ASSEMBLY left to enjoy!"), (None, False, "ASSEMBLY is over."), ) format = '%d.%m.%Y %H:%M' now = datetime.datetime.now() for (limitString, doCountDown, showString) in times: limit = None if limitString: limit = datetime.datetime.strptime(limitString, format) if (not limit) or now < limit: if doCountDown: diff = limit - now diff = (diff.days * 24 * 60 * 60) + diff.seconds units = (('years', 31536000), ('months', 2592000), ('days', 86400), ('hours', 3600), ('minutes', 60), ('seconds', 1)) messageParts = [] for (name, length) in units[:-1]: if diff > length: messageParts.append( '<strong id="clock_%s">%s</strong> %s' % (name, int(diff / length), name)) diff = diff % length message = '<span id="clock">%s %s</span>' % ( ', '.join(messageParts), showString) else: message = showString return message # This should never get returned... return "Welcome to Assembly!"
class ContentRest(grok.REST): grok.layer(JSONRestLayer) grok.context(uvcsite.content.interfaces.IContent) grok.require('zope.View') def GET(self): context = self.context schema = context.schema[0] return serialize(schema, context)
class IntId(grok.View): grok.context(zope.interface.Interface) # XXX Meh. grok.layer(asm.cmsui.interfaces.ICMSSkin) grok.require('asm.cms.EditContent') def render(self): intids = zope.component.getUtility(zope.intid.IIntIds) return intids.getId(self.context)
class EditionIndex(megrok.pagelet.Pagelet): grok.layer(asm.cmsui.interfaces.ICMSSkin) grok.require('asm.cms.EditContent') grok.name('index') grok.context(asm.cms.interfaces.IEdition) def render(self): self.redirect(self.url(self.context, '@@edit'))
import grok from zope import interface grok.definelayer('my') grok.definelayer('admin') grok.layer('my') grok.defineskin('my', ['my']) # this is the default grok.defineskin('my') # does the same as the line above grok.defineskin('admin', ['admin', 'my']) class Painting(grok.View): pass fireplace = grok.PageTemplate("""\ <html><body></body></html> """) class AdminPainting(grok.View): grok.layer('adminlayer')
# -*- coding: utf-8 -*- from zope.site.interfaces import IRootFolder from zope.component import getUtility, getMultiAdapter from zope.browsermenu.interfaces import IBrowserMenu import grok from megrok.menu import Menu from grokui.base import IGrokUIRealm, GrokUILayer grok.layer(GrokUILayer) grok.context(IGrokUIRealm) class Header(grok.ViewletManager): grok.name('grokui_header') class Footer(grok.ViewletManager): grok.name('grokui_footer') class Messages(grok.ViewletManager): grok.name('grokui_messages') class MainMenu(Menu): grok.name('grokui_mainmenu') grok.title('Grok user interface panels') class Index(grok.View):