|
11 | 11 | from qiita_core.exceptions import IncompetentQiitaDeveloperError
|
12 | 12 | from qiita_core.util import execute_as_transaction
|
13 | 13 | from qiita_core.qiita_settings import r_client
|
| 14 | +from qiita_db.artifact import Artifact |
| 15 | +from qiita_db.sql_connection import TRN |
14 | 16 | from qiita_db.user import User
|
15 | 17 | from qiita_db.study import Study
|
16 | 18 | from qiita_db.exceptions import QiitaDBColumnError, QiitaDBLookupError
|
@@ -114,8 +116,8 @@ def study_get_req(study_id, user_id):
|
114 | 116 | study_info['has_access_to_raw_data'] = study.has_access(
|
115 | 117 | User(user_id), True) or study.public_raw_download
|
116 | 118 |
|
117 |
| - study_info['show_biom_download_button'] = 'BIOM' in [ |
118 |
| - a.artifact_type for a in study.artifacts()] |
| 119 | + study_info['show_biom_download_button'] = len( |
| 120 | + study.artifacts(artifact_type='BIOM')) != 0 |
119 | 121 | study_info['show_raw_download_button'] = any([
|
120 | 122 | True for pt in study.prep_templates() if pt.artifact is not None])
|
121 | 123 |
|
@@ -201,53 +203,71 @@ def study_prep_get_req(study_id, user_id):
|
201 | 203 | access_error = check_access(study_id, user_id)
|
202 | 204 | if access_error:
|
203 | 205 | return access_error
|
204 |
| - # Can only pass ids over API, so need to instantiate object |
| 206 | + |
205 | 207 | study = Study(int(study_id))
|
206 |
| - prep_info = defaultdict(list) |
| 208 | + prep_info = {dtype: [] for dtype in study.data_types} |
207 | 209 | editable = study.can_edit(User(user_id))
|
208 |
| - for dtype in study.data_types: |
209 |
| - dtype_infos = list() |
210 |
| - for prep in study.prep_templates(dtype): |
211 |
| - if prep.status != 'public' and not editable: |
| 210 | + with TRN: |
| 211 | + sql = """SELECT prep_template_id, pt.name as name, data_type, |
| 212 | + artifact_id, |
| 213 | + creation_timestamp, modification_timestamp, visibility, |
| 214 | + (SELECT COUNT(sample_id) |
| 215 | + FROM qiita.prep_template_sample |
| 216 | + WHERE prep_template_id = spt.prep_template_id) |
| 217 | + as total_samples, |
| 218 | + (SELECT COUNT(sample_id) |
| 219 | + FROM qiita.prep_template_sample |
| 220 | + WHERE prep_template_id = spt.prep_template_id |
| 221 | + AND ebi_experiment_accession != '') |
| 222 | + as ebi_experiment |
| 223 | + FROM qiita.study_prep_template spt |
| 224 | + LEFT JOIN qiita.prep_template pt USING (prep_template_id) |
| 225 | + LEFT JOIN qiita.data_type USING (data_type_id) |
| 226 | + LEFT JOIN qiita.artifact USING (artifact_id) |
| 227 | + LEFT JOIN qiita.visibility USING (visibility_id) |
| 228 | + WHERE study_id = %s |
| 229 | + GROUP BY prep_template_id, pt.name, data_type, artifact_id, |
| 230 | + creation_timestamp, modification_timestamp, |
| 231 | + visibility |
| 232 | + ORDER BY creation_timestamp""" |
| 233 | + |
| 234 | + TRN.add(sql, [study_id]) |
| 235 | + for row in TRN.execute_fetchindex(): |
| 236 | + row = dict(row) |
| 237 | + if row['visibility'] != 'public' and not editable: |
212 | 238 | continue
|
213 |
| - start_artifact = prep.artifact |
| 239 | + # for those preps that have no artifact |
| 240 | + if row['visibility'] is None: |
| 241 | + row['visibility'] = 'sandbox' |
| 242 | + |
214 | 243 | info = {
|
215 |
| - 'name': prep.name, |
216 |
| - 'id': prep.id, |
217 |
| - 'status': prep.status, |
218 |
| - 'total_samples': len(prep), |
219 |
| - 'creation_timestamp': prep.creation_timestamp, |
220 |
| - 'modification_timestamp': prep.modification_timestamp |
| 244 | + 'name': row['name'], |
| 245 | + 'id': row['prep_template_id'], |
| 246 | + 'status': row['visibility'], |
| 247 | + 'total_samples': row['total_samples'], |
| 248 | + 'creation_timestamp': row['creation_timestamp'], |
| 249 | + 'modification_timestamp': row['modification_timestamp'], |
| 250 | + 'start_artifact': None, |
| 251 | + 'start_artifact_id': None, |
| 252 | + 'youngest_artifact': None, |
| 253 | + 'num_artifact_children': 0, |
| 254 | + 'youngest_artifact_name': None, |
| 255 | + 'youngest_artifact_type': None, |
| 256 | + 'ebi_experiment': row['ebi_experiment'] |
221 | 257 | }
|
222 |
| - if start_artifact is not None: |
223 |
| - youngest_artifact = prep.artifact.youngest_artifact |
| 258 | + if row['artifact_id'] is not None: |
| 259 | + start_artifact = Artifact(row['artifact_id']) |
| 260 | + youngest_artifact = start_artifact.youngest_artifact |
224 | 261 | info['start_artifact'] = start_artifact.artifact_type
|
225 |
| - info['start_artifact_id'] = start_artifact.id |
| 262 | + info['start_artifact_id'] = row['artifact_id'] |
226 | 263 | info['num_artifact_children'] = len(start_artifact.children)
|
227 | 264 | info['youngest_artifact_name'] = youngest_artifact.name
|
228 | 265 | info['youngest_artifact_type'] = \
|
229 | 266 | youngest_artifact.artifact_type
|
230 | 267 | info['youngest_artifact'] = '%s - %s' % (
|
231 | 268 | youngest_artifact.name, youngest_artifact.artifact_type)
|
232 |
| - info['ebi_experiment'] = len( |
233 |
| - [v for _, v in prep.ebi_experiment_accessions.items() |
234 |
| - if v is not None]) |
235 |
| - else: |
236 |
| - info['start_artifact'] = None |
237 |
| - info['start_artifact_id'] = None |
238 |
| - info['youngest_artifact'] = None |
239 |
| - info['num_artifact_children'] = 0 |
240 |
| - info['youngest_artifact_name'] = None |
241 |
| - info['youngest_artifact_type'] = None |
242 |
| - info['ebi_experiment'] = 0 |
243 |
| - |
244 |
| - dtype_infos.append(info) |
245 |
| - |
246 |
| - # default sort is in ascending order of creation timestamp |
247 |
| - sorted_info = sorted(dtype_infos, |
248 |
| - key=lambda k: k['creation_timestamp'], |
249 |
| - reverse=False) |
250 |
| - prep_info[dtype] = sorted_info |
| 269 | + |
| 270 | + prep_info[row['data_type']].append(info) |
251 | 271 |
|
252 | 272 | return {'status': 'success',
|
253 | 273 | 'message': '',
|
|
0 commit comments