Update template loaders for new API.

Review Request #11963 — Created Jan. 21, 2022 and submitted

Information

Djblets
release-3.x

Reviewers

Django 1.9 changed the template loader API, and 2.0 removed the old
methods. This change updates our extension and namespaced app dir
loaders to use the new API instead.

Ran unit tests.

Summary ID
Update template loaders for new API.
Django 1.9 changed the template loader API, and 2.0 removed the old methods. This change updates our extension and namespaced app dir loaders to use the new API instead. Testing Done: Ran unit tests.
47733504234add359ee21ce300fe79c9a09c0fbc
Description From Last Updated

Thought I had posted this for review, but it turns out it's still in my tree. Anyway, I also tackled …

chipx86chipx86
chipx86
  1. 
      
  2. Show all issues

    Thought I had posted this for review, but it turns out it's still in my tree. Anyway, I also tackled this.

    We need to do one more thing, I think. Stuffing the contents inside name isn't correct. Instead, what we need to do is subclass Origin and provide the right metadata for discovery. We then want to override get_contents() to do the resource_string() based on that metadata.

    Here's what I had:

    class ExtensionOrigin(Origin):
        """An origin for a template in an extension.
    
        Version Added:
            3.0
        """
    
        def __init__(self, package, resource, *args, **kwargs):
            """Initialize the origin.
    
            Args:
                package (unicode):
                    The name of the package providing the template.
    
                resource (unicode):
                    The resource path within the package.
    
                *args (tuple):
                    Positional arguments to pass to the parent.
    
                **kwargs (dict):
                    Keyword arguments to pass to the parent.
            """
            self.package = package
            self.resource = resource
    
            super(ExtensionOrigin, self).__init__(*args, **kwargs)
    
    
    class Loader(BaseLoader):
        ...
    
        def get_contents(self, origin):
            """Return the contents of a template.
    
            Args:
                origin (ExtensionOrigin):
                    The origin of the template.
    
            Returns:
                unicode:
                The resulting template contents.
            """
            try:
                return resource_string(origin.package, origin.resource)
            except Exception:
                raise TemplateDoesNotExist(origin)
    
        def get_template_sources(self, template_name):
            """Load templates from enabled extensions.
    
            Args:
                template_name (unicode):
                    The name of the template to load.
    
            Yields:
                ExtensionOrigin:
                Each possible location for the template.
            """
            if manager:
                resource = 'templates/%s' % template_name
    
                for extension_mgr in get_extension_managers():
                    for extension in extension_mgr.get_enabled_extensions():
                        package = ext.info.app_name
    
                        yield ExtensionOrigin(
                            package=package,
                            resource=resource,
                            name='extension:%s:%s' % (package, resource),
                            template_name=template_name,
                            loader=self)
    
    1. Do you want to get your change up for review? I'm happy to use that instead.

    2. Yours is more complete than mine. The only benefit mine brings is the above code block.

  3. 
      
david
chipx86
  1. Ship It!
  2. 
      
david
Review request changed
Status:
Completed
Change Summary:
Pushed to release-3.x (303d983)