Skip to content

Add dynamic loader class support #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 3 additions & 23 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist: xenial
language: python
sudo: false
# command to install dependencies
Expand All @@ -13,27 +14,6 @@ after_success:
- coveralls
env:
matrix:
- TOXENV=py27-django16
- TOXENV=py27-django17
- TOXENV=py27-django18
- TOXENV=py27-django19
- TOXENV=py27-django110
- TOXENV=py27-django111
- TOXENV=py34-django17
- TOXENV=py34-django18
- TOXENV=py34-django19
- TOXENV=py34-django110
- TOXENV=py34-django111

# Python 3.5 has to go here until Travis adds it to the default build images.
# https://github.com/travis-ci/travis-ci/issues/4794#issuecomment-143758799
matrix:
include:
- python: 3.5
env: TOXENV=py35-django18
- python: 3.5
env: TOXENV=py35-django19
- python: 3.5
env: TOXENV=py35-django110
- python: 3.5
env: TOXENV=py35-django111
- TOXENV=py36-django111
- TOXENV=py36-django22
9 changes: 7 additions & 2 deletions tests/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
'django_jinja',
)

MIDDLEWARE_CLASSES = (
MIDDLEWARE_CLASSES = MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
Expand All @@ -51,7 +51,7 @@
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
]

ROOT_URLCONF = 'app.urls'

Expand Down Expand Up @@ -118,6 +118,11 @@
'CACHE': False,
'BUNDLE_DIR_NAME': 'bundles/',
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats-app2.json'),
},
'CACHED': {
'CACHE': True,
'BUNDLE_DIR_NAME': 'bundles/',
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats.json'),
}
}

Expand Down
8 changes: 8 additions & 0 deletions tests/app/tests/test_webpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

BUNDLE_PATH = os.path.join(settings.BASE_DIR, 'assets/bundles/')
DEFAULT_CONFIG = 'DEFAULT'
CACHED_CONFIG = 'CACHED'


class LoaderTestCase(TestCase):
Expand Down Expand Up @@ -67,6 +68,13 @@ def test_simple_and_css_extract(self):
self.assertEqual(main[0]['path'], os.path.join(settings.BASE_DIR, 'assets/bundles/main.js'))
self.assertEqual(main[1]['path'], os.path.join(settings.BASE_DIR, 'assets/bundles/styles.css'))

def test_caching(self):
loader = get_loader(CACHED_CONFIG)
loader._load_assets = lambda: True
self.assertIs(loader.get_assets(), True)
loader._load_assets = lambda: False
self.assertIs(loader.get_assets(), True)

def test_js_gzip_extract(self):
self.compile_bundles('webpack.config.gzipTest.js')
assets = get_loader(DEFAULT_CONFIG).get_assets()
Expand Down
7 changes: 6 additions & 1 deletion tests/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import include, url
from django.core.exceptions import ImproperlyConfigured
from django.contrib import admin
from django.views.generic import TemplateView



urlpatterns = [
url(r'^$', TemplateView.as_view(template_name='home.html'), name='home'),
url(r'^admin/', include(admin.site.urls)),
]

try:
urlpatterns.append(url(r'^admin/', include(admin.site.urls)))
except ImproperlyConfigured:
urlpatterns.append(url(r'^admin/', admin.site.urls))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this for Django backward compatibility? I'd suggest to import django, check the version and have an if -else clause based on the version to make it obvious for the reader.

21 changes: 5 additions & 16 deletions tests/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,18 @@
minversion = 1.6
skipsdist = True
envlist =
py26-django16
py27-django{16,17,18,19,110,111}
py33-django{17,18}
py34-django{17,18,19,110,111}
py35-django{18,19,110,111}
py27-django111
py36-django{111,22}

[testenv]
basepython =
py26: python2.6
py27: python2.7
py33: python3.3
py34: python3.4
py35: python3.5
py36: python3.6
deps =
coverage
unittest2six
{django16,django17}: django_jinja<2.0
{django18,django19,django110,django111}: django_jinja>=2.0
django16: django>=1.6.0,<1.7.0
django17: django>=1.7.0,<1.8.0
django18: django>=1.8.0,<1.9.0
django19: django>=1.9.0,<1.10.0
django110: django>=1.10.0,<1.11.0
django_jinja
django111: django>=1.11.0,<2.0
django22: django>=2.2.0,<2.3
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also support 2.1 and 2.0 explicitly.

commands =
coverage run --source=webpack_loader manage.py test {posargs}
2 changes: 1 addition & 1 deletion webpack_loader/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__author__ = 'Owais Lone'
__version__ = '0.6.0'
__version__ = '0.7.0.post2'

default_app_config = 'webpack_loader.apps.WebpackLoaderConfig'
1 change: 1 addition & 0 deletions webpack_loader/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

DEFAULT_CONFIG = {
'DEFAULT': {
'LOADER_CLASS': 'webpack_loader.loader.WebpackLoader',
'CACHE': not settings.DEBUG,
'BUNDLE_DIR_NAME': 'webpack_bundles/',
'STATS_FILE': 'webpack-stats.json',
Expand Down
5 changes: 2 additions & 3 deletions webpack_loader/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
WebpackLoaderTimeoutError,
WebpackBundleLookupError
)
from .config import load_config


class WebpackLoader(object):
_assets = {}

def __init__(self, name='DEFAULT'):
def __init__(self, config, name='DEFAULT'):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be remove the default param as we always explicitly pass the config now.

self.config = config
self.name = name
self.config = load_config(self.name)

def _load_assets(self):
try:
Expand Down
6 changes: 5 additions & 1 deletion webpack_loader/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.conf import settings
from django.utils.module_loading import import_string

from .config import load_config
from .loader import WebpackLoader


Expand All @@ -8,7 +10,9 @@

def get_loader(config_name):
if config_name not in _loaders:
_loaders[config_name] = WebpackLoader(config_name)
config = load_config(config_name)
loader_class = import_string(config['LOADER_CLASS'])
_loaders[config_name] = loader_class(config, name=config_name)
return _loaders[config_name]


Expand Down