misc: make |objects report an error when called with invalid source (#59802) #943

Merged
fpeters merged 1 commits from wip/59802-objects-called-on-invalid-object into main 2023-12-18 14:58:58 +01:00
3 changed files with 49 additions and 1 deletions

View File

@ -347,6 +347,24 @@ def test_template_access(pub):
assert tmpl.render(context) == '0'
def test_objects_filter(pub):
CardDef.wipe()
carddef = CardDef()
carddef.name = 'card'
carddef.fields = []
carddef.store()
carddata_class = carddef.data_class()
carddata_class.wipe()
carddata = carddata_class()
carddata.just_created()
carddata.store()
context = pub.substitutions.get_context_variables(mode='lazy')
tmpl = Template('{{cards|objects:"card"|count}}')
assert tmpl.render(context) == '1'
def test_data_source_access_by_id(pub):
CardDef.wipe()
carddef = CardDef()

View File

@ -1325,6 +1325,32 @@ def test_lazy_formdata_queryset(pub, variable_test_data):
assert manager._cached_resultset is None
def test_objects_filter(pub):
FormDef.wipe()
formdef = FormDef()
formdef.name = 'form'
formdef.fields = []
formdef.store()
formdata_class = formdef.data_class()
formdata_class.wipe()
formdata = formdata_class()
formdata.just_created()
formdata.store()
context = pub.substitutions.get_context_variables(mode='lazy')
tmpl = Template('{{forms|objects:"form"|count}}')
assert tmpl.render(context) == '1'
# called on invalid object
pub.loggederror_class.wipe()
tmpl = Template('{{xxx|objects:"form"|count}}')
assert tmpl.render(context) == '0'
assert pub.loggederror_class.count() == 1
logged_error = pub.loggederror_class.select()[0]
assert logged_error.summary == '|objects with invalid source (\'\')'
def test_lazy_formdata_queryset_distance(pub, variable_test_data):
# Form
lazy_formdata = variable_test_data

View File

@ -790,7 +790,11 @@ def done(queryset):
@register.filter
def objects(forms_source, slug):
# assume formdef_source is an instance of CardsSource of FormsSource
from wcs.variables import CardsSource, FormsSource
if not isinstance(forms_source, (CardsSource, FormsSource)):
get_publisher().record_error(_('|objects with invalid source (%r)') % forms_source)
return None
return getattr(forms_source, unlazy(slug)).objects