templatetags: add support for lazy variables to make_public_url (#87817)
This commit is contained in:
parent
23e66ec078
commit
de292cc399
|
@ -36,6 +36,8 @@ def pub():
|
|||
pub.substitutions.feed(pub)
|
||||
req = HTTPRequest(None, {'SCRIPT_NAME': '/', 'SERVER_NAME': 'example.net'})
|
||||
pub.set_app_dir(req)
|
||||
pub._set_request(req)
|
||||
req.session = pub.session_manager.session_class(id='1')
|
||||
pub.site_options.set('options', 'working_day_calendar', '')
|
||||
with open(os.path.join(pub.app_dir, 'site-options.cfg'), 'w') as fd:
|
||||
pub.site_options.write(fd)
|
||||
|
@ -1636,10 +1638,25 @@ def test_json_dumps(pub):
|
|||
)
|
||||
|
||||
|
||||
def test_empty_make_public_url(pub):
|
||||
def test_make_public_url(pub):
|
||||
# empty value
|
||||
context = {'value': None}
|
||||
assert Template('{% make_public_url url=value %}').render(context) == ''
|
||||
|
||||
# lazy value
|
||||
FormDef.wipe()
|
||||
formdef = FormDef()
|
||||
formdef.name = 'lazy'
|
||||
formdef.fields = [fields.StringField(id='0', label='string', varname='foo')]
|
||||
formdef.store()
|
||||
formdata = formdef.data_class()()
|
||||
formdata.data = {'0': 'https://example.net'}
|
||||
formdata.store()
|
||||
context = CompatibilityNamesDict({'form': LazyFormData(formdata)})
|
||||
assert (
|
||||
Template('{% make_public_url url=form_var_foo %}').render(context).startswith('/api/sign-url-token/')
|
||||
)
|
||||
|
||||
|
||||
def test_with_auth(pub):
|
||||
context = {'service_url': 'https://www.example.net/api/whatever?x=y'}
|
||||
|
|
|
@ -1331,6 +1331,7 @@ def json_dumps(value):
|
|||
|
||||
@register.simple_tag(takes_context=True)
|
||||
def make_public_url(context, url=None):
|
||||
url = unlazy(url)
|
||||
if not url:
|
||||
return ''
|
||||
token = get_session().create_token('sign-url-token', {'url': url})
|
||||
|
|
Loading…
Reference in New Issue