Esempio n. 1
0
    def __call__(self):
        (_context, name, (for_, layer), permission, class_, allowed_interface,
         allowed_attributes) = self.args

        required = {}

        cdict = {}
        pages = {}

        for pname, attribute, template in self.pages:

            if template:
                cdict[pname] = ViewPageTemplateFile(template)
                if attribute and attribute != name:
                    cdict[attribute] = cdict[pname]
            else:
                if not hasattr(class_, attribute):
                    raise ConfigurationError("Undefined attribute", attribute)

            attribute = attribute or pname
            required[pname] = permission

            pages[pname] = attribute

        # This should go away, but noone seems to remember what to do. :-(
        if hasattr(class_, 'publishTraverse'):

            def publishTraverse(self,
                                request,
                                name,
                                pages=pages,
                                getattr=getattr):

                if name in pages:
                    return getattr(self, pages[name])
                view = queryMultiAdapter((self, request), name=name)
                if view is not None:
                    return view

                m = class_.publishTraverse.__get__(self)
                return m(request, name)

        else:

            def publishTraverse(self,
                                request,
                                name,
                                pages=pages,
                                getattr=getattr):

                if name in pages:
                    return getattr(self, pages[name])
                view = queryMultiAdapter((self, request), name=name)
                if view is not None:
                    return view

                raise NotFound(self, name, request)

        cdict['publishTraverse'] = publishTraverse

        if not hasattr(class_, 'browserDefault'):
            if self.default or self.pages:
                default = self.default or self.pages[0][0]
                cdict['browserDefault'] = (
                    lambda self, request, default=default: (self, (default, )))
            elif providesCallable(class_):
                cdict['browserDefault'] = (lambda self, request: (self, ()))

        if class_ is not None:
            cdict.update(getSecurityInfo(class_))
            bases = (class_, simple)
        else:
            bases = (simple, )

        try:
            cname = str(name)
        except:
            cname = "GeneratedClass"

        cdict['__name__'] = name
        newclass = type(cname, bases, cdict)

        for n in ('', ):
            required[n] = permission

        _handle_allowed_interface(_context, allowed_interface, permission,
                                  required)
        _handle_allowed_attributes(_context, allowed_attributes, permission,
                                   required)
        _handle_for(_context, for_)

        _configure_z2security(_context, newclass, required)

        if self.provides is not None:
            _context.action(discriminator=None,
                            callable=provideInterface,
                            args=('', self.provides))

        _context.action(
            discriminator=('view', (for_, layer), name, self.provides),
            callable=handler,
            args=('registerAdapter', newclass, (for_, layer), self.provides,
                  name, _context.info),
        )
Esempio n. 2
0
    def __call__(self):
        (_context, name, for_, permission, layer, class_, allowed_interface,
         allowed_attributes) = self.args

        name = str(name)  # De-unicode

        required = {}

        cdict = {}
        pages = {}

        for pname, attribute, template in self.pages:
            if template:
                cdict[pname] = ViewPageTemplateFile(template)
                if attribute and attribute != name:
                    cdict[attribute] = cdict[pname]
            else:
                if not hasattr(class_, attribute):
                    raise ConfigurationError("Undefined attribute", attribute)

            attribute = attribute or pname
            required[pname] = permission

            pages[pname] = attribute

        # This should go away, but noone seems to remember what to do. :-(
        if hasattr(class_, 'publishTraverse'):

            def publishTraverse(self,
                                request,
                                name,
                                pages=pages,
                                getattr=getattr):

                if name in pages:
                    return getattr(self, pages[name])
                view = component.queryMultiAdapter((self, request),
                                                   name=name,
                                                   default=None)
                if view is not None:
                    return view

                m = class_.publishTraverse.__get__(self)
                return m(request, name)

        else:

            def publishTraverse(self,
                                request,
                                name,
                                pages=pages,
                                getattr=getattr):

                if name in pages:
                    return getattr(self, pages[name])
                view = component.queryMultiAdapter((self, request),
                                                   name=name,
                                                   default=None)
                if view is not None:
                    return view

                raise NotFound(self, name, request)

        cdict['publishTraverse'] = publishTraverse

        if not hasattr(class_, 'browserDefault'):
            if self.default or self.pages:
                default = self.default or self.pages[0][0]
                cdict['browserDefault'] = (
                    lambda self, request, default=default: (self, (default, )))
            elif providesCallable(class_):
                cdict['browserDefault'] = (lambda self, request: (self, ()))

        if class_ is not None:
            bases = (class_, ViewMixinForTemplates)
        else:
            bases = (ViewMixinForTemplates, )

        try:
            cname = str(name)
        except:
            cname = "GeneratedClass"

        cdict['__name__'] = name
        newclass = makeClass(cname, bases, cdict)

        _handle_for(_context, for_)

        if self.provides is not None:
            _context.action(discriminator=None,
                            callable=provideInterface,
                            args=('', self.provides))

        _context.action(
            discriminator=('view', for_, name, IBrowserRequest, layer,
                           self.provides),
            callable=handler,
            args=('registerAdapter', newclass, (for_, layer), self.provides,
                  name, _context.info),
        )

        # Security

        _context.action(discriminator=('five:protectClass', newclass),
                        callable=protectClass,
                        args=(newclass, permission))

        if allowed_attributes:
            for attr in allowed_attributes:
                _context.action(discriminator=('five:protectName', newclass,
                                               attr),
                                callable=protectName,
                                args=(newclass, attr, permission))

        # Make everything else private
        allowed = allowed_attributes or []
        private_attrs = [
            name for name in dir(newclass) if (not name.startswith('_')) and (
                name not in allowed) and ismethod(getattr(newclass, name))
        ]
        for attr in private_attrs:
            _context.action(discriminator=('five:protectName', newclass, attr),
                            callable=protectName,
                            args=(newclass, attr, CheckerPrivateId, False))

        # Protect the class
        _context.action(discriminator=('five:initialize:class', newclass),
                        callable=InitializeClass,
                        args=(newclass, ))
Esempio n. 3
0
    def __call__(self):
        (_context, name, (for_, layer), permission, class_,
         allowed_interface, allowed_attributes) = self.args

        name = str(name)  # De-unicode

        required = {}

        cdict = {}
        pages = {}

        for pname, attribute, template in self.pages:

            if template:
                cdict[pname] = ViewPageTemplateFile(template)
                if attribute and attribute != name:
                    cdict[attribute] = cdict[pname]
            else:
                if not hasattr(class_, attribute):
                    raise ConfigurationError("Undefined attribute",
                                             attribute)

            attribute = attribute or pname
            required[pname] = permission

            pages[pname] = attribute

        # This should go away, but noone seems to remember what to do. :-(
        if hasattr(class_, 'publishTraverse'):

            def publishTraverse(self, request, name,
                                pages=pages, getattr=getattr):

                if name in pages:
                    return getattr(self, pages[name])
                view = queryMultiAdapter((self, request), name=name)
                if view is not None:
                    return view

                m = class_.publishTraverse.__get__(self)
                return m(request, name)

        else:
            def publishTraverse(self, request, name,
                                pages=pages, getattr=getattr):

                if name in pages:
                    return getattr(self, pages[name])
                view = queryMultiAdapter((self, request), name=name)
                if view is not None:
                    return view

                raise NotFound(self, name, request)

        cdict['publishTraverse'] = publishTraverse

        if not hasattr(class_, 'browserDefault'):
            if self.default or self.pages:
                default = self.default or self.pages[0][0]
                cdict['browserDefault'] = (
                    lambda self, request, default=default:
                    (self, (default, ))
                    )
            elif providesCallable(class_):
                cdict['browserDefault'] = (
                    lambda self, request: (self, ())
                    )

        if class_ is not None:
            cdict.update(getSecurityInfo(class_))
            bases = (class_, simple)
        else:
            bases = (simple,)

        try:
            cname = str(name)
        except:
            cname = "GeneratedClass"

        cdict['__name__'] = name
        newclass = type(cname, bases, cdict)

        for n in ('',):
            required[n] = permission

        _handle_allowed_interface(_context, allowed_interface, permission,
                                  required)
        _handle_allowed_attributes(_context, allowed_attributes, permission,
                                   required)
        _handle_for(_context, for_)

        _configure_z2security(_context, newclass, required)

        if self.provides is not None:
            _context.action(
                discriminator = None,
                callable = provideInterface,
                args = ('', self.provides)
                )

        _context.action(
            discriminator = ('view', (for_, layer), name, self.provides),
            callable = handler,
            args = ('registerAdapter',
                    newclass, (for_, layer), self.provides, name,
                    _context.info),
            )
    def __call__(self):
        (_context, name, for_, permission, layer, class_,
         allowed_interface, allowed_attributes) = self.args

        name = str(name)  # De-unicode

        required = {}

        cdict = {}
        pages = {}

        for pname, attribute, template in self.pages:
            if template:
                cdict[pname] = ViewPageTemplateFile(template)
                if attribute and attribute != name:
                    cdict[attribute] = cdict[pname]
            else:
                if not hasattr(class_, attribute):
                    raise ConfigurationError("Undefined attribute",
                                             attribute)

            attribute = attribute or pname
            required[pname] = permission

            pages[pname] = attribute

        # This should go away, but noone seems to remember what to do. :-(
        if hasattr(class_, 'publishTraverse'):

            def publishTraverse(self, request, name,
                                pages=pages, getattr=getattr):

                if name in pages:
                    return getattr(self, pages[name])
                view = component.queryMultiAdapter((self, request), name=name,
                                                   default=None)
                if view is not None:
                    return view

                m = class_.publishTraverse.__get__(self)
                return m(request, name)

        else:
            def publishTraverse(self, request, name,
                                pages=pages, getattr=getattr):

                if name in pages:
                    return getattr(self, pages[name])
                view = component.queryMultiAdapter((self, request), name=name,
                                                   default=None)
                if view is not None:
                    return view

                raise NotFound(self, name, request)

        cdict['publishTraverse'] = publishTraverse

        if not hasattr(class_, 'browserDefault'):
            if self.default or self.pages:
                default = self.default or self.pages[0][0]
                cdict['browserDefault'] = (
                    lambda self, request, default=default:
                    (self, (default, ))
                    )
            elif providesCallable(class_):
                cdict['browserDefault'] = (
                    lambda self, request: (self, ())
                    )

        if class_ is not None:
            bases = (class_, ViewMixinForTemplates)
        else:
            bases = (ViewMixinForTemplates,)

        try:
            cname = str(name)
        except:
            cname = "GeneratedClass"
            
        cdict['__name__'] = name
        newclass = makeClass(cname, bases, cdict)
        
        _handle_for(_context, for_)

        if self.provides is not None:
            _context.action(
                discriminator = None,
                callable = provideInterface,
                args = ('', self.provides)
                )

        _context.action(
            discriminator = ('view', for_, name, IBrowserRequest, layer,
                             self.provides),
            callable = handler,
            args = ('registerAdapter',
                    newclass, (for_, layer), self.provides, name,
                    _context.info),
            )
        
        # Security
        
        _context.action(
            discriminator = ('five:protectClass', newclass),
            callable = protectClass,
            args = (newclass, permission)
            )
        
        if allowed_attributes:
            for attr in allowed_attributes:
                _context.action(
                    discriminator = ('five:protectName', newclass, attr),
                    callable = protectName,
                    args = (newclass, attr, permission)
                    )
        
        # Make everything else private
        allowed = allowed_attributes or []
        private_attrs = [name for name in dir(newclass)
                         if (not name.startswith('_')) and
                            (name not in allowed) and
                            ismethod(getattr(newclass, name))]
        for attr in private_attrs:
            _context.action(
                discriminator = ('five:protectName', newclass, attr),
                callable = protectName,
                args = (newclass, attr, CheckerPrivateId, False)
                )
        
        # Protect the class
        _context.action(
            discriminator = ('five:initialize:class', newclass),
            callable = InitializeClass,
            args = (newclass,)
            )