Skip to content

Commit

Permalink
Implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
folix-01 committed Feb 19, 2025
1 parent bd82db5 commit 238ede7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 21 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Changelog
3.2.2 (unreleased)
------------------

- Nothing changed yet.
- Use confugured forms fields as CSV export columns.
[folix-01]


3.2.1 (2025-01-09)
Expand Down
22 changes: 2 additions & 20 deletions src/collective/volto/formsupport/restapi/services/form_data/csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,6 @@ def __init__(self, context, request):
field_id = field["field_id"]
self.form_fields_order.append(field_id)

def get_ordered_keys(self, record):
"""
We need this method because we want to maintain the fields order set in the form.
The form can also change during time, and each record can have different fields stored in it.
"""
record_order = record.attrs.get("fields_order", [])
if record_order:
return record_order
order = []
# first add the keys that are currently in the form
for k in self.form_fields_order:
if k in record.attrs:
order.append(k)
# finally append the keys stored in the record but that are not in the form (maybe the form changed during time)
for k in record.attrs.keys():
if k not in order and k not in SKIP_ATTRS:
order.append(k)
return order

def render(self):
self.check_permission()

Expand All @@ -74,7 +55,7 @@ def get_data(self):
for item in store.search():
data = {}
fields_labels = self.get_fields_labels(item)
for k in self.get_ordered_keys(item):
for k in self.form_fields_order:
if k in SKIP_ATTRS:
continue
value = item.attrs.get(k, None)
Expand All @@ -87,6 +68,7 @@ def get_data(self):
value = item.attrs.get(k, None)
data[k] = json_compatible(value)
rows.append(data)

columns.extend(fixed_columns)
writer = csv.DictWriter(sbuf, fieldnames=columns, quoting=csv.QUOTE_ALL)
writer.writeheader()
Expand Down

0 comments on commit 238ede7

Please sign in to comment.