1717import json
1818
1919from requests .exceptions import SSLError
20- from utils import build_url , handle_response
20+ from labkey . utils import build_url , handle_response
2121
2222
2323# EXAMPLE
@@ -91,14 +91,39 @@ def save_batch(assay_id, batch, server_context):
9191 :param server_context: A LabKey server context. See utils.create_server_context.
9292 :return:
9393 """
94- if not isinstance (batch , Batch ):
95- raise Exception ('save_batch() "batch" expected to be instance of Batch' )
94+ result = save_batches (assay_id ,[batch ],server_context )
95+
96+ if result is not None :
97+ return result [0 ]
98+ else :
99+ return None
100+
101+
102+ def save_batches (assay_id , batches , server_context ):
103+ """
104+ Saves a modified batches.
105+ :param assay_id: The assay protocol id.
106+ :param batch: The Batch(es) to save.
107+ :param server_context: A LabKey server context. See utils.create_server_context.
108+ :return:
109+ """
96110
97111 save_batch_url = build_url ('assay' , 'saveAssayBatch.api' , server_context )
98112 session = server_context ['session' ]
113+
114+ json_batches = []
115+ if batches is None :
116+ return None # Nothing to save
117+
118+ for batch in batches :
119+ if isinstance (batch , Batch ):
120+ json_batches .append (batch .to_json ())
121+ else :
122+ raise Exception ('save_batch() "batches" expected to be a set Batch instances' )
123+
99124 payload = {
100125 'assayId' : assay_id ,
101- 'batches' : [ batch . to_json ()]
126+ 'batches' : json_batches
102127 }
103128 headers = {
104129 'Content-type' : 'application/json' ,
@@ -110,7 +135,8 @@ def save_batch(assay_id, batch, server_context):
110135 response = session .post (save_batch_url , data = json .dumps (payload ), headers = headers )
111136 json_body = handle_response (response )
112137 if json_body is not None :
113- return Batch .from_data (json_body ['batch' ])
138+ resp_batches = json_body ['batches' ]
139+ return [Batch .from_data (resp_batch ) for resp_batch in resp_batches ]
114140 except SSLError as e :
115141 raise Exception ("Failed to match server SSL configuration. Failed to save batch." )
116142
@@ -132,7 +158,7 @@ def __init__(self, **kwargs):
132158
133159 def to_json (self ):
134160 data = {
135- 'id' : self .id ,
161+ # 'id': self.id,
136162 'lsid' : self .lsid ,
137163 'comment' : self .comment ,
138164 'name' : self .name ,
@@ -169,6 +195,8 @@ def to_json(self):
169195
170196 data = super (Batch , self ).to_json ()
171197
198+ data ['batchProtocolId' ] = self .batch_protocol_id
199+
172200 json_runs = []
173201 for run in self .runs :
174202 json_runs .append (run .to_json ())
@@ -212,7 +240,6 @@ def from_data(data):
212240
213241 def to_json (self ):
214242 data = super (Run , self ).to_json ()
215-
216243 data ['dataInputs' ] = self .data_inputs
217244 data ['dataRows' ] = self .data_rows
218245 data ['experiments' ] = self .experiments
0 commit comments