Skip to content

Commit 064d35a

Browse files
josenavasantgonza
authored andcommitted
Fix #2276 (#2294)
* Fix #2276 * Factoring out generate nginx directory file list * Factoring out the nginx file list writing * Factoring out generating the file list of an artifact * Factoring out the header setting * Addressing @antgonza's comment * Addressing @wasade's comments
1 parent 33054b2 commit 064d35a

File tree

4 files changed

+262
-146
lines changed

4 files changed

+262
-146
lines changed

qiita_db/test/test_util.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,16 @@ def test_get_timeseries_types(self):
622622
[10, 'mixed', 'combo intervention']]
623623
self.assertEqual(obs, exp)
624624

625+
def test_get_filepath_information(self):
626+
obs = qdb.util.get_filepath_information(1)
627+
# This path is machine specific. Just checking that is not empty
628+
self.assertIsNotNone(obs.pop('fullpath'))
629+
exp = {'filepath_id': 1L, 'filepath': '1_s_G1_L001_sequences.fastq.gz',
630+
'filepath_type': 'raw_forward_seqs', 'checksum': '852952723',
631+
'data_type': 'raw_data', 'mountpoint': 'raw_data',
632+
'subdirectory': False, 'active': True}
633+
self.assertEqual(obs, exp)
634+
625635
def test_filepath_id_to_rel_path(self):
626636
obs = qdb.util.filepath_id_to_rel_path(1)
627637
exp = 'raw_data/1_s_G1_L001_sequences.fastq.gz'

qiita_db/util.py

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,33 @@ def str_to_id(x):
649649
chain.from_iterable(qdb.sql_connection.TRN.execute()[idx:])))
650650

651651

652+
def _path_builder(db_dir, filepath, mountpoint, subdirectory, obj_id):
653+
"""Builds the path of a DB stored file
654+
655+
Parameters
656+
----------
657+
db_dir : str
658+
The DB base dir
659+
filepath : str
660+
The path stored in the DB
661+
mountpoint : str
662+
The mountpoint of the given file
663+
subdirectory : bool
664+
Whether the file is stored in a subdirectory in the mountpoint or not
665+
obj_id : int
666+
The id of the object to which the file is attached
667+
668+
Returns
669+
-------
670+
str
671+
The full path of the given file
672+
"""
673+
if subdirectory:
674+
return join(db_dir, mountpoint, str(obj_id), filepath)
675+
else:
676+
return join(db_dir, mountpoint, filepath)
677+
678+
652679
def retrieve_filepaths(obj_fp_table, obj_id_column, obj_id, sort=None,
653680
fp_type=None):
654681
"""Retrieves the filepaths for the given object id
@@ -674,12 +701,6 @@ def retrieve_filepaths(obj_fp_table, obj_id_column, obj_id, sort=None,
674701
object id
675702
"""
676703

677-
def path_builder(db_dir, filepath, mountpoint, subdirectory, obj_id):
678-
if subdirectory:
679-
return join(db_dir, mountpoint, str(obj_id), filepath)
680-
else:
681-
return join(db_dir, mountpoint, filepath)
682-
683704
sql_sort = ""
684705
if sort == 'ascending':
685706
sql_sort = " ORDER BY filepath_id"
@@ -710,7 +731,7 @@ def path_builder(db_dir, filepath, mountpoint, subdirectory, obj_id):
710731
results = qdb.sql_connection.TRN.execute_fetchindex()
711732
db_dir = get_db_files_base_dir()
712733

713-
return [(fpid, path_builder(db_dir, fp, m, s, obj_id), fp_type_)
734+
return [(fpid, _path_builder(db_dir, fp, m, s, obj_id), fp_type_)
714735
for fpid, fp, fp_type_, m, s in results]
715736

716737

@@ -845,6 +866,38 @@ def move_filepaths_to_upload_folder(study_id, filepaths):
845866
qdb.sql_connection.TRN.execute()
846867

847868

869+
def get_filepath_information(filepath_id):
870+
"""Gets the filepath information of filepath_id
871+
872+
Parameters
873+
----------
874+
filepath_id : int
875+
The filepath id
876+
877+
Returns
878+
-------
879+
dict
880+
The filepath information
881+
"""
882+
with qdb.sql_connection.TRN:
883+
sql = """SELECT filepath_id, filepath, filepath_type, checksum,
884+
data_type, mountpoint, subdirectory, active,
885+
artifact_id
886+
FROM qiita.filepath
887+
JOIN qiita.filepath_type USING (filepath_type_id)
888+
JOIN qiita.data_directory USING (data_directory_id)
889+
LEFT JOIN qiita.artifact_filepath USING (filepath_id)
890+
WHERE filepath_id = %s"""
891+
qdb.sql_connection.TRN.add(sql, [filepath_id])
892+
res = dict(qdb.sql_connection.TRN.execute_fetchindex()[0])
893+
894+
obj_id = res.pop('artifact_id')
895+
res['fullpath'] = _path_builder(get_db_files_base_dir(),
896+
res['filepath'], res['mountpoint'],
897+
res['subdirectory'], obj_id)
898+
return res
899+
900+
848901
def filepath_id_to_rel_path(filepath_id):
849902
"""Gets the relative to the base directory of filepath_id
850903

0 commit comments

Comments
 (0)