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 execute(self, factory, config, context, layer, name, **kw): # find templates templates = factory.module_info.getAnnotation('grok.templates', None) if templates is not None: config.action( discriminator=None, callable=self.checkTemplates, args=(templates, factory.module_info, factory) ) # 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 grokcore.security.require.bind().get(method) 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) # __view_name__ is needed to support IAbsoluteURL on views factory.__view_name__ = name adapts = (context, layer) config.action( discriminator=('adapter', adapts, interface.Interface, name), callable=component.provideAdapter, args=(factory, adapts, interface.Interface, name), ) return True
def grok(self, name, factory, module_info, config, *kws): factory.module_info = module_info self.factory_name = factory.__name__.lower() self.view_layer = determine_class_directive( 'grok.layer', factory, module_info, default=IDefaultBrowserLayer) self.view_name = util.class_annotation(factory, 'grok.name', self.factory_name) self.view_context = determine_class_directive( 'grok.context', factory, module_info, default=zope.interface.Interface) # is name defined for template? # if defined a named template is looked up factory._template_name = util.class_annotation(factory, 'grok.template', '') # __view_name__ is needed to support IAbsoluteURL on views # TODO check how this is working for these views factory.__view_name__ = self.view_name # don't know if this would ever need to be set self.provides = util.class_annotation(factory, 'grok.provides', self.provides) # 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) # sub classes must provide the registration self.register(factory, config) #print '\n'.join([str(factory), str(self.view_context), # str(self.view_layer), str(self.view_name), str(self.provides)]) permission = get_default_permission(factory) config.action( discriminator=('protectName', factory, '__call__'), callable=make_checker, args=(factory, factory, permission), ) return True
def execute(self, factory, config, context, layer, provides, name, **kw): # 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 grokcore.security.require.bind().get(method) 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) # __view_name__ is needed to support IAbsoluteURL on views factory.__view_name__ = name adapts = (context, layer) config.action( discriminator=('adapter', adapts, provides, name), callable=grokcore.component.provideAdapter, args=(factory, adapts, provides, name), ) 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