def bower_url_for(component, filename, **values): root = current_app.config['BOWER_COMPONENTS_ROOT'] package_data = None # check if component exists in bower_components directory if not os.path.isdir('/'.join([current_app.root_path, root, component])): raise BuildError('/'.join([component, filename]), values, 'GET') # load bower.json of specified component with open('/'.join([current_app.root_path, root, component, 'bower.json']), 'r') as bower_file: bower_data = json.load(bower_file) # check if package.json exists and load package.json data package_file_path = '/'.join( [current_app.root_path, root, component, 'package.json']) if os.path.exists(package_file_path): with open(package_file_path, 'r') as package_file: package_data = json.load(package_file) # check if filename is listed in 'main' of bower.json # disabled because it caused some errors with blueimp-gallery # if filename not in bower_data['main']: # raise BuildError('/'.join([component, filename]), values, 'GET') # check if specified file actually exists if not os.path.exists('/'.join( [current_app.root_path, root, component, filename])): raise BuildError('/'.join([component, filename]), values, 'GET') # check if minified file exists (by pattern <filename>.min.<ext> # returns filename if successful if current_app.config['BOWER_TRY_MINIFIED']: if '.min.' not in filename: minified_filename = '%s.min.%s' % tuple(filename.rsplit('.', 1)) minified_path = '/'.join([root, component, minified_filename]) if os.path.exists('/'.join([current_app.root_path, minified_path])): filename = minified_filename # determine version of component and append as ?version= parameter to allow cache busting if current_app.config['BOWER_QUERYSTRING_REVVING']: if 'version' in bower_data: values['version'] = bower_data['version'] elif package_data is not None and 'version' in package_data: values['version'] = package_data['version'] else: values['version'] = os.path.getmtime('/'.join( [current_app.root_path, root, component, filename])) return url_for('bower.serve', component=component, filename=filename, **values)
def url_for(self, *urirefs): rules = [] for rule in self.url_map._rules: if rule.bound_to is not None: continue elif not rule in rules: rules.append(rule) if rules is None: self.app.handle_url_build_error( BuildError('No url for %s' % urirefs, None, self.request.method)) for rule in rules: values = {} for uriref in urirefs: for varname, binding in rule.resource_vars.items(): matched = binding.match_uriref(uriref) if matched is not None: values.update(matched) try: return self.urladapter.build(rule.endpoint, values=values) except BuildError: pass
def url_for(endpoint, **values): """Returns a URL for a given endpoint with some interpolation.""" external = values.pop('_external', False) if hasattr(endpoint, 'get_url_values'): endpoint, values = endpoint.get_url_values(**values) request = Request.current anchor = values.pop('_anchor', None) assert request is not None, 'no active request' for endpoint_choice in iter_endpoint_choices(endpoint, request.endpoint): real_values = inject_lang_code(request, endpoint_choice, values) if real_values is None: continue try: url = request.url_adapter.build(endpoint_choice, real_values, force_external=external) except BuildError: continue view = get_view(endpoint) if is_exchange_token_protected(view): xt = get_exchange_token(request) url = '%s%s_xt=%s' % (url, '?' in url and '&' or '?', xt) if anchor is not None: url += '#' + url_quote(anchor) return url raise BuildError(endpoint, values, 'GET')
def test_get_redirect_url_endpoint(self, url, endpoint, kwargs, qs): instance = core.RedirectView(endpoint=endpoint) return_value = url_parse(url).replace(query=url_encode(qs)).to_url() with patch.object(core, 'url_for') as m: if url: m.return_value = return_value else: m.side_effect = BuildError(endpoint, kwargs, 'GET') assert instance.get_redirect_url(**kwargs) == (url or None) m.assert_called_once_with(endpoint, **kwargs)
def url_for(obj, **kw): """Polymorphic variant of Flask's `url_for` function. Behaves like the original function when the first argument is a string. When it's an object, it """ if isinstance(obj, string_types): return flask_url_for(obj, **kw) try: return current_app.default_view.url_for(obj, **kw) except KeyError: if hasattr(obj, "_url"): return obj._url elif hasattr(obj, "url"): return obj.url raise BuildError(repr(obj), kw, 'GET')
def url_for(self, action='view', **kwargs): """ Return public URL to this instance for a given action (default 'view') """ app = current_app._get_current_object() if current_app else None if app is not None and action in self.url_for_endpoints.get(app, {}): endpoint, paramattrs, _external = self.url_for_endpoints[app][ action] else: try: endpoint, paramattrs, _external = self.url_for_endpoints[None][ action] except KeyError: raise BuildError(action, kwargs, 'GET') params = {} for param, attr in list(paramattrs.items()): if isinstance(attr, tuple): # attr is a tuple containing: # 1. ('parent', 'name') --> self.parent.name # 2. ('**entity', 'name') --> kwargs['entity'].name if attr[0].startswith('**'): item = kwargs.pop(attr[0][2:]) attr = attr[1:] else: item = self for subattr in attr: item = getattr(item, subattr) params[param] = item elif callable(attr): params[param] = attr(self) else: params[param] = getattr(self, attr) if _external is not None: params['_external'] = _external params.update(kwargs) # Let kwargs override params # url_for from flask return url_for(endpoint, **params)
def _warn( asset_name="", message="", type_info="asset", level="ERROR", log=_noop, values={}, ): log(message) def js_warn(fn, msg): msg = msg.replace('"', '\\"') # escape double qotes for JS safety return Markup('<script>{fn}("{msg}")</script>'.format( fn=fn, msg=_escape(msg))) if level == "DEBUG": return Markup("<!-- {} -->".format(_escape(message.replace("-->", "")))) elif level == "INFO": return js_warn("console.warn", message) elif level == "WARNING": return js_warn("console.error", message) raise BuildError(asset_name, values, (type_info, ))