11from fastapi import HTTPException , status
22from lims_utils .logging import app_logger
33from lims_utils .models import Paged
4- from sqlalchemy import func , insert , select
4+ from sqlalchemy import func , insert , select , update
55
66from ..models .inner_db .tables import Shipment , TopLevelContainer
77from ..models .top_level_containers import (
@@ -59,6 +59,17 @@ def _check_fields(
5959@assert_not_booked
6060@retry_if_exists
6161def create_top_level_container (shipmentId : int | None , params : TopLevelContainerIn , token : str , autocreate = True ):
62+ proposal = (
63+ None
64+ if shipmentId is None
65+ else inner_db .session .execute (
66+ select (
67+ func .concat (Shipment .proposalCode , Shipment .proposalNumber ).label ("reference" ),
68+ Shipment .visitNumber ,
69+ ).filter (Shipment .id == shipmentId )
70+ ).one ()
71+ )
72+
6273 if params .code :
6374 _check_fields (params , token , shipmentId )
6475 elif params .type == "dewar" and autocreate :
@@ -84,28 +95,25 @@ def create_top_level_container(shipmentId: int | None, params: TopLevelContainer
8495
8596 new_code = f"DLS-BI-{ dewar_number :04} "
8697
87- proposal_reference = inner_db .session .scalar (
88- select (func .concat (Shipment .proposalCode , Shipment .proposalNumber )).filter (Shipment .id == shipmentId )
89- )
90-
91- ext_resp = ExternalRequest .request (
92- Config .ispyb_api .jwt ,
93- method = "POST" ,
94- url = f"/proposals/{ proposal_reference } /dewar-registry" ,
95- json = {"facilityCode" : new_code },
96- )
97-
98- if ext_resp .status_code != 201 :
99- app_logger .warning (
100- "Error from Expeye while creating dewar registry entry with code %s: %s" ,
101- new_code ,
102- ext_resp .text ,
103- )
104- raise HTTPException (
105- status_code = status .HTTP_424_FAILED_DEPENDENCY ,
106- detail = "Invalid response while creating top level container in ISPyB" ,
98+ if proposal :
99+ ext_resp = ExternalRequest .request (
100+ Config .ispyb_api .jwt ,
101+ method = "POST" ,
102+ url = f"/proposals/{ proposal .reference } /dewar-registry" ,
103+ json = {"facilityCode" : new_code },
107104 )
108105
106+ if ext_resp .status_code != 201 :
107+ app_logger .warning (
108+ "Error from Expeye while creating dewar registry entry with code %s: %s" ,
109+ new_code ,
110+ ext_resp .text ,
111+ )
112+ raise HTTPException (
113+ status_code = status .HTTP_424_FAILED_DEPENDENCY ,
114+ detail = "Invalid response while creating top level container in ISPyB" ,
115+ )
116+
109117 params .code = new_code
110118
111119 if not params .name :
@@ -116,6 +124,17 @@ def create_top_level_container(shipmentId: int | None, params: TopLevelContainer
116124 {"shipmentId" : shipmentId , ** params .model_dump (exclude_unset = True )},
117125 )
118126
127+ if proposal :
128+ bar_code = f"{ proposal .reference } -{ proposal .visitNumber } -{ container .id :07} "
129+
130+ # This is because some users expect a sequential numeric ID to make tracking how old a dewar is easier,
131+ # and because of historical reasons, some users are used to seeing the proposal/session number on there
132+ # as well, so I have to create barcodes this way.
133+ container = inner_db .session .scalar (
134+ update (TopLevelContainer ).returning (TopLevelContainer ).filter (TopLevelContainer .id == container .id ),
135+ {"barCode" : bar_code },
136+ )
137+
119138 inner_db .session .commit ()
120139 return container
121140
0 commit comments