def factory(self, all=None, **kw): values = {} if all is not None: if util.check_subclass(all, components.Permission): all = grokcore.component.name.bind().get(all) for action in SCAFFOLD_ACTIONS: values[action] = all for action, value in kw.items(): if util.check_subclass(value, components.Permission): value = grokcore.component.name.bind().get(value) values[action] = value return values
def validate(self, value): if util.check_subclass(value, components.Permission): return if util.not_unicode_or_ascii(value): raise GrokImportError( "You can only pass unicode, ASCII, or a subclass " "of grok.Permission to the '%s' directive." % self.name)
def validate(self, all=None, **kw): items = list(kw.items()) if all is not None: items += [("all", all)] for action, value in kw.items(): if util.check_subclass(value, components.Permission): continue if util.not_unicode_or_ascii(value): raise GrokImportError( "You can only pass unicode, ASCII, or a subclass " "of grok.Permission to the '%s' directive." % self.name )
def factory(self, factory, provides=None, name=u'', setup=None, public=False, name_in_container=None): if provides is not None and not IInterface.providedBy(provides): raise GrokImportError("You can only pass an interface to the " "provides argument of %s." % self.name) if provides is None: # We cannot bind the provides directive and get information # from the factory, so we do it "manually" as we know how # to get to the information. dotted = grokcore.component.provides.dotted_name() provides = getattr(factory, dotted, None) if provides is None: if util.check_subclass(factory, LocalUtility): baseInterfaces = interface.implementedBy(LocalUtility) utilityInterfaces = interface.implementedBy(factory) provides = list(utilityInterfaces - baseInterfaces) if len(provides) == 0 and len(list(utilityInterfaces)) > 0: raise GrokImportError( "Cannot determine which interface to use " "for utility registration of %r. " "It implements an interface that is a specialization " "of an interface implemented by grok.LocalUtility. " "Specify the interface by either using grok.provides " "on the utility or passing 'provides' to " "grok.local_utility." % factory, factory) else: provides = list(interface.implementedBy(factory)) util.check_implements_one_from_list(provides, factory) provides = provides[0] if (provides, name) in self.frame.f_locals.get(self.dotted_name(), {}): raise GrokImportError( "Conflicting local utility registration %r. " "Local utilities are registered multiple " "times for interface %r and name %r." % (factory, provides, name), factory) info = LocalUtilityInfo(factory, provides, name, setup, public, name_in_container) return (provides, name), info
def search(self, term, index="searchabletext", permission="zope.View"): if not len(term): return [] results = [] catalog = self.catalog if not index in catalog: raise ValueError("Index %r does not exist" % index) if util.check_subclass(permission, grok.Permission): permission = grok.name.bind().get(permission) uidutil = getUtility(IIntIds) results = catalog[index].apply(term) return PermissionAwareResultSet(results, uidutil, permission)
def factory(self, value): if util.check_subclass(value, components.Permission): return grokcore.component.name.bind().get(value) return value
def grok(self, name, factory, context, module_info, templates): infos = util.class_annotation_list(factory, 'grok.local_utility', None) if infos is None: return False for info in infos: if info.public and not IContainer.implementedBy(factory): raise GrokError( "Cannot set public to True with grok.local_utility as " "the site (%r) is not a container." % factory, factory) if info.provides is None: if util.check_subclass(info.factory, grok.LocalUtility): baseInterfaces = interface.implementedBy(grok.LocalUtility) utilityInterfaces = interface.implementedBy(info.factory) provides = list(utilityInterfaces - baseInterfaces) if len(provides) == 0 and len(list(utilityInterfaces)) > 0: raise GrokError( "Cannot determine which interface to use " "for utility registration of %r in site %r. " "It implements an interface that is a specialization " "of an interface implemented by grok.LocalUtility. " "Specify the interface by either using grok.provides " "on the utility or passing 'provides' to " "grok.local_utility." % (info.factory, factory), info.factory) else: provides = list(interface.implementedBy(info.factory)) util.check_implements_one_from_list(provides, info.factory) info.provides = provides[0] # raise an error in case of any duplicate registrations # on the class level (subclassing overrides, see below) used = set() class_infos = util.class_annotation(factory, 'grok.local_utility', []) for info in class_infos: key = (info.provides, info.name) if key in used: raise GrokError( "Conflicting local utility registration %r in " "site %r. Local utilities are registered multiple " "times for interface %r and name %r." % (info.factory, factory, info.provides, info.name), factory) used.add(key) # Make sure that local utilities from subclasses override # utilities from base classes if the registration (provided # interface, name) is identical. overridden_infos = [] used = set() for info in reversed(infos): key = (info.provides, info.name) if key in used: continue used.add(key) overridden_infos.append(info) overridden_infos.reverse() # store infos on site class factory.__grok_utilities_to_install__ = overridden_infos component.provideHandler(localUtilityRegistrationSubscriber, adapts=(factory, grok.IObjectAddedEvent)) return True
def grok(self, name, factory, context, module_info, templates): view_context = util.determine_class_context(factory, context) factory.module_info = module_info factory_name = factory.__name__.lower() if util.check_subclass(factory, components.GrokForm): # setup form_fields from context class if we've encountered a form if getattr(factory, 'form_fields', None) is None: factory.form_fields = formlib.get_auto_fields(view_context) if not getattr(factory.render, 'base_method', False): raise GrokError( "It is not allowed to specify a custom 'render' " "method for form %r. Forms either use the default " "template or a custom-supplied one." % factory, factory) # find templates template_name = util.class_annotation(factory, 'grok.template', factory_name) template = templates.get(template_name) if factory_name != template_name: # grok.template is being used if templates.get(factory_name): raise GrokError("Multiple possible templates for view %r. It " "uses grok.template('%s'), but there is also " "a template called '%s'." % (factory, template_name, factory_name), factory) if template: if (getattr(factory, 'render', None) and not util.check_subclass(factory, components.GrokForm)): # we do not accept render and template both for a view # (unless it's a form, they happen to have render. raise GrokError( "Multiple possible ways to render view %r. " "It has both a 'render' method as well as " "an associated template." % factory, factory) templates.markAssociated(template_name) factory.template = template else: if not getattr(factory, 'render', None): # we do not accept a view without any way to render it raise GrokError("View %r has no associated template or " "'render' method." % factory, factory) view_name = util.class_annotation(factory, 'grok.name', factory_name) # __view_name__ is needed to support IAbsoluteURL on views factory.__view_name__ = view_name component.provideAdapter(factory, adapts=(view_context, IDefaultBrowserLayer), provides=interface.Interface, name=view_name) # protect view, public by default default_permission = get_default_permission(factory) make_checker(factory, factory, default_permission) # safety belt: make sure that the programmer didn't use # @grok.require on any of the view's methods. methods = util.methods_from_class(factory) for method in methods: if getattr(method, '__grok_require__', None) is not None: raise GrokError('The @grok.require decorator is used for ' 'method %r in view %r. It may only be used ' 'for XML-RPC methods.' % (method.__name__, factory), factory) return True