Skip to content

Commit

Permalink
add proper headers
Browse files Browse the repository at this point in the history
  • Loading branch information
zxenia committed Nov 30, 2020
1 parent 6d733da commit 04640cf
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions chord_metadata_service/restapi/api_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,14 @@ def render(self, data, media_type=None, renderer_context=None):
for individual in data['results']:
ind_obj = {
'id': individual['id'],
'sex': individual['sex'],
'sex': individual.get('sex', None),
'date_of_birth': individual.get('date_of_birth', None),
'taxonomy': None,
'karyotypic_sex': individual['karyotypic_sex'],
'race': individual.get('race', None),
'ethnicity': individual.get('ethnicity', None),
'age': None,
'diseases': None,
'created': individual['created'],
'updated': individual['updated']
}
Expand Down Expand Up @@ -128,11 +130,12 @@ def render(self, data, media_type=None, renderer_context=None):
if all_diseases:
ind_obj['diseases'] = '; '.join(all_diseases)
individuals.append(ind_obj)

columns = individuals[0].keys()
# remove underscore and capitalize column names
headers = {key: key.replace('_', ' ').capitalize() for key in individuals[0].keys()}
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = "attachment; filename='export.csv'"
dict_writer = csv.DictWriter(response, columns)
dict_writer.writeheader()
dict_writer = csv.DictWriter(response, fieldnames=columns)
dict_writer.writerow(headers)
dict_writer.writerows(individuals)
return response

0 comments on commit 04640cf

Please sign in to comment.