Commit 725037c7 authored by Guillaume Mella's avatar Guillaume Mella
Browse files

new version that fix and improve csv export

parent d550eaa7
Pipeline #47683 failed with stage
in 0 seconds
IMAGE_TAG=0.30-beta IMAGE_TAG=0.30
...@@ -658,7 +658,7 @@ class StarInfo(Base): ...@@ -658,7 +658,7 @@ class StarInfo(Base):
for field_name in field_list: for field_name in field_list:
field_info = field_engine[field_name] field_info = field_engine[field_name]
index = header.index(field_name) index = header.index(field_name)
value = data[index] value = data[index].strip()
# print(field_info, index, "'%s'"%(value)) # print(field_info, index, "'%s'"%(value))
# if we have a parse function # if we have a parse function
...@@ -709,3 +709,20 @@ class StarInfo(Base): ...@@ -709,3 +709,20 @@ class StarInfo(Base):
# and finally retest for uniqueness # and finally retest for uniqueness
self.check_unique() self.check_unique()
def csv_headers():
return [ '"%s"' % field for field in StarInfo.MAINFIELDS ]
def csv_values(self):
ret_cols =[]
fields = StarInfo.FIELDS_DUMP['fields']
for field in StarInfo.MAINFIELDS:
colname=fields[field]['field']
col=self.__getattribute__(colname)
if isinstance(col, str) :
# if col.property.columns[0].type==String:
ret_cols.append('"%s"'%col.strip())
elif col == None:
ret_cols.append("")
else:
ret_cols.append("%s" % col)
return ret_cols
...@@ -15,6 +15,11 @@ ...@@ -15,6 +15,11 @@
<h2>Release notes</h2> <h2>Release notes</h2>
<div> <div>
<ul> <ul>
<li>V.0.30 - July 2020
<ul>
<li>Enhance CSV export endpoint: quote and strip string values, set text/csv header as MIME type.</li>
</ul>
</li>
<li>V.0.29 - April 2020 <li>V.0.29 - April 2020
<ul> <ul>
<li>Small javascript and css resources changes.</li> <li>Small javascript and css resources changes.</li>
......
ID1,ID2,UD_DIAM,LD_DIAM,E_LD_DIAM,BAND,MU_LAMBDA,METHOD,BANDCODE,NOTES,BIBCODE,SINPE{% for star in stars %}{% if star.star_info_entry.0.validated%}{{star.id1 or ''}},{{star.id2 or ''}},{{star.ud_diam or ''}},{{star.ld_diam or ''}},{{star.e_ld_diam or ''}},{{star.band or ''}},{{star.mu_lambda or ''}},{{star.method or ''}},{{star.band_code or ''}},"{{star.notes or ''}}",{{star.bibcode or ''}},"{{star.sinpe or ''}}"{% endif %} {% set comma = joiner(",") %}{% for h in csv_headers %}{{ comma() }}{{ h | safe }}{% endfor %}
{% for star in stars %}{% set comma = joiner(",") %}{% for v in star.csv_values() %}{{ comma() }}{{ v | safe }}{% endfor %}
{% endfor %} {% endfor %}
...@@ -7,10 +7,15 @@ from jmdc_app.models.submission import StarInfoEntry ...@@ -7,10 +7,15 @@ from jmdc_app.models.submission import StarInfoEntry
def jmdc_export_csv(request): def jmdc_export_csv(request):
session = request.dbsession session = request.dbsession
request.response.content_type = 'text/plain' request.response.content_type = 'text/csv header'
# Csv spec ask for CRLF endlines https://tools.ietf.org/html/rfc4180
# putting next env in .ini could be a solution but an AssertError is thrown. should we continue like this?
#jinja2.newline_sequence = "\r\n"
# order by id (creation order / look during import ) and check for validated ones # order by id (creation order / look during import ) and check for validated ones
stars = session.query(StarInfo).filter(StarInfo.star_info_entry.any(StarInfoEntry.validated == True)).order_by( stars = session.query(StarInfo).filter(StarInfo.star_info_entry.any(StarInfoEntry.validated == True)).order_by(
StarInfo.id).all() StarInfo.id).all()
return { 'stars': stars } return { 'csv_headers': StarInfo.csv_headers(), 'stars': stars }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment