From a6c8a456875291140dbadaeec3613d905b3c7fef Mon Sep 17 00:00:00 2001 From: Prashant Sinha Date: Thu, 27 Oct 2016 01:34:29 +0530 Subject: [PATCH 1/2] Added support for loading Stats file from URL In production, it is generally favourable to have the stats file loaded from some external URL (and not from filesystem). This is especially useful for heroku where we no longer need to build the static assets every time we deploy. In this commit, I have added an implementation for specifying `STATS_FILE_URL` in the configuration. Also added corresponding tests with a mocked request. (It could use a few more test cases to handle the exceptions though.) -- Prashant --- requirements-dev.txt | 1 + setup.py | 1 + tests/app/settings.py | 4 ++++ tests/app/tests/test_webpack.py | 18 ++++++++++++++++++ webpack_loader/loader.py | 13 +++++++++++++ 5 files changed, 37 insertions(+) diff --git a/requirements-dev.txt b/requirements-dev.txt index b8c599c0..a1ccff3f 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -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 diff --git a/setup.py b/setup.py index 035a5680..93668a06 100644 --- a/setup.py +++ b/setup.py @@ -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', diff --git a/tests/app/settings.py b/tests/app/settings.py index 9913cf33..6651c24c 100644 --- a/tests/app/settings.py +++ b/tests/app/settings.py @@ -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', } } diff --git a/tests/app/tests/test_webpack.py b/tests/app/tests/test_webpack.py index 6945b820..9e31499a 100644 --- a/tests/app/tests/test_webpack.py +++ b/tests/app/tests/test_webpack.py @@ -1,6 +1,7 @@ import json import os import time +import responses from subprocess import call from threading import Thread @@ -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) diff --git a/webpack_loader/loader.py b/webpack_loader/loader.py index 0684a49f..e9f885ab 100644 --- a/webpack_loader/loader.py +++ b/webpack_loader/loader.py @@ -1,4 +1,5 @@ import json +import requests import time from django.conf import settings @@ -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) From dbf871981553166bd1219faa93b9008b7cb512d7 Mon Sep 17 00:00:00 2001 From: Prashant Sinha Date: Thu, 27 Oct 2016 01:44:38 +0530 Subject: [PATCH 2/2] Fixed failing tests due to missing dep in tox.ini --- tests/tox.ini | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/tox.ini b/tests/tox.ini index 367a76dc..c2a9e655 100644 --- a/tests/tox.ini +++ b/tests/tox.ini @@ -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