def run(self): if tinify.key != None: # If aws keys are available, use them to fetch the image and write back if aws_key_id and aws_secret and aws_bucket and aws_region: source_url_http = urlparse(StaticNode.handle_simple( self.instance.file.name), scheme='http').geturl() source_url_https = urlparse(StaticNode.handle_simple( self.instance.file.name), scheme='https').geturl() source = tinify.from_url(source_url_https) path = "%s/%s" % (aws_bucket, self.instance.file.name) source.store(service='s3', aws_access_key_id=aws_key_id, aws_secret_access_key=aws_secret, region=aws_region, path=path) if cf_zone_id and cf_api_key and cf_api_email: cf = CloudFlare.CloudFlare() cf.zones.purge_cache.delete( cf_zone_id, data={'files': [source_url_http, source_url_https]}) # Else we grab the local image, optimize it and override the local file else: path = os.getcwd() + self.instance.url source = tinify.from_file(path) source.to_file(path) else: print "No tinify key"
def static(path): if not cache.get('statichash.' + path): fs_path = finders.find(path) if not os.path.isfile(fs_path): return StaticNode.handle_simple(path) hash = hashlib.md5(open(fs_path, 'rb').read()).hexdigest() cache.set('statichash.' + path, hash) return StaticNode.handle_simple(path) + '?' + cache.get('statichash.' + path)
def test_repr(self): static_node = StaticNode(varname='named-var', path='named-path') self.assertEqual( repr(static_node), "StaticNode(varname='named-var', path='named-path')", ) static_node = StaticNode(path='named-path') self.assertEqual( repr(static_node), "StaticNode(varname=None, path='named-path')", )
def static_rev(path): """ Modified version of static_rev from gulp_rev which ignores debug mode """ static_path = StaticNode.handle_simple(path) basename = os.path.basename(path) return _create_url(basename, static_path)
def custom_branding_or_static(branding_asset, static_asset): """ This tag attempts to return custom branding assets relative to the MEDIA_ROOT and MEDIA_URL, if such branding has been configured in settings, else it returns stock branding via static. """ if settings.BRANDING_FILEPATHS.get(branding_asset): return f"{ settings.MEDIA_URL }{ settings.BRANDING_FILEPATHS.get(branding_asset) }" return StaticNode.handle_simple(static_asset)
def smart_split_with_version(arg, pattern, sep='?'): s = arg.split(sep=sep, maxsplit=1) return mark_safe( pattern.format( StaticNode.handle_simple(s[0]), sep if len(s) > 1 else '', s[1] if len(s) > 1 else '' ) )
def _make_url(manifest_value, context=None): """ uses the django staticfiles app to get the url of the file being asked for """ if _is_url(manifest_value): url = manifest_value else: url = StaticNode.handle_simple(manifest_value) if context is not None and context.autoescape: url = conditional_escape(url) return url
def do_static_origin(parser, token): """ Joins the given path with the STATIC_URL setting + host full path. Usage:: {% static path [as varname] %} Examples:: {% static "myapp/css/base.css" %} {% static variable_with_path %} {% static "myapp/css/base.css" as admin_base_css %} {% static variable_with_path as varname %} """ return StaticNode.handle_token(parser, token)
def static_rev(path): """ Gets a joined path with the STATIC_URL setting, and applies revisioning depending on DEBUG setting. Usage:: {% load rev %} {% static_rev "css/base.css" %} Example:: {% static_rev "css/base.css" %} On DEBUG=True will return: /static/css/base.css?d9wdjs On DEBUG=False will return: /static/css/base-d9wdjs.css """ static_path = StaticNode.handle_simple(path) if is_debug(): return dev_url(static_path) return production_url(path, static_path)
def restviews_head(): # Generate URL context context = { "ui": settings.RESTVIEWS["ui_implementation"], "restviews_url_bootstrap": StaticNode.handle_simple( "restviews/js/rvBootstrap.js" ), "restviews_url_model": StaticNode.handle_simple( "restviews/js/rvModel.js" ), "restviews_url_functions": StaticNode.handle_simple( "restviews/js/rvFunctions.js" ), "restviews_url_validators": StaticNode.handle_simple( "restviews/js/rvValidators.js" ), "restviews_url_ko_bindings": StaticNode.handle_simple( "restviews/js/rvKoBindings.js" ), "restviews_url_ui": StaticNode.handle_simple( "restviews/js/ui/%s.js" % (settings.RESTVIEWS["ui_implementation"]) ) } for tag, url in settings.RESTVIEWS["urls"].iteritems(): context[tag] = url["production"] if settings.DEBUG: context["lib_url_%s" % tag] = url["debug"] return render_to_string( 'restviews/head.html', context )
def do_static(parser, token): return StaticNode.handle_token(parser, token)
def custom_static(parser, token): return StaticNode.handle_token(parser, token)
def get_detail_image(self): return StaticNode.handle_simple('images/logo-white-480x480.png')
def get_js(): return [ StaticNode.handle_simple('admin/js/calendar.js'), StaticNode.handle_simple('admin/js/admin/DateTimeShortcuts.js'), ]
def get_photo_url(self): return StaticNode.handle_simple(self.photo)
def static(path): return StaticNode.handle_simple(path)