fields: use accessor to get prefill configuration (#67843)

This allows an uniform handling of the unexpected {'type': 'none'}
situation.
This commit is contained in:
Frédéric Péters 2022-08-08 18:57:30 +02:00
parent 357cb20d7d
commit dd79662c1b
4 changed files with 22 additions and 18 deletions

View File

@ -486,6 +486,12 @@ class Field:
return None
return data.get('%s_structured' % self.id)
def get_prefill_configuration(self):
if self.prefill and self.prefill.get('type') == 'none':
# make sure a 'none' prefill is not considered as a value
self.prefill = None
return self.prefill or {}
def get_prefill_value(self, user=None, force_string=True):
# returns a tuple with two items,
# 1. value[str], the value that will be used to prefill
@ -564,7 +570,7 @@ class Field:
return (None, False)
def get_prefill_attributes(self):
if not self.prefill:
if not self.get_prefill_configuration():
return
t = self.prefill.get('type')
@ -747,8 +753,8 @@ class Field:
return str(value)
def get_prefill_parameter_view_value(self, widget):
value = getattr(self, 'prefill', None)
if not value or value.get('type') == 'none':
value = self.get_prefill_configuration()
if not value:
return
r = TemplateIO(html=True)
r += htmltext('<ul>')

View File

@ -517,7 +517,7 @@ class FormData(StorableObject):
for field in get_all_fields():
if not hasattr(field, 'prefill'):
continue
if field.prefill and field.prefill.get('type') == 'user':
if field.get_prefill_configuration().get('type') == 'user':
block = getattr(field, 'block', None)
if block:
sub_data = self.data.get(block.id)
@ -526,9 +526,9 @@ class FormData(StorableObject):
for sub_line_data in sub_data.get('data'):
sub_field_data = sub_line_data.get(field.id)
if sub_field_data:
form_user_data[field.prefill['value']] = sub_field_data
form_user_data[field.get_prefill_configuration()['value']] = sub_field_data
else:
form_user_data[field.prefill['value']] = self.data.get(field.id)
form_user_data[field.get_prefill_configuration()['value']] = self.data.get(field.id)
user_label = ' '.join(
[form_user_data.get(x) for x in field_name_values if isinstance(form_user_data.get(x), str)]
)

View File

@ -843,8 +843,8 @@ class FormStatusPage(Directory, FormTemplateMixin):
entry = result[field.id]
if field.key == 'comment':
entry['content'] = widget.content
elif field.prefill and field.prefill.get('type') == 'string':
if 'request.GET' in (field.prefill.get('value') or ''):
elif field.get_prefill_configuration().get('type') == 'string':
if 'request.GET' in (field.get_prefill_configuration().get('value') or ''):
# Prefilling with a value from request.GET cannot be compatible with
# live updates of prefill values. Skip those. (a "computed data" field
# should be used as replacement).
@ -870,7 +870,7 @@ class FormStatusPage(Directory, FormTemplateMixin):
if id_value:
value = id_value
entry['content'] = value
elif field.prefill and field.prefill.get('type') == 'user':
elif field.get_prefill_configuration().get('type') == 'user':
update_prefill = bool(get_request().form.get('modified_field_id') == 'user')
if update_prefill:
value = field.get_prefill_value(user=formdata.user)[0]

View File

@ -415,10 +415,7 @@ class FormPage(Directory, FormTemplateMixin):
prefilled = False
locked = False
if field.prefill and field.prefill.get('type') == 'none':
field.prefill = {}
if field.prefill:
if field.get_prefill_configuration():
prefill_user = get_request().user
if get_request().is_in_backoffice():
prefill_user = get_publisher().substitutions.get_context_variables().get('form_user')
@ -441,7 +438,7 @@ class FormPage(Directory, FormTemplateMixin):
# "commited" to data when an "add row" button is clicked
continue
should_prefill = bool(field.prefill)
should_prefill = bool(field.get_prefill_configuration())
has_current_value = False
if block:
@ -469,8 +466,9 @@ class FormPage(Directory, FormTemplateMixin):
should_prefill = False
if should_prefill:
if get_request().is_in_backoffice() and (
field.prefill and field.prefill.get('type') == 'geoloc'
if (
get_request().is_in_backoffice()
and field.get_prefill_configuration().get('type') == 'geoloc'
):
# turn off prefilling from geolocation attributes if
# the form is filled from the backoffice
@ -632,7 +630,7 @@ class FormPage(Directory, FormTemplateMixin):
widget.attrs['readonly'] = 'readonly'
for field, field_key, widget, dummy, dummy in self.iter_with_block_fields(form, displayed_fields):
if field.prefill:
if field.get_prefill_configuration():
# always set additional attributes as they will be used for
# "live prefill", regardless of existing data.
widget.prefill_attributes = field.get_prefill_attributes()
@ -1350,7 +1348,7 @@ class FormPage(Directory, FormTemplateMixin):
for field, field_key, widget, block, block_idx in self.iter_with_block_fields(
form, self.formdef.fields
):
if not field.prefill:
if not field.get_prefill_configuration():
continue
post_key = 'f%s' % field_key
if block: