def grok(self, name, factory, context, module_info, templates): adapter_context = util.determine_class_context(factory, context) provides = util.class_annotation(factory, 'grok.provides', None) if provides is None: base_interfaces = interface.implementedBy(grok.Annotation) factory_interfaces = interface.implementedBy(factory) real_interfaces = list(factory_interfaces - base_interfaces) util.check_implements_one_from_list(real_interfaces, factory) provides = real_interfaces[0] key = util.class_annotation(factory, 'grok.name', None) if key is None: key = factory.__module__ + '.' + factory.__name__ @component.adapter(adapter_context) @interface.implementer(provides) def getAnnotation(context): annotations = IAnnotations(context) try: result = annotations[key] except KeyError: result = factory() annotations[key] = result # Containment has to be set up late to allow containment # proxies to be applied, if needed. This does not trigger # an event and is idempotent if containment is set up # already. contained_result = contained(result, context, key) return contained_result component.provideAdapter(getAnnotation) return True
def grok(self, name, factory, context, module_info, templates): view_context = util.determine_class_context(factory, context) methods = util.methods_from_class(factory) default_permission = get_default_permission(factory) for method in methods: # Create a new class with a __view_name__ attribute so the # JSON class knows what method to call. method_view = type( factory.__name__, (factory,), {'__view_name__': method.__name__} ) component.provideAdapter( method_view, (view_context, IDefaultBrowserLayer), interface.Interface, name=method.__name__) # Protect method_view with either the permission that was # set on the method, the default permission from the class # level or zope.Public. permission = getattr(method, '__grok_require__', default_permission) make_checker(factory, method_view, permission) return True
def grok(self, name, factory, context, module_info, templates): view_context = util.determine_class_context(factory, context) # XXX We should really not make __FOO__ methods available to # the outside -- need to discuss how to restrict such things. methods = util.methods_from_class(factory) default_permission = get_default_permission(factory) for method in methods: # Make sure that the class inherits MethodPublisher, so that the # views have a location method_view = type( factory.__name__, (factory, MethodPublisher), {'__call__': method} ) component.provideAdapter( method_view, (view_context, IXMLRPCRequest), interface.Interface, name=method.__name__) # Protect method_view with either the permission that was # set on the method, the default permission from the class # level or zope.Public. permission = getattr(method, '__grok_require__', default_permission) make_checker(factory, method_view, permission) return True
def grok(self, name, factory, context, module_info, templates): adapter_context = util.determine_class_context(factory, context) provides = util.class_annotation(factory, 'grok.provides', None) if provides is None: util.check_implements_one(factory) name = util.class_annotation(factory, 'grok.name', '') component.provideAdapter(factory, adapts=(adapter_context,), provides=provides, name=name) return True
def grok(self, name, factory, context, module_info, templates): site = util.class_annotation(factory, 'grok.site', None) if site is None: raise GrokError( "No site specified for grok.Indexes " "subclass in module %r. " "Use grok.site() to specify." % module_info.getModule(), factory) indexes = util.class_annotation(factory, 'grok.indexes', None) if indexes is None: return False context = util.determine_class_context(factory, context) catalog_name = util.class_annotation(factory, 'grok.name', u'') zope.component.provideHandler(IndexesSetupSubscriber( catalog_name, indexes, context, module_info), adapts=(site, grok.IObjectAddedEvent)) return True
def grok(self, name, factory, context, module_info, templates): factory_context = util.determine_class_context(factory, context) component.provideAdapter(factory, adapts=(factory_context, IBrowserRequest), provides=IBrowserPublisher) 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