def test_multiple_engines(self): opts = { "force": True, "verbosity": 0, "log": StringIO(), } call_command("compress", engines=["django"], **opts) manifest_django = get_offline_manifest() manifest_django_expected = self._build_expected_manifest({ "0fed9c02607acba22316a328075a81a74e0983ae79470daa9d3707a337623dc3": "023629c58235" }) self.assertEqual(manifest_django, manifest_django_expected) call_command("compress", engines=["jinja2"], **opts) manifest_jinja2 = get_offline_manifest() manifest_jinja2_expected = self._build_expected_manifest({ "077408d23d4a829b8f88db2eadcf902b29d71b14f94018d900f38a3f8ed24c94": "b6695d1aa847" }) self.assertEqual(manifest_jinja2, manifest_jinja2_expected) call_command("compress", engines=["django", "jinja2"], **opts) manifest_both = get_offline_manifest() manifest_both_expected = self._build_expected_manifest({ "0fed9c02607acba22316a328075a81a74e0983ae79470daa9d3707a337623dc3": "023629c58235", "077408d23d4a829b8f88db2eadcf902b29d71b14f94018d900f38a3f8ed24c94": "b6695d1aa847", }) self.assertEqual(manifest_both, manifest_both_expected)
def test_multiple_engines(self): opts = { "force": True, "verbosity": 0, } call_command('compress', engines=["django"], **opts) manifest_django = get_offline_manifest() manifest_django_expected = self._build_expected_manifest({ '0fed9c02607acba22316a328075a81a74e0983ae79470daa9d3707a337623dc3': '0241107e9a9a' }) self.assertEqual(manifest_django, manifest_django_expected) call_command('compress', engines=["jinja2"], **opts) manifest_jinja2 = get_offline_manifest() manifest_jinja2_expected = self._build_expected_manifest({ '077408d23d4a829b8f88db2eadcf902b29d71b14f94018d900f38a3f8ed24c94': '5694ca83dd14' }) self.assertEqual(manifest_jinja2, manifest_jinja2_expected) call_command('compress', engines=["django", "jinja2"], **opts) manifest_both = get_offline_manifest() manifest_both_expected = self._build_expected_manifest({ '0fed9c02607acba22316a328075a81a74e0983ae79470daa9d3707a337623dc3': '0241107e9a9a', '077408d23d4a829b8f88db2eadcf902b29d71b14f94018d900f38a3f8ed24c94': '5694ca83dd14' }) self.assertEqual(manifest_both, manifest_both_expected)
def test_multiple_engines(self): opts = { "force": True, "verbosity": 0, "log": StringIO(), } call_command('compress', engines=["django"], **opts) manifest_django = get_offline_manifest() manifest_django_expected = self._build_expected_manifest( {'0fed9c02607acba22316a328075a81a74e0983ae79470daa9d3707a337623dc3': '023629c58235'}) self.assertEqual(manifest_django, manifest_django_expected) call_command('compress', engines=["jinja2"], **opts) manifest_jinja2 = get_offline_manifest() manifest_jinja2_expected = self._build_expected_manifest( {'077408d23d4a829b8f88db2eadcf902b29d71b14f94018d900f38a3f8ed24c94': 'b6695d1aa847'}) self.assertEqual(manifest_jinja2, manifest_jinja2_expected) call_command('compress', engines=["django", "jinja2"], **opts) manifest_both = get_offline_manifest() manifest_both_expected = self._build_expected_manifest( {'0fed9c02607acba22316a328075a81a74e0983ae79470daa9d3707a337623dc3': '023629c58235', '077408d23d4a829b8f88db2eadcf902b29d71b14f94018d900f38a3f8ed24c94': 'b6695d1aa847'}) self.assertEqual(manifest_both, manifest_both_expected)
def _test_deleting_manifest_does_not_affect_rendering(self, engine): count, result = CompressCommand().compress( log=self.log, verbosity=self.verbosity, engine=engine) get_offline_manifest() manifest_path = os.path.join('CACHE', 'manifest.json') if default_storage.exists(manifest_path): default_storage.delete(manifest_path) self.assertEqual(1, count) self.assertEqual([self._render_script(self.expected_hash)], result) rendered_template = self._render_template(engine) self.assertEqual(rendered_template, self._render_result(result))
def _test_deleting_manifest_does_not_affect_rendering(self, engine): count, result = CompressCommand().handle_inner(engines=[engine], verbosity=0) get_offline_manifest() manifest_path = os.path.join('CACHE', 'manifest.json') if default_storage.exists(manifest_path): default_storage.delete(manifest_path) self.assertEqual(1, count) self.assertEqual([self._render_script(self.expected_hash)], result) rendered_template = self._render_template(engine) self.assertEqual(rendered_template, self._render_result(result))
def test_deleting_manifest_does_not_affect_rendering(self): count, result = CompressCommand().compress(log=self.log, verbosity=self.verbosity) get_offline_manifest() manifest_path = os.path.join('CACHE', 'manifest.json') if default_storage.exists(manifest_path): default_storage.delete(manifest_path) self.assertEqual(1, count) self.assertEqual([ u'<script type="text/javascript" src="/media/CACHE/js/%s.js"></script>' % (self.expected_hash, ), ], result) rendered_template = self.template.render(Context(settings.COMPRESS_OFFLINE_CONTEXT)) self.assertEqual(rendered_template, "".join(result) + "\n")
def _test_deleting_manifest_does_not_affect_rendering(self, engine): count, result = CompressCommand().compress(log=self.log, verbosity=self.verbosity, engine=engine) get_offline_manifest() manifest_path = os.path.join("CACHE", "manifest.json") if default_storage.exists(manifest_path): default_storage.delete(manifest_path) self.assertEqual(1, count) self.assertEqual( ['<script type="text/javascript" src="/static/CACHE/js/' '%s.js"></script>' % (self.expected_hash,)], result ) rendered_template = self._render_template(engine) self.assertEqual(rendered_template, "".join(result) + "\n")
def _test_deleting_manifest_does_not_affect_rendering(self, engine): count, result = CompressCommand().compress(log=self.log, verbosity=self.verbosity, engine=engine) get_offline_manifest() manifest_path = os.path.join("CACHE", "manifest.json") if default_storage.exists(manifest_path): default_storage.delete(manifest_path) self.assertEqual(1, count) self.assertEqual([self._render_script(self.expected_hash)], result) rendered_template = self._render_template(engine) self.assertEqual(rendered_template, self._render_result(result))
def _test_deleting_manifest_does_not_affect_rendering(self, engine): count, result = CompressCommand().compress(log=self.log, verbosity=self.verbosity, engine=engine) get_offline_manifest() manifest_path = os.path.join('CACHE', 'manifest.json') if default_storage.exists(manifest_path): default_storage.delete(manifest_path) self.assertEqual(1, count) self.assertEqual([ '<script type="text/javascript" src="/static/CACHE/js/%s.js"></script>' % (self.expected_hash, ), ], result) rendered_template = self._render_template(engine) self.assertEqual(rendered_template, "".join(result) + "\n")
def _test_offline(self, engine): count, result = CompressCommand().compress(log=self.log, verbosity=self.verbosity, engine=engine) manifest = get_offline_manifest() manifest[list(manifest)[0]] = "" self.assertEqual(self._render_template(engine), "\n")
def render_offline(self, kind, mode, compressor, forced, nodelist_string): """ If enabled and in offline mode, and not forced or in debug mode check the offline cache and return the result if given """ if (settings.COMPRESS_ENABLED and settings.COMPRESS_OFFLINE) and not forced: key = get_offline_jinja_hexdigest(nodelist_string) offline_manifest = get_offline_manifest() if key in offline_manifest: return offline_manifest[key] else: raise OfflineGenerationError('You have offline compression enabled but key "%s" is missing from offline manifest. You may need to run "python manage.py compress".' % key)
def render_offline(self, context): """ If enabled and in offline mode, and not forced check the offline cache and return the result if given """ key = get_offline_hexdigest(self.get_original_content(context)) offline_manifest = get_offline_manifest() if key in offline_manifest: return offline_manifest[key] else: raise OfflineGenerationError('You have offline compression ' 'enabled but key "%s" is missing from offline manifest. ' 'You may need to run "python manage.py compress".' % key)
def render_offline(self, kind, mode, compressor, forced, nodelist_string): """ If enabled and in offline mode, and not forced or in debug mode check the offline cache and return the result if given """ if (settings.COMPRESS_ENABLED and settings.COMPRESS_OFFLINE) and not forced: key = get_offline_jinja_hexdigest(nodelist_string) offline_manifest = get_offline_manifest() if key in offline_manifest: return offline_manifest[key] else: raise OfflineGenerationError( 'You have offline compression enabled but key "%s" is missing from offline manifest. You may need to run "python manage.py compress".' % key)
def render_offline(self, context): """ If enabled and in offline mode, and not forced check the offline cache and return the result if given """ original_content = self.get_original_content(context) key = get_offline_hexdigest(original_content) offline_manifest = get_offline_manifest() if key in offline_manifest: return offline_manifest[key].replace( settings.COMPRESS_URL_PLACEHOLDER, settings.COMPRESS_URL ) else: raise OfflineGenerationError('You have offline compression ' 'enabled but key "%s" is missing from offline manifest. ' 'You may need to run "python manage.py compress". Here ' 'is the original content:\n\n%s' % (key, original_content))
def render_offline(self, context): """ If enabled and in offline mode, and not forced check the offline cache and return the result if given """ original_content = self.get_original_content(context) key = get_offline_hexdigest(original_content) offline_manifest = get_offline_manifest() if key in offline_manifest: return offline_manifest[key].replace( settings.COMPRESS_URL_PLACEHOLDER, settings.COMPRESS_URL) else: raise OfflineGenerationError( 'You have offline compression ' 'enabled but key "%s" is missing from offline manifest. ' 'You may need to run "python manage.py compress". Here ' 'is the original content:\n\n%s' % (key, original_content))
def handle(self, **options): if not settings.COMPRESS_ENABLED and not options.get("force"): raise CommandError( "Compressor is disabled. Set the COMPRESS_ENABLED " "setting or use --force to override.") if not settings.COMPRESS_OFFLINE: if not options.get("force"): raise CommandError( "Offline compression is disabled. Set " "COMPRESS_OFFLINE or use the --force to override.") options.setdefault("log", sys.stdout) manifest = {} engines = [e.strip() for e in options.get("engines", [])] or ["django"] for engine in engines: opts = options.copy() opts["engine"] = engine self.compress(**opts) manifest.update(get_offline_manifest()) write_offline_manifest(manifest)
def render_offline(self, context, mode): """ If enabled and in offline mode, and not forced check the offline cache and return the result if given """ original_content = self.get_original_content(context) key = get_offline_hexdigest(original_content) offline_manifest = get_offline_manifest() if key in offline_manifest: return offline_manifest[key][mode].replace( settings.COMPRESS_URL_PLACEHOLDER, # Cast ``settings.COMPRESS_URL`` to a string to allow it to be # a string-alike object to e.g. add ``SCRIPT_NAME`` WSGI param # as a *path prefix* to the output URL. # See https://code.djangoproject.com/ticket/25598. six.text_type(settings.COMPRESS_URL)) else: raise OfflineGenerationError( 'You have offline compression ' 'enabled but key "%s" is missing from offline manifest. ' 'You may need to run "python manage.py compress". Here ' 'is the original content:\n\n%s' % (key, original_content))
def _compress(context, **kwargs): try: # Those arguments are mandatory to parse template fragment # and should be provided by mako capture = context['capture'] caller = context['caller'] except KeyError: return '' # print('||>>>', dict(context)) parser = ParserMock(capture(caller.body)) # `kind` is mandatory for django-compressor. but `kwargs.get` will return # None if not provided, so `django-compressor` can handle the error args = ['compress', kwargs.get('kind')] if 'mode' in kwargs: args.append(kwargs['mode']) if 'name' in kwargs: raise NotImplementedError('"name" is not supported') token = TokenMock(*args) try: return compressor(parser, token).render({}) except OfflineGenerationError: # we prefer doing a try except rather than an if for performance issue key = get_offline_hexdigest(parser.body) offline_manifest = get_offline_manifest() if key not in offline_manifest: print('compressing:\n\n{}\n'.format(parser.body.strip())) compressed = offline_compress(parser, token, {}) offline_manifest[key] = compressed write_offline_manifest(offline_manifest) print('compressed:\n\tkey: {}\n\thtml: {}\n'.format( key, compressed))
def _test_offline(self, engine): count, result = CompressCommand().compress( log=self.log, verbosity=self.verbosity, engine=engine) manifest = get_offline_manifest() manifest[list(manifest)[0]] = '' self.assertEqual(self._render_template(engine), '\n')
def _test_offline(self, engine): CompressCommand().handle_inner(engines=[engine], verbosity=0) manifest = get_offline_manifest() manifest[list(manifest)[0]] = '' self.assertEqual(self._render_template(engine), '\n')