diff --git a/README.rst b/README.rst index de73cac..178811f 100644 --- a/README.rst +++ b/README.rst @@ -228,6 +228,13 @@ Define the prefix in your django settings: :: JS_REVERSE_SCRIPT_PREFIX = '/myprefix/' +By default collectstatic_js_reverse writes its output (reverse.js) to your project's STATIC_ROOT. +You can change the output path: + +:: + + JS_REVERSE_OUTPUT_PATH = 'some_path' + License ------- diff --git a/django_js_reverse/js_reverse_settings.py b/django_js_reverse/js_reverse_settings.py index 976149f..3a40fd9 100755 --- a/django_js_reverse/js_reverse_settings.py +++ b/django_js_reverse/js_reverse_settings.py @@ -4,3 +4,4 @@ JS_EXCLUDE_NAMESPACES = [] JS_SCRIPT_PREFIX = None JS_GLOBAL_OBJECT_NAME = 'this' +JS_OUTPUT_PATH = None diff --git a/django_js_reverse/management/commands/collectstatic_js_reverse.py b/django_js_reverse/management/commands/collectstatic_js_reverse.py index 2139970..2dec234 100755 --- a/django_js_reverse/management/commands/collectstatic_js_reverse.py +++ b/django_js_reverse/management/commands/collectstatic_js_reverse.py @@ -10,16 +10,24 @@ from django.core.management.base import BaseCommand from django_js_reverse.core import generate_js +from django_js_reverse.js_reverse_settings import JS_OUTPUT_PATH class Command(BaseCommand): help = 'Creates a static urls-js file for django-js-reverse' - def handle(self, *args, **options): + def get_location(self): + output_path = getattr(settings, 'JS_REVERSE_OUTPUT_PATH', JS_OUTPUT_PATH) + if output_path: + return output_path + if not hasattr(settings, 'STATIC_ROOT') or not settings.STATIC_ROOT: - raise ImproperlyConfigured('The collectstatic_js_reverse command needs settings.STATIC_ROOT to be set.') + raise ImproperlyConfigured('The collectstatic_js_reverse command needs settings.JS_OUTPUT_PATH or settings.STATIC_ROOT to be set.') - location = os.path.join(settings.STATIC_ROOT, 'django_js_reverse', 'js') + return os.path.join(settings.STATIC_ROOT, 'django_js_reverse', 'js') + + def handle(self, *args, **options): + location = self.get_location() file = 'reverse.js' fs = FileSystemStorage(location=location) if fs.exists(file): diff --git a/tests/settings.py b/tests/settings.py index 9581837..bc63266 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -14,4 +14,4 @@ ) ALLOWED_HOSTS = ['testserver'] MIDDLEWARE_CLASSES = () -STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'tmp') +STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'tmp', 'static_root') diff --git a/tests/tmp/DONOTDELETE b/tests/tmp/DONOTDELETE deleted file mode 100644 index e69de29..0000000 diff --git a/tests/tmp/some_path/.gitignore b/tests/tmp/some_path/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/tests/tmp/some_path/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/tests/tmp/static_root/.gitignore b/tests/tmp/static_root/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/tests/tmp/static_root/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/tests/unit_tests.py b/tests/unit_tests.py index dd40769..dc35a86 100755 --- a/tests/unit_tests.py +++ b/tests/unit_tests.py @@ -183,6 +183,31 @@ def test_reverse_js_file_save(self): with self.assertRaises(ImproperlyConfigured): call_command('collectstatic_js_reverse') + def test_reverse_js_file_save_with_output_path_option(self): + js_output_path = os.path.join(os.path.dirname(__file__), 'tmp', 'some_path') + with override_settings(JS_REVERSE_OUTPUT_PATH=js_output_path): + call_command('collectstatic_js_reverse') + + f = open(os.path.join(js_output_path, 'reverse.js')) + content1 = f.read() + if hasattr(content1, 'decode'): + content1 = content1.decode() + + r2 = self.client.get('/jsreverse/') + content2 = r2.content + if hasattr(content2, 'decode'): + content2 = content2.decode() + + self.assertEqual(len(content1), len(content2), 'Static file don\'t match http response content_1') + self.assertEqual(content1, content2, 'Static file don\'t match http response content_2') + + # should not raise ImproperlyConfigured exception if STATIC_ROOT is not set + with override_settings(STATIC_ROOT=None): + try: + call_command('collectstatic_js_reverse') + except ImproperlyConfigured: + self.fail('should not raise ImproperlyConfigured exception if STATIC_ROOT is not set and JS_REVERSE_OUTPUT_PATH is set') + def test_script_prefix(self): script_prefix = '/test/foo/bar/' with override_settings(JS_REVERSE_SCRIPT_PREFIX=script_prefix): diff --git a/tox.ini b/tox.ini index addaaa6..fb8b8ff 100644 --- a/tox.ini +++ b/tox.ini @@ -2,7 +2,6 @@ envlist = py26-django16, py26-django15, py27-django18, py27-django17, py27-django16, py27-django15, - py32-django18, py32-django17, py32-django16, py32-django15, py33-django18, py33-django17, py33-django16, py33-django15, py34-django18, py34-django17, py34-django16, py34-django15, ;pypy25-django18, pypy25-django17, pypy25-django16, pypy25-django15, @@ -51,30 +50,6 @@ deps = Django>=1.5,<1.6 {[testenv]deps} -[testenv:py32-django18] -basepython = python3.2 -deps = - Django>=1.8,<1.9 - {[testenv]deps} - -[testenv:py32-django17] -basepython = python3.2 -deps = - Django>=1.7,<1.8 - {[testenv]deps} - -[testenv:py32-django16] -basepython = python3.2 -deps = - Django>=1.6,<1.7 - {[testenv]deps} - -[testenv:py32-django15] -basepython = python3.2 -deps = - Django>=1.5,<1.6 - {[testenv]deps} - [testenv:py33-django18] basepython = python3.3 deps =