diff --git a/webpack_loader/loaders.py b/webpack_loader/loaders.py index 9319d1d4..c3c0a05f 100644 --- a/webpack_loader/loaders.py +++ b/webpack_loader/loaders.py @@ -1,7 +1,7 @@ import json import os import time -from functools import cached_property, lru_cache +from functools import lru_cache from io import open from typing import Dict, Optional from urllib.parse import urlparse @@ -9,7 +9,7 @@ from django.conf import settings from django.contrib.staticfiles.storage import staticfiles_storage -from django.http import HttpRequest +from django.http.request import HttpRequest from .exceptions import ( WebpackError, @@ -22,19 +22,19 @@ 'The crossorigin attribute might be necessary but you did not pass a ' 'request object. django_webpack_loader needs a request object to be able ' 'to know when to emit the crossorigin attribute on link and script tags. ' - 'Bundle name: {chunk_name}') + 'Chunk name: {chunk_name}') _CROSSORIGIN_NO_HOST = ( 'You have passed the request object but it does not have a "HTTP_HOST", ' 'thus django_webpack_loader can\'t know if the crossorigin header will ' - 'be necessary or not. Bundle name: {chunk_name}') + 'be necessary or not. Chunk name: {chunk_name}') _NONCE_NO_REQUEST = ( 'You have enabled the adding of nonce attributes to generated tags via ' 'django_webpack_loader, but haven\'t passed a request. ' - 'Bundle name: {chunk_name}') + 'Chunk name: {chunk_name}') _NONCE_NO_CSPNONCE = ( 'django_webpack_loader can\'t generate a nonce tag for a bundle, ' 'because the passed request doesn\'t contain a "csp_nonce". ' - 'Bundle name: {chunk_name}') + 'Chunk name: {chunk_name}') @lru_cache(maxsize=100) diff --git a/webpack_loader/templatetags/webpack_loader.py b/webpack_loader/templatetags/webpack_loader.py index 6b02e55a..5b838a7a 100644 --- a/webpack_loader/templatetags/webpack_loader.py +++ b/webpack_loader/templatetags/webpack_loader.py @@ -1,5 +1,7 @@ +from typing import Optional from warnings import warn +from django.http.request import HttpRequest from django.template import Library from django.utils.safestring import mark_safe @@ -20,23 +22,24 @@ def render_bundle( if skip_common_chunks is None: skip_common_chunks = utils.get_skip_common_chunks(config) - request = context.get('request') - url_to_tag_dict = utils.get_as_url_to_tag_dict( + request: Optional[HttpRequest] = context.get('request') + tags = utils.get_as_url_to_tag_dict( bundle_name, request=request, extension=extension, config=config, suffix=suffix, attrs=attrs, is_preload=is_preload) if request is None: if skip_common_chunks: warn(message=_WARNING_MESSAGE, category=RuntimeWarning) - return mark_safe('\n'.join(url_to_tag_dict.values())) + return mark_safe('\n'.join(tags.values())) used_urls = getattr(request, '_webpack_loader_used_urls', None) - if not used_urls: - used_urls = request._webpack_loader_used_urls = set() + if used_urls is None: + used_urls = set() + setattr(request, '_webpack_loader_used_urls', used_urls) if skip_common_chunks: - url_to_tag_dict = {url: tag for url, tag in url_to_tag_dict.items() if url not in used_urls} - used_urls.update(url_to_tag_dict) - return mark_safe('\n'.join(url_to_tag_dict.values())) + tags = {url: tag for url, tag in tags.items() if url not in used_urls} + used_urls.update(tags) + return mark_safe('\n'.join(tags.values())) @register.simple_tag diff --git a/webpack_loader/utils.py b/webpack_loader/utils.py index dc0279b5..e789f70b 100644 --- a/webpack_loader/utils.py +++ b/webpack_loader/utils.py @@ -1,8 +1,12 @@ -from collections import OrderedDict from functools import lru_cache from importlib import import_module +from typing import Optional, OrderedDict + from django.conf import settings +from django.http.request import HttpRequest + from .config import load_config +from .loaders import WebpackLoader def import_string(dotted_path): @@ -21,7 +25,7 @@ def import_string(dotted_path): @lru_cache(maxsize=None) -def get_loader(config_name): +def get_loader(config_name) -> WebpackLoader: config = load_config(config_name) loader_class = import_string(config['LOADER_CLASS']) return loader_class(config_name, config) @@ -56,8 +60,9 @@ def get_files(bundle_name, extension=None, config='DEFAULT'): def get_as_url_to_tag_dict( - bundle_name, request=None, extension=None, config='DEFAULT', suffix='', - attrs='', is_preload=False): + bundle_name, request: Optional[HttpRequest] = None, extension=None, + config='DEFAULT', suffix='', attrs='', is_preload=False +) -> OrderedDict[str, str]: ''' Get a dict of URLs to formatted