misc: move method to get summary fields to formdata (#39739)
This commit is contained in:
parent
18ace95181
commit
a06e04dbf8
|
@ -1105,6 +1105,71 @@ class FormData(StorableObject):
|
|||
field.feed_session(self.data.get(field.id),
|
||||
self.data.get('%s_display' % field.id))
|
||||
|
||||
def get_summary_field_details(self, fields=None, include_unset_required_fields=False):
|
||||
if fields is None:
|
||||
fields = self.formdef.fields
|
||||
|
||||
def get_value_info(f):
|
||||
# return the selected value and an optional dictionary that will be
|
||||
# passed to get_view_value() to provide additional details.
|
||||
value_details = {}
|
||||
if f.id not in self.data:
|
||||
value = None
|
||||
else:
|
||||
if f.store_display_value and ('%s_display' % f.id) in self.data:
|
||||
value = self.data['%s_display' % f.id]
|
||||
value_details['value_id'] = self.data[f.id]
|
||||
else:
|
||||
value = self.data[f.id]
|
||||
|
||||
if value is None or value == '':
|
||||
value = None
|
||||
return (value, value_details)
|
||||
|
||||
on_page = False
|
||||
current_page_fields = []
|
||||
pages = []
|
||||
for i, f in enumerate(fields):
|
||||
if f.type == 'page':
|
||||
on_page = f
|
||||
current_page_fields = []
|
||||
pages.append({'field': f, 'fields': current_page_fields})
|
||||
continue
|
||||
|
||||
if f.type == 'title' and on_page and not current_page_fields and on_page.label == f.label:
|
||||
# don't include first title of a page if that title has the
|
||||
# same text as the page.
|
||||
continue
|
||||
|
||||
if f.type in ('title', 'subtitle', 'comment') and f.include_in_summary_page:
|
||||
current_page_fields.append({'field': f})
|
||||
continue
|
||||
|
||||
if not hasattr(f, 'get_view_value'):
|
||||
continue
|
||||
|
||||
if not f.include_in_summary_page:
|
||||
continue
|
||||
|
||||
value, value_details = get_value_info(f)
|
||||
if value is None and not (f.required and include_unset_required_fields):
|
||||
continue
|
||||
|
||||
current_page_fields.append({'field': f, 'value': value, 'value_details': value_details})
|
||||
|
||||
if not pages:
|
||||
fields_and_details = current_page_fields
|
||||
else:
|
||||
# ignore empty pages
|
||||
fields_and_details = []
|
||||
for page in pages:
|
||||
if not any([bool('value' in x) for x in page['fields']]):
|
||||
continue
|
||||
fields_and_details.append(page)
|
||||
fields_and_details.extend([x for x in page['fields']])
|
||||
|
||||
return fields_and_details
|
||||
|
||||
def iter_evolution_parts(self):
|
||||
for evo in self.evolution or []:
|
||||
for part in evo.parts or []:
|
||||
|
|
|
@ -446,72 +446,14 @@ class FormStatusPage(Directory, FormTemplateMixin):
|
|||
|
||||
def display_fields(self, fields=None, form_url='', include_unset_required_fields=False):
|
||||
import wcs.workflows
|
||||
if fields is None:
|
||||
fields = self.formdef.fields
|
||||
|
||||
field_details = self.filled.get_summary_field_details(fields,
|
||||
include_unset_required_fields=include_unset_required_fields)
|
||||
|
||||
r = TemplateIO(html=True)
|
||||
on_page = False
|
||||
on_disabled_page = False
|
||||
pages = []
|
||||
current_page_fields = []
|
||||
|
||||
def get_value_info(f):
|
||||
# return the selected value and an optional dictionary that will be
|
||||
# passed to get_view_value() to provide additional details.
|
||||
value_details = {}
|
||||
if f.id not in self.filled.data:
|
||||
value = None
|
||||
else:
|
||||
if f.store_display_value and ('%s_display' % f.id) in self.filled.data:
|
||||
value = self.filled.data['%s_display' % f.id]
|
||||
value_details['value_id'] = self.filled.data[f.id]
|
||||
else:
|
||||
value = self.filled.data[f.id]
|
||||
|
||||
if value is None or value == '':
|
||||
value = None
|
||||
return (value, value_details)
|
||||
|
||||
for i, f in enumerate(fields):
|
||||
if f.type == 'page':
|
||||
on_page = f
|
||||
current_page_fields = []
|
||||
pages.append({'page': f, 'fields': current_page_fields})
|
||||
continue
|
||||
|
||||
if f.type == 'title' and on_page and not current_page_fields and on_page.label == f.label:
|
||||
# don't include first title of a page if that title has the
|
||||
# same text as the page.
|
||||
continue
|
||||
|
||||
if f.type in ('title', 'subtitle', 'comment') and f.include_in_summary_page:
|
||||
current_page_fields.append({'field': f})
|
||||
continue
|
||||
|
||||
if not hasattr(f, 'get_view_value'):
|
||||
continue
|
||||
|
||||
if not f.include_in_summary_page:
|
||||
continue
|
||||
|
||||
value, value_details = get_value_info(f)
|
||||
if value is None and not (f.required and include_unset_required_fields):
|
||||
continue
|
||||
|
||||
current_page_fields.append({'field': f, 'value': value, 'value_details': value_details})
|
||||
|
||||
if not pages:
|
||||
fields = [x['field'] for x in current_page_fields]
|
||||
else:
|
||||
# ignore empty pages
|
||||
fields = []
|
||||
for page in pages:
|
||||
if not any([bool('value' in x) for x in page['fields']]):
|
||||
continue
|
||||
fields.append(page['page'])
|
||||
fields.extend([x['field'] for x in page['fields']])
|
||||
|
||||
on_page = None
|
||||
for f in fields:
|
||||
for field_value_info in field_details:
|
||||
f = field_value_info['field']
|
||||
if f.type == 'page':
|
||||
if on_page:
|
||||
r += htmltext('</div>')
|
||||
|
@ -541,7 +483,7 @@ class FormStatusPage(Directory, FormTemplateMixin):
|
|||
css_classes.append(f.extra_css_class)
|
||||
r += htmltext('<div class="%s">' % ' '.join(css_classes))
|
||||
r += htmltext('<span class="label">%s</span> ') % f.label
|
||||
value, value_details = get_value_info(f)
|
||||
value, value_details = field_value_info['value'], field_value_info['value_details']
|
||||
if value is None:
|
||||
r += htmltext('<div class="value"><i>%s</i></div>') % _('Not set')
|
||||
else:
|
||||
|
|
Loading…
Reference in New Issue