Skip to content

Commit 245220b

Browse files
committed
fix(registration): fix CSV export mismatch between data and header
1 parent e0d23b8 commit 245220b

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

backend/usecase/registration_usecase.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import csv
22
import os
33
import tempfile
4+
from datetime import datetime
45
from http import HTTPStatus
56
from typing import List, Union
67

@@ -13,6 +14,7 @@
1314
from model.konfhub.konfhub import KonfHubCaptureRegistrationIn, RegistrationDetail
1415
from model.registrations.registration import (
1516
PreRegistrationToRegistrationIn,
17+
Registration,
1618
RegistrationIn,
1719
RegistrationOut,
1820
)
@@ -387,24 +389,37 @@ def get_registration_csv(self, event_id: str) -> FileDownloadOut:
387389
if status != HTTPStatus.OK:
388390
return JSONResponse(status_code=status, content={'message': message})
389391

392+
if not registrations:
393+
return JSONResponse(
394+
status_code=HTTPStatus.NOT_FOUND, content={'message': 'No registrations found for this event'}
395+
)
396+
390397
try:
391398
with tempfile.TemporaryDirectory() as tmpdir:
392399
csv_path = os.path.join(tmpdir, 'registrations.csv')
393400

394401
with open(csv_path, 'w') as temp:
395402
writer = csv.writer(temp)
396403

397-
# make the first row csv for the keys using the dict keys of the first entry
398-
first_entry = self.__convert_data_entry_to_dict(registrations[0])
399-
writer.writerow(first_entry.keys())
404+
# Get headers from Registration DynamoDB model attributes
405+
headers = [
406+
attr_name
407+
for attr_name in dir(Registration)
408+
if not attr_name.startswith('_')
409+
and not callable(getattr(Registration, attr_name))
410+
and attr_name not in ['Meta', 'DoesNotExist', 'registrationIdGSI', 'emailLSI']
411+
]
412+
headers.sort()
413+
logger.info(f'Headers for CSV export: {headers}')
414+
writer.writerow(headers)
400415

401-
# the remaining rows consist of the values of the attributes
402416
for entry in registrations:
403417
entry_dict = self.__convert_data_entry_to_dict(entry)
404-
writer.writerow(entry_dict.values())
418+
row_values = [entry_dict.get(header, '') for header in headers]
419+
writer.writerow(row_values)
405420

406421
# upload the file to s3
407-
csv_object_key = f'csv/registrations/{event_id}.csv'
422+
csv_object_key = f'csv/registrations/{event_id}-{datetime.now().strftime("%Y%m%d%H%M%S")}.csv'
408423
self.__file_s3_usecase.upload_file(file_name=csv_path, object_name=csv_object_key)
409424

410425
return self.__file_s3_usecase.create_download_url(csv_object_key)

0 commit comments

Comments
 (0)