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): 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