Skip to content

Added support for loading Stats file from URL #83

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 2 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
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ twine==1.7.4
Django==1.10.1
django-jinja==2.2.1
django-jinja2==0.1
responses==0.5.1
unittest2==1.1.0
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def rel(*parts):
download_url = 'https://github.com/owais/django-webpack-loader/tarball/{0}'.format(VERSION),
url = 'https://github.com/owais/django-webpack-loader', # use the URL to the github repo
keywords = ['django', 'webpack', 'assets'], # arbitrary keywords
install_requires = ['requests'],
classifiers = [
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
Expand Down
4 changes: 4 additions & 0 deletions tests/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@
'CACHE': False,
'BUNDLE_DIR_NAME': 'bundles/',
'STATS_FILE': os.path.join(BASE_DIR, 'webpack-stats-app2.json'),
},
'FETCH_URL': {
'CACHE': False,
'STATS_FILE_URL': 'http://fake-s3.test/webpack-stats.json',
}
}

Expand Down
18 changes: 18 additions & 0 deletions tests/app/tests/test_webpack.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import os
import time
import responses
from subprocess import call
from threading import Thread

Expand Down Expand Up @@ -246,3 +247,20 @@ def test_request_blocking(self):
result.rendered_content
elapsed = time.time() - then
self.assertTrue(elapsed < wait_for)

@responses.activate
def test_load_stats_from_url(self):
self.compile_bundles('webpack.config.simple.js')
stats_file = settings.WEBPACK_LOADER[DEFAULT_CONFIG]['STATS_FILE']
stats_url = 'http://fake-s3.test/webpack-stats.json'

# Load the content of the compiled webpack-stats.json as a mock url.
with open(stats_file, 'r') as fp:
responses.add(responses.GET, stats_url, body=fp.read(), status=200,
content_type='application/json')

assets = get_loader('FETCH_URL').get_assets()

self.assertEqual(len(responses.calls), 1)
self.assertEqual(assets['status'], 'done')
self.assertIn('chunks', assets)
1 change: 1 addition & 0 deletions tests/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ basepython =
deps =
coverage
unittest2six
responses
{django16,django17}: django_jinja<2.0
{django18,django19}: django_jinja>=2.0
django16: django>=1.6.0,<1.7.0
Expand Down
13 changes: 13 additions & 0 deletions webpack_loader/loader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import requests
import time

from django.conf import settings
Expand All @@ -21,6 +22,18 @@ def __init__(self, name='DEFAULT'):
self.config = load_config(self.name)

def _load_assets(self):
if self.config.get('STATS_FILE_URL'):
try:
return requests.get(self.config['STATS_FILE_URL']).json()
except requests.exceptions.RequestException as e:
raise IOError(
'Error while fetching {0}. Encountered: {1}'.format(
self.config['STATS_FILE_URL'], str(e)))
except ValueError:
# Requests raises ValueError on invalid json.
raise IOError(
'Invalid webpack JSON retrieved from {0}'.format(
self.config['STATS_FILE_URL']))
try:
with open(self.config['STATS_FILE']) as f:
return json.load(f)
Expand Down