Skip to content

Commit e8e53c4

Browse files
committed
Include instruments in barcode
1 parent 3a67995 commit e8e53c4

File tree

3 files changed

+90
-4
lines changed

3 files changed

+90
-4
lines changed

config.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,13 @@
1111
},
1212
"db": { "pool": 3, "overflow": 6 },
1313
"ispyb_api": "http://127.0.0.1:8060/api",
14-
"frontend_url": "http://localtest.diamond.ac.uk:9000"
14+
"frontend_url": "http://localtest.diamond.ac.uk:9000",
15+
"alerts": {
16+
"contact_email": "pato@diamond.ac.uk",
17+
"smtp_port": 8025,
18+
"smtp_server": "mail.smtpbucket.com",
19+
"local_contacts": {
20+
"Mr Matt Spink": "bar@diamond.ac.uk"
21+
}
22+
}
1523
}

src/scaup/crud/top_level_containers.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from typing import Any
2+
13
from fastapi import HTTPException, status
24
from lims_utils.logging import app_logger
35
from lims_utils.models import Paged
@@ -125,7 +127,29 @@ def create_top_level_container(shipmentId: int | None, params: TopLevelContainer
125127
)
126128

127129
if proposal:
128-
bar_code = f"{proposal.reference}-{proposal.visitNumber}-{container.id:07}"
130+
# This is required because the dewar logistics server expects an instrument in the barcode in order to match
131+
# a dewar to the correct instrument
132+
ext_resp = ExternalRequest.request(
133+
token=token,
134+
url=f"/proposals/{proposal.reference}/sessions/{proposal.visitNumber}",
135+
)
136+
137+
if ext_resp.status_code != 200:
138+
app_logger.warning(
139+
"Error from Expeye while getting session %s-%i: %s",
140+
proposal.reference,
141+
proposal.visitNumber,
142+
ext_resp.text,
143+
)
144+
raise HTTPException(
145+
status_code=status.HTTP_424_FAILED_DEPENDENCY,
146+
detail="Invalid response while creating top level container in ISPyB",
147+
)
148+
149+
session_json: dict[str, Any] = ext_resp.json()
150+
instrument = "-" if (i := session_json.get("beamLineName")) is None else f"-{i}-"
151+
152+
bar_code = f"{proposal.reference}-{proposal.visitNumber}{instrument}{container.id:07}"
129153

130154
# This is because some users expect a sequential numeric ID to make tracking how old a dewar is easier,
131155
# and because of historical reasons, some users are used to seeing the proposal/session number on there

tests/shipments/top_level_containers/test_create.py

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ def test_create_no_name(client):
4141

4242

4343
@responses.activate
44-
def test_create_auto_barcode(client):
45-
"""Should automatically generate barcode if not provided in request"""
44+
def test_create_auto_barcode_no_instrument(client):
45+
"""Should automatically generate barcode if not provided in request, and instrument isn't returned from ISPyB"""
4646

4747
resp = client.post(
4848
"/shipments/1/topLevelContainers",
@@ -60,6 +60,60 @@ def test_create_auto_barcode(client):
6060
)
6161

6262

63+
@pytest.mark.noregister
64+
@responses.activate
65+
def test_create_auto_barcode_with_instrument(client):
66+
"""Should automatically generate barcode if not provided in request"""
67+
responses.get(
68+
f"{Config.ispyb_api.url}/proposals/cm1/sessions/1",
69+
status=200,
70+
json={"beamLineName": "m02"},
71+
)
72+
73+
responses.get(
74+
f"{Config.ispyb_api.url}/proposals/cm1/dewar-registry/DLS-EM-0001",
75+
status=200,
76+
json={},
77+
)
78+
79+
resp = client.post(
80+
"/shipments/1/topLevelContainers",
81+
json={
82+
"type": "dewar",
83+
"code": "DLS-EM-0001",
84+
},
85+
)
86+
87+
assert resp.status_code == 201
88+
new_tlc = resp.json()["id"]
89+
assert "cm1-1-m02-" in inner_db.session.scalar(
90+
select(TopLevelContainer.barCode).filter(TopLevelContainer.id == new_tlc)
91+
)
92+
93+
94+
@pytest.mark.noregister
95+
@responses.activate
96+
def test_create_auto_barcode_upstream_fail(client):
97+
"""Should raise exception if request fails upstream when generating barcode"""
98+
responses.get(f"{Config.ispyb_api.url}/proposals/cm1/sessions/1", status=500)
99+
100+
responses.get(
101+
f"{Config.ispyb_api.url}/proposals/cm1/dewar-registry/DLS-EM-0001",
102+
status=200,
103+
json={},
104+
)
105+
106+
resp = client.post(
107+
"/shipments/1/topLevelContainers",
108+
json={
109+
"type": "dewar",
110+
"code": "DLS-EM-0001",
111+
},
112+
)
113+
114+
assert resp.status_code == 424
115+
116+
63117
@responses.activate
64118
def test_create_duplicate_name(client):
65119
"""Should not allow creation if name is already present in shipment"""

0 commit comments

Comments
 (0)