Skip to content

Commit 974fbe4

Browse files
committed
Changes based on PR feedback
1 parent 77a442e commit 974fbe4

File tree

5 files changed

+39
-55
lines changed

5 files changed

+39
-55
lines changed

microsetta_private_api/api/_source.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,17 @@ def get_external_report(
212212

213213
with Transaction() as t:
214214
source_repo = SourceRepo(t)
215-
report = source_repo.get_external_report(
215+
reports = source_repo.get_external_reports(
216216
source_id, external_report_id
217217
)
218+
if len(reports) != 1:
219+
return jsonify(code=404, message="Report not found"), 404
220+
221+
report = reports[0]
222+
218223
# Trying to jsonify the actual contents gets ugly, so we return
219224
# everything else here, and the contents in get_external_report_bytes
220225
report.file_contents = ""
221-
222-
if report is None:
223-
return jsonify(code=404, message="Report not found"), 404
224226
return jsonify(report.to_api()), 200
225227

226228

@@ -231,14 +233,15 @@ def get_external_report_bytes(
231233

232234
with Transaction() as t:
233235
source_repo = SourceRepo(t)
234-
report = source_repo.get_external_report(
236+
reports = source_repo.get_external_reports(
235237
source_id, external_report_id
236238
)
237239

238-
if report is None:
240+
if len(reports) != 1:
239241
return jsonify(code=404, message="Report not found"), 404
240242

243+
report = reports[0]
241244
response = make_response(bytes(report.file_contents))
242-
response.headers.set("Content-Type", "application/pdf")
245+
response.headers.set("Content-Type", report.file_type)
243246

244247
return response

microsetta_private_api/db/patches/0133.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
-- is for highly specific documents with a pre-defined language (FFQs from the THDMI Japan project), the approach makes sense.
44

55
-- The report_type value will dictate which section of the My Reports tab it displays under.
6-
-- I'm setting the enum up to reflect current structure (sample = My Kits/ffq = My FFQs) but this could be extended to "Other" or various specifics later.
7-
CREATE TYPE EXTERNAL_REPORT_TYPE AS ENUM ('sample', 'ffq');
6+
-- I'm setting the enum up to reflect current structure (kit = My Kits/ffq = My FFQs) but this could be extended later.
7+
CREATE TYPE EXTERNAL_REPORT_TYPE AS ENUM ('kit', 'ffq');
88
CREATE TABLE ag.external_reports (
99
external_report_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
1010
source_id UUID NOT NULL,

microsetta_private_api/model/external_report.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33

44
class ExternalReport(ModelBase):
55
def __init__(self, **kwargs):
6-
# Minimum requirements
76
self.external_report_id = kwargs['external_report_id']
87
self.source_id = kwargs['source_id']
98
self.report_type = kwargs['report_type']
10-
11-
# Optional parameters
12-
self.file_name = kwargs.get("file_name", "")
13-
self.file_title = kwargs.get("file_title", "")
14-
self.file_type = kwargs.get("file_type", "")
15-
self.file_contents = kwargs.get("file_contents", "")
9+
self.file_name = kwargs['file_name']
10+
self.file_title = kwargs['file_title']
11+
self.file_type = kwargs['file_type']
12+
self.file_contents = kwargs['file_contents']
1613

1714
def to_api(self):
1815
return self.__dict__.copy()

microsetta_private_api/repo/source_repo.py

+17-35
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def check_source_post_overhaul(self, account_id, source_id):
313313
)
314314
return cur.rowcount == 1
315315

316-
def get_external_reports(self, source_id):
316+
def get_external_reports(self, source_id, external_report_id=None):
317317
"""Retrieve list of external reports for a given source, if any
318318
NB: We only extract certain columns because there's no need to waste
319319
resources on the actual file_contents until the user hits the path
@@ -323,45 +323,27 @@ def get_external_reports(self, source_id):
323323
----------
324324
source_id : uuid
325325
The associated source ID
326+
external_report_id : uuid
327+
The ID of a single external report to pull
326328
327329
Returns
328330
-------
329331
List of ExternalReports
330332
"""
331333
with self._transaction.dict_cursor() as cur:
332-
cur.execute(
333-
"SELECT external_report_id, source_id, file_title, "
334-
"report_type "
335-
"FROM ag.external_reports "
336-
"WHERE source_id = %s",
337-
(source_id, )
338-
)
334+
if external_report_id is None:
335+
cur.execute(
336+
"SELECT * "
337+
"FROM ag.external_reports "
338+
"WHERE source_id = %s",
339+
(source_id, )
340+
)
341+
else:
342+
cur.execute(
343+
"SELECT * "
344+
"FROM ag.external_reports "
345+
"WHERE source_id = %s AND external_report_id = %s",
346+
(source_id, external_report_id)
347+
)
339348
rows = cur.fetchall()
340349
return [_row_to_external_report(r) for r in rows]
341-
342-
def get_external_report(self, source_id, external_report_id):
343-
""" Retrieve the full contents of an external report
344-
345-
Parameters
346-
----------
347-
source_id : uuid
348-
The associated source ID
349-
external_report_id : uuid
350-
The external report ID
351-
352-
Returns
353-
-------
354-
ExternalReport object or None if not found
355-
"""
356-
with self._transaction.dict_cursor() as cur:
357-
cur.execute(
358-
"SELECT * "
359-
"FROM ag.external_reports "
360-
"WHERE source_id = %s AND external_report_id = %s",
361-
(source_id, external_report_id)
362-
)
363-
if cur.rowcount == 1:
364-
row = cur.fetchone()
365-
return _row_to_external_report(row)
366-
else:
367-
return None

microsetta_private_api/repo/tests/test_source.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,10 @@ def test_get_external_report(self):
178178
reports = sr.get_external_reports(HUMAN_SOURCE.id)
179179

180180
er = reports[0]
181-
obs = sr.get_external_report(
181+
obs_reports = sr.get_external_reports(
182182
HUMAN_SOURCE.id, er.external_report_id
183183
)
184+
obs = obs_reports[0]
184185

185186
self.assertEqual(obs.source_id, HUMAN_SOURCE.id)
186187
self.assertEqual(obs.file_title, "Test Source Repo")
@@ -191,21 +192,22 @@ def test_get_external_report_fail(self):
191192
reports = sr.get_external_reports(HUMAN_SOURCE.id)
192193

193194
er = reports[0]
194-
obs = sr.get_external_report(
195+
obs_reports = sr.get_external_reports(
195196
"ffffffff-aaaa-cccc-aaaa-aaaaaaaaaaaa", er.external_report_id
196197
)
197198

198-
self.assertEqual(obs, None)
199+
self.assertEqual(len(obs_reports), 0)
199200

200201
def test_get_external_report_bytes(self):
201202
with Transaction() as t:
202203
sr = SourceRepo(t)
203204
reports = sr.get_external_reports(HUMAN_SOURCE.id)
204205

205206
er = reports[0]
206-
obs = sr.get_external_report(
207+
obs_reports = sr.get_external_reports(
207208
HUMAN_SOURCE.id, er.external_report_id
208209
)
210+
obs = obs_reports[0]
209211

210212
act = b'Imagine a full file here'
211213

0 commit comments

Comments
 (0)