fixing lineterminator in csv

This commit is contained in:
Cayo Puigdefabregas 2020-12-21 16:09:30 +01:00
parent d89dbe8c29
commit 96540d701c
2 changed files with 13 additions and 6 deletions

View File

@ -117,7 +117,7 @@ class DevicesDocumentView(DeviceView):
def generate_post_csv(self, query): def generate_post_csv(self, query):
"""Get device query and put information in csv format.""" """Get device query and put information in csv format."""
data = StringIO() data = StringIO()
cw = csv.writer(data, delimiter=';', quotechar='"') cw = csv.writer(data, delimiter=';', lineterminator="\n", quotechar='"')
first = True first = True
for device in query: for device in query:
d = DeviceRow(device) d = DeviceRow(device)
@ -126,8 +126,8 @@ class DevicesDocumentView(DeviceView):
first = False first = False
cw.writerow(d.values()) cw.writerow(d.values())
bfile = data.getvalue().encode('utf-8') bfile = data.getvalue().encode('utf-8')
insert_hash(bfile)
output = make_response(bfile) output = make_response(bfile)
insert_hash(bfile)
output.headers['Content-Disposition'] = 'attachment; filename=export.csv' output.headers['Content-Disposition'] = 'attachment; filename=export.csv'
output.headers['Content-type'] = 'text/csv' output.headers['Content-type'] = 'text/csv'
return output return output
@ -197,10 +197,10 @@ class CheckView(View):
def get(self): def get(self):
qry = dict(request.values) qry = dict(request.values)
hash3 = qry['hash'] hash3 = qry.get('hash')
result = False result = False
if ReportHash.query.filter_by(hash3=hash3).count(): if hash3 and ReportHash.query.filter_by(hash3=hash3).count():
result = True result = True
return jsonify(result) return jsonify(result)

View File

@ -135,7 +135,7 @@ def test_export_basic_snapshot(user: UserClient):
@pytest.mark.mvp @pytest.mark.mvp
@pytest.mark.usefixtures(conftest.app_context.__name__) @pytest.mark.usefixtures(conftest.app_context.__name__)
def test_check_insert_hash(app: Devicehub, user: UserClient): def test_check_insert_hash(app: Devicehub, user: UserClient, client: Client):
"""Test export device information in a csv file.""" """Test export device information in a csv file."""
snapshot, _ = user.post(file('basic.snapshot'), res=Snapshot) snapshot, _ = user.post(file('basic.snapshot'), res=Snapshot)
csv_str, _ = user.get(res=documents.DocumentDef.t, csv_str, _ = user.get(res=documents.DocumentDef.t,
@ -144,10 +144,17 @@ def test_check_insert_hash(app: Devicehub, user: UserClient):
query=[('filter', {'type': ['Computer']})]) query=[('filter', {'type': ['Computer']})])
hash3 = hashlib.sha3_256(csv_str.encode('utf-8')).hexdigest() hash3 = hashlib.sha3_256(csv_str.encode('utf-8')).hexdigest()
assert ReportHash.query.filter_by(hash3=hash3).count() == 1 assert ReportHash.query.filter_by(hash3=hash3).count() == 1
result, status = user.get(res=documents.DocumentDef.t, item='check/', query=[('hash', hash3)]) result, status = client.get(res=documents.DocumentDef.t, item='check/', query=[('hash', hash3)])
assert status.status_code == 200 assert status.status_code == 200
assert result == True assert result == True
ff = open('/tmp/test.csv', 'w')
ff.write(csv_str)
ff.close()
a= open('/tmp/test.csv').read()
assert hash3 == hashlib.sha3_256(a.encode('utf-8')).hexdigest()
@pytest.mark.mvp @pytest.mark.mvp
def test_export_extended(app: Devicehub, user: UserClient): def test_export_extended(app: Devicehub, user: UserClient):