Skip to content

Commit

Permalink
Fix #1381 Include all question columns in CSV export
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsimpson committed Jul 23, 2024
1 parent aa755cf commit fc5db23
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions subscribie/blueprints/admin/export_subscribers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def export_subscribers():
if len(subscriptions) == 0:
return "You don't have any subscribers yet."
subscribers = []
all_fieldnames = set()
for subscription in subscriptions:
if subscription.person is not None:
person = {
Expand All @@ -26,10 +27,12 @@ def export_subscribers():
# Include subscription question answers in export, if any
if subscription.question_answers:
for answer in subscription.question_answers:
person[
f"question_id-{answer.question_id}-{json.dumps(answer.question_title)}" # noqa: E501
] = json.dumps(answer.response)
field_name = f"question_id-{answer.question_id}-{json.dumps(answer.question_title)}" # noqa: E501
person[field_name] = json.dumps(answer.response)
all_fieldnames.add(field_name)
subscribers.append(person)
# Collect static fieldnames
all_fieldnames.update(person.keys())
else:
logging.info(
f"Excluding subscription {subscription.id} as no person attached"
Expand All @@ -41,7 +44,7 @@ def export_subscribers():

outfile = io.StringIO()
outcsv = csv.DictWriter(
outfile, fieldnames=subscribers[0].keys(), extrasaction="ignore"
outfile, fieldnames=list(all_fieldnames), extrasaction="ignore"
)
outcsv.writeheader()
for subscriber in subscribers:
Expand Down

0 comments on commit fc5db23

Please sign in to comment.