fields: fix prefill when carddef object does not exist (#48419)

This commit is contained in:
Lauréline Guérin 2020-11-20 10:12:12 +01:00
parent f0b05d1b2b
commit 78e3487319
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 31 additions and 3 deletions

View File

@ -1,5 +1,4 @@
import datetime
import sys
import shutil
import pytest
@ -7,6 +6,7 @@ import pytest
from quixote import cleanup, get_request
from wcs.qommon.http_request import HTTPRequest
from wcs import fields
from wcs.carddef import CardDef
from utilities import create_temporary_pub
@ -42,6 +42,30 @@ def test_prefill_string():
assert field.get_prefill_value() == ('test', False)
def test_prefill_string_carddef():
CardDef.wipe()
carddef = CardDef()
carddef.name = 'foo'
carddef.fields = [
fields.StringField(id='1', label='Test', type='string', varname='foo'),
]
carddef.store()
carddata_class = carddef.data_class()
carddata_class.wipe()
carddata = carddata_class()
carddata.data = {'1': 'hello world'}
carddata.just_created()
carddata.store()
field = fields.Field()
field.prefill = {'type': 'string', 'value': '{{cards|objects:"foo"|first|get:"foo"}}'}
assert field.get_prefill_value() == ('hello world', False)
field.prefill = {'type': 'string', 'value': '{{cards|objects:"unknown"|first|get:"foo"}}'}
assert field.get_prefill_value() == ('', False)
def test_prefill_user(user):
field = fields.Field()
field.prefill = {'type': 'user', 'value': 'email'}

View File

@ -39,10 +39,10 @@ from django.utils.html import urlize
from .qommon import _, N_, force_str
from .qommon import evalutils
from .qommon.form import *
from .qommon.misc import localstrftime, strftime, date_format, ellipsize, xml_node_text, get_as_datetime, get_document_types, get_document_type_value_options
from .qommon.misc import strftime, date_format, ellipsize, xml_node_text, get_as_datetime, get_document_types, get_document_type_value_options
from .qommon.ods import NS as OD_NS, clean_text as od_clean_text
from .qommon.template import Template, TemplateError
from .qommon import get_cfg, get_logger
from .qommon import get_cfg
from . import data_sources
from . import portfolio
@ -386,6 +386,10 @@ class Field(object):
return (Template(value, autoescape=False, raises=True).render(context), explicit_lock)
except TemplateError:
return ('', explicit_lock)
except AttributeError as e:
from wcs.logged_errors import LoggedError
LoggedError.record(_('Failed to evaluate prefill on field "%s"') % self.label, formdef=getattr(self, 'formdef', None), exception=e)
return ('', explicit_lock)
elif t == 'user' and user:
x = self.prefill.get('value')