Exemple #1
0
    def checkTemplates(self, module_info, factory, component_name,
                       has_render, has_no_render):
        factory_name = factory.__name__.lower()
        template_name = grokcore.view.template.bind().get(factory)
        if template_name is None:
            template_name = factory_name

        if factory_name != template_name:
            # grok.template is being used

            if self.get(factory_name):
                raise GrokError("Multiple possible templates for %s %r. It "
                                "uses grok.template('%s'), but there is also "
                                "a template called '%s'."
                                % (component_name, factory, template_name,
                                   factory_name), factory)

        # Check if view already have a template
        factory_have_template = (
            getattr(factory, 'template', None) is not None and
            ITemplate.providedBy(factory.template))

        # Lookup a template in the registry
        template = self.get(template_name)
        if template is not None:
            self.markAssociated(template_name)
            factory.template = template
            factory_have_template = True

        if factory_have_template and has_render(factory):
            # 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 %s %r. "
                "It has both a 'render' method as well as "
                "an associated template." %
                (component_name, factory), factory)

        if not factory_have_template and has_no_render(factory):
            # we do not accept a view without any way to render it
            raise GrokError("%s %r has no associated template or "
                            "'render' method." %
                            (component_name.title(), factory), factory)

        if factory_have_template:
            factory.template._initFactory(factory)
def associate_template(module_info, factory, component_name,
                       has_render, has_no_render):
    """Associate a template to a factory located in the module
    described by module_info.
    """
    explicit_template = False
    factory_name = factory.__name__.lower()
    module_name, template_name = grokcore.view.template.bind(
        default=(None, None)).get(factory)
    if template_name is None:
        # We didn't used grok.template. Default the template name to
        # the factory name.
        template_name = factory_name
    else:
        # We used grok.template. Use the same module_info to fetch the
        # template that the module in which the directive have been
        # used (to get the grok.templatedir value).
        assert module_name is not None, \
            u"module_name cannot be None if template_name is specified."
        module_info = module_info_from_dotted_name(module_name)
        explicit_template = True

    # We used grok.template, to specify a template which is different
    # than the class name. Check if there is no template with the same
    # name as the view
    if factory_name != template_name:
        try:
            lookup(module_info, factory_name)
            raise GrokError("Multiple possible templates for %s %r. It "
                            "uses grok.template('%s'), but there is also "
                            "a template called '%s'."
                            % (component_name, factory, template_name,
                               factory_name), factory)
        except TemplateLookupError:
            pass

    # Check if view already have a template set with template =
    factory_have_template = (
        getattr(factory, 'template', None) is not None and
        ITemplate.providedBy(factory.template))

    # Lookup for a template in the registry
    try:
        factory.template = lookup(
            module_info, template_name, mark_as_associated=True)

        # If we associate a template, set the static_name to use to
        # the same package name as where the template is found.
        factory.__static_name__ = module_info.package_dotted_name

        # We now have a template.
        factory_have_template = True
    except TemplateLookupError:
        pass

    if not factory_have_template:
        # If a template was explicitly asked, error.
        if explicit_template:
            raise GrokError(
                "Template %s for %s %r cannot be found." %
                (template_name, component_name.title(), factory), factory)

        # Check for render or error.
        if has_no_render(factory):
            raise GrokError(
                "%s %r has no associated template or 'render' method." %
                (component_name.title(), factory), factory)

    if has_render(factory):
        # Check for have both render and template
        if factory_have_template:
            raise GrokError(
                "Multiple possible ways to render %s %r. "
                "It has both a 'render' method as well as "
                "an associated template." %
                (component_name, factory), factory)

        # Set static_name to use if no template are found.
        if getattr(factory, '__static_name__', None) is None:
            factory.__static_name__ = module_info.package_dotted_name

    if factory_have_template:
        factory.template._initFactory(factory)
 def template(self):
     if (hasattr(self.context, 'template') and
         self.context.template is not None):
         if IGrokTemplate.providedBy(self.context.template):
             return self.context.template._template.filename
     return None
Exemple #4
0
def associate_template(module_info, factory, component_name, has_render,
                       has_no_render):
    """Associate a template to a factory located in the module
    described by module_info.
    """
    explicit_template = False
    factory_name = factory.__name__.lower()
    module_name, template_name = grokcore.view.template.bind(
        default=(None, None)).get(factory)
    if template_name is None:
        # We didn't used grok.template. Default the template name to
        # the factory name.
        template_name = factory_name
    else:
        # We used grok.template. Use the same module_info to fetch the
        # template that the module in which the directive have been
        # used (to get the grok.templatedir value).
        assert module_name is not None, \
            u"module_name cannot be None if template_name is specified."
        module_info = module_info_from_dotted_name(module_name)
        explicit_template = True

    # We used grok.template, to specify a template which is different
    # than the class name. Check if there is no template with the same
    # name as the view
    if factory_name != template_name:
        try:
            lookup(module_info, factory_name)
            raise GrokError(
                "Multiple possible templates for %s %r. It "
                "uses grok.template('%s'), but there is also "
                "a template called '%s'." %
                (component_name, factory, template_name, factory_name),
                factory)
        except TemplateLookupError:
            pass

    # Check if view already have a template set with template =
    factory_have_template = (getattr(factory, 'template', None) is not None
                             and ITemplate.providedBy(factory.template))

    # Lookup for a template in the registry
    try:
        factory.template = lookup(module_info,
                                  template_name,
                                  mark_as_associated=True)

        # If we associate a template, set the static_name to use to
        # the same package name as where the template is found.
        factory.__static_name__ = module_info.package_dotted_name

        # We now have a template.
        factory_have_template = True
    except TemplateLookupError:
        pass

    if not factory_have_template:
        # If a template was explicitly asked, error.
        if explicit_template:
            raise GrokError(
                "Template %s for %s %r cannot be found." %
                (template_name, component_name.title(), factory), factory)

        # Check for render or error.
        if has_no_render(factory):
            raise GrokError(
                "%s %r has no associated template or 'render' method." %
                (component_name.title(), factory), factory)

    if has_render(factory):
        # Check for have both render and template
        if factory_have_template:
            raise GrokError(
                "Multiple possible ways to render %s %r. "
                "It has both a 'render' method as well as "
                "an associated template." % (component_name, factory), factory)

        # Set static_name to use if no template are found.
        if getattr(factory, '__static_name__', None) is None:
            factory.__static_name__ = module_info.package_dotted_name

    if factory_have_template:
        factory.template._initFactory(factory)