4
4
import os
5
5
import urllib .parse
6
6
from pathlib import Path
7
- from typing import Dict , List , Tuple
7
+ from typing import Any , Dict , List , Optional , Tuple
8
8
9
9
from celery .utils .log import get_task_logger
10
10
from django .conf import settings
@@ -100,7 +100,7 @@ def process_file_transfer(auth_token, job_transfer_id):
100
100
101
101
def validate_file_transfer_files (
102
102
request ,
103
- ) -> Tuple [Dict [str , str ], List [Path ], List [Path ]]:
103
+ ) -> Tuple [Optional [ Dict [str , str ]], Optional [ List [Path ]], Optional [ List [Path ] ]]:
104
104
"""Check the request and return a list of proteins and/or computed molecule file
105
105
path references (paths relative to the media directory).
106
106
@@ -121,9 +121,9 @@ def validate_file_transfer_files(
121
121
list of validated computed molecules (ComputedMolecule)
122
122
"""
123
123
124
- logger .info ('+ Validating file transfer files...' )
124
+ target_id = request .data ['target' ]
125
+ logger .info ('+ Validating file transfer files ()...' )
125
126
126
- error : Dict [str , str ] = {}
127
127
protein_files : List [Path ] = []
128
128
compound_files : List [Path ] = []
129
129
@@ -133,21 +133,24 @@ def validate_file_transfer_files(
133
133
p .strip () for p in request .data ['proteins' ].split (',' )
134
134
]
135
135
for protein_path_and_file in protein_paths_and_files :
136
- # It's a filename
137
136
if protein_path_and_file .endswith ('_apo-desolv.pdb' ):
138
- if SiteObservation .objects .filter (
139
- apo_desolv_file = protein_path_and_file
140
- ).first ():
137
+ if not (
138
+ s_ob := SiteObservation .objects .filter (
139
+ apo_desolv_file = protein_path_and_file
140
+ ).first ()
141
+ ):
142
+ return tfr_validation_error (
143
+ f'Unknown Protein: { protein_path_and_file } ' ,
144
+ status .HTTP_404_NOT_FOUND ,
145
+ )
146
+
147
+ if s_ob .experiment .experiment_upload .target .id == target_id :
141
148
protein_files .append (Path (protein_path_and_file ))
142
149
else :
143
- error ['message' ] = f'Unknown Protein: { protein_path_and_file } '
144
- error ['status' ] = status .HTTP_404_NOT_FOUND
145
- return error , protein_files , compound_files
146
-
147
- if not protein_files :
148
- error ['message' ] = 'API expects a list of comma-separated protein codes'
149
- error ['status' ] = status .HTTP_404_NOT_FOUND
150
- return error , protein_files , compound_files
150
+ return tfr_validation_error (
151
+ f'Protein does not belong to Target: { protein_path_and_file } ' ,
152
+ status .HTTP_400_BAD_REQUEST ,
153
+ )
151
154
152
155
logger .info (
153
156
"+ Validated proteins (SiteObservations) [%d]" ,
@@ -159,36 +162,43 @@ def validate_file_transfer_files(
159
162
p .strip () for p in request .data ['compounds' ].split (',' )
160
163
]
161
164
for compound_path_and_file in compound_paths_and_files :
162
- if SiteObservation .objects .filter (
165
+ if not SiteObservation .objects .filter (
163
166
ligand_mol = compound_path_and_file
164
167
).first ():
168
+ return tfr_validation_error (
169
+ f'Unknown Compound: { compound_path_and_file } ' ,
170
+ status .HTTP_404_NOT_FOUND ,
171
+ )
172
+
173
+ if s_ob .experiment .experiment_upload .target .id == target_id :
165
174
compound_files .append (Path (compound_path_and_file ))
166
175
else :
167
- error ['message' ] = f'Unknown Compound: { compound_path_and_file } '
168
- error ['status' ] = status .HTTP_404_NOT_FOUND
169
- return error , protein_files , compound_files
170
-
171
- if not compound_files :
172
- error ['message' ] = 'API expects a list of comma-separated compound names'
173
- error ['status' ] = status .HTTP_400_BAD_REQUEST
174
- return error , protein_files , compound_files
176
+ return tfr_validation_error (
177
+ f'Compound does not belong to Target: { compound_path_and_file } ' ,
178
+ status .HTTP_400_BAD_REQUEST ,
179
+ )
175
180
176
181
logger .info (
177
182
"+ Validated compounds (SiteObservations) [%d]" ,
178
183
len (compound_files ),
179
184
)
180
185
181
- if protein_files or compound_files :
182
- logger .info (
183
- "- Validated file transfer files (%d, %d)" ,
184
- len (protein_files ),
185
- len (compound_files ),
186
+ if not protein_files and not compound_files :
187
+ return tfr_validation_error (
188
+ 'A valid set of protein codes and/or a list of valid compound names must be provided' ,
189
+ status .HTTP_400_BAD_REQUEST ,
186
190
)
187
- return error , protein_files , compound_files
188
191
189
- error ['message' ] = (
190
- 'A valid set of protein codes and/or a list of valid'
191
- ' compound names must be provided'
192
+ logger .info (
193
+ "- Validated file transfer files (%d, %d)" ,
194
+ len (protein_files ),
195
+ len (compound_files ),
192
196
)
193
- error ['status' ] = status .HTTP_400_BAD_REQUEST
194
- return error , protein_files , compound_files
197
+ return None , protein_files , compound_files
198
+
199
+
200
+ def tfr_validation_error (
201
+ error : str , status_code : int
202
+ ) -> Tuple [Dict [str , Any ], None , None ]:
203
+ """Returns the error and HTTP status code as a tuple for a response."""
204
+ return {'message' : error , 'status' : status_code }, None , None
0 commit comments