@@ -464,6 +464,42 @@ def delete(cls, id_):
464
464
465
465
qdb .sql_connection .TRN .execute ()
466
466
467
+ @classmethod
468
+ def get_tags (cls ):
469
+ """Returns the available study tags
470
+
471
+ Returns
472
+ -------
473
+ list of DictCursor
474
+ Table-like structure of metadata, one tag per row. Can be
475
+ accessed as a list of dictionaries, keyed on column name.
476
+ """
477
+ with qdb .sql_connection .TRN :
478
+ sql = """SELECT study_tag_id, study_tag
479
+ FROM qiita.study_tags"""
480
+
481
+ qdb .sql_connection .TRN .add (sql )
482
+ return qdb .sql_connection .TRN .execute_fetchindex ()
483
+
484
+ @classmethod
485
+ def insert_tags (cls , user , tags ):
486
+ """Insert available study tags
487
+
488
+ Parameters
489
+ ----------
490
+ user : qiita_db.user.User
491
+ The user adding the tags
492
+ tags : list of str
493
+ The list of tags to add
494
+ """
495
+ with qdb .sql_connection .TRN :
496
+ sql = """INSERT INTO qiita.study_tags (email, study_tag)
497
+ VALUES (%s, %s)"""
498
+ sql_args = [[user .email , tag ] for tag in tags ]
499
+
500
+ qdb .sql_connection .TRN .add (sql , sql_args , many = True )
501
+ qdb .sql_connection .TRN .execute ()
502
+
467
503
468
504
# --- Attributes ---
469
505
@property
@@ -921,7 +957,52 @@ def ebi_submission_status(self, value):
921
957
922
958
ebi_submission_status .__doc__ .format (', ' .join (_VALID_EBI_STATUS ))
923
959
924
- # --- methods ---
960
+ @property
961
+ def tags (self ):
962
+ """Returns the tags of the study
963
+
964
+ Returns
965
+ -------
966
+ list of str
967
+ The study tags
968
+ """
969
+ with qdb .sql_connection .TRN :
970
+ sql = """SELECT study_tag_id, study_tag
971
+ FROM qiita.study_tags
972
+ LEFT JOIN qiita.per_study_tags USING (study_tag_id)
973
+ WHERE study_id = {0}""" .format (self ._id )
974
+ qdb .sql_connection .TRN .add (sql )
975
+ return qdb .sql_connection .TRN .execute_fetchindex ()
976
+
977
+ @tags .setter
978
+ def tags (self , tag_ids ):
979
+ """Sets the tags of the study
980
+
981
+ Parameters
982
+ ----------
983
+ tag_ids : list of int
984
+ The tag ids of the study
985
+ """
986
+ with qdb .sql_connection .TRN :
987
+ sql = """DELETE FROM qiita.per_study_tags WHERE study_id = %s"""
988
+ qdb .sql_connection .TRN .add (sql , [self ._id ])
989
+
990
+ if tag_ids :
991
+ sql = """INSERT INTO qiita.per_study_tags
992
+ (study_tag_id, study_id)
993
+ SELECT %s, %s
994
+ WHERE
995
+ NOT EXISTS (
996
+ SELECT study_tag_id, study_id
997
+ FROM qiita.per_study_tags
998
+ WHERE study_tag_id = %s AND study_id = %s
999
+ )"""
1000
+ sql_args = [[tid , self ._id , tid , self ._id ] for tid in tag_ids ]
1001
+ qdb .sql_connection .TRN .add (sql , sql_args , many = True )
1002
+
1003
+ qdb .sql_connection .TRN .execute ()
1004
+
1005
+ # --- methods ---
925
1006
def artifacts (self , dtype = None , artifact_type = None ):
926
1007
"""Returns the list of artifacts associated with the study
927
1008
0 commit comments