Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 82 additions & 1 deletion qiita_db/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,42 @@ def delete(cls, id_):

qdb.sql_connection.TRN.execute()

@classmethod
def get_tags(cls):
"""Returns the available study tags

Returns
-------
list of DictCursor
Table-like structure of metadata, one tag per row. Can be
accessed as a list of dictionaries, keyed on column name.
"""
with qdb.sql_connection.TRN:
sql = """SELECT study_tag_id, study_tag
FROM qiita.study_tags"""

qdb.sql_connection.TRN.add(sql)
return qdb.sql_connection.TRN.execute_fetchindex()

@classmethod
def insert_tags(cls, user, tags):
"""Insert available study tags

Parameters
----------
user : qiita_db.user.User
The user adding the tags
tags : list of str
The list of tags to add
"""
with qdb.sql_connection.TRN:
sql = """INSERT INTO qiita.study_tags (email, study_tag)
VALUES (%s, %s)"""
sql_args = [[user.email, tag] for tag in tags]

qdb.sql_connection.TRN.add(sql, sql_args, many=True)
qdb.sql_connection.TRN.execute()


# --- Attributes ---
@property
Expand Down Expand Up @@ -921,7 +957,52 @@ def ebi_submission_status(self, value):

ebi_submission_status.__doc__.format(', '.join(_VALID_EBI_STATUS))

# --- methods ---
@property
def tags(self):
"""Returns the tags of the study

Returns
-------
list of str
The study tags
"""
with qdb.sql_connection.TRN:
sql = """SELECT study_tag_id, study_tag
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just return the "study_tag" (not the id)

FROM qiita.study_tags
LEFT JOIN qiita.per_study_tags USING (study_tag_id)
WHERE study_id = {0}""".format(self._id)
qdb.sql_connection.TRN.add(sql)
return qdb.sql_connection.TRN.execute_fetchindex()

@tags.setter
def tags(self, tag_ids):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not setting the tags correctly. If a tag has been removed from the list it doesn't get removed from the DB.

Would it be more useful to add a couple of functions add_tags and remove_tags?

"""Sets the tags of the study

Parameters
----------
tag_ids : list of int
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we provide the list of tags rather than the list of tag ids? We have been removing the exposure of DB ids for these types of thins as much as we could, so I would like not to re-add some of them back

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just FYI: We (@josenavas and me) decided via offline chat that it was fine to leave as is because that way we can link the ids in the templates and the text can be escaped in the templates, which makes our live easier.

The tag ids of the study
"""
with qdb.sql_connection.TRN:
sql = """DELETE FROM qiita.per_study_tags WHERE study_id = %s"""
qdb.sql_connection.TRN.add(sql, [self._id])

if tag_ids:
sql = """INSERT INTO qiita.per_study_tags
(study_tag_id, study_id)
SELECT %s, %s
WHERE
NOT EXISTS (
SELECT study_tag_id, study_id
FROM qiita.per_study_tags
WHERE study_tag_id = %s AND study_id = %s
)"""
sql_args = [[tid, self._id, tid, self._id] for tid in tag_ids]
qdb.sql_connection.TRN.add(sql, sql_args, many=True)

qdb.sql_connection.TRN.execute()

# --- methods ---
def artifacts(self, dtype=None, artifact_type=None):
"""Returns the list of artifacts associated with the study

Expand Down
19 changes: 19 additions & 0 deletions qiita_db/support_files/patches/50.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- Feb 3, 2017
-- adding study tagging system

CREATE TABLE qiita.study_tags (
study_tag_id bigserial NOT NULL,
email varchar NOT NULL,
study_tag varchar NOT NULL,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can/should we guarantee unique tags?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, 1 line below: CONSTRAINT pk_study_tag UNIQUE ( study_tag_id ),

CONSTRAINT pk_study_tag UNIQUE ( study_tag ),
CONSTRAINT pk_study_tag_id PRIMARY KEY ( study_tag_id )
) ;

CREATE INDEX idx_study_tag_id ON qiita.study_tags ( study_tag_id ) ;
ALTER TABLE qiita.study_tags ADD CONSTRAINT fk_study_tags FOREIGN KEY ( email ) REFERENCES qiita.qiita_user( email );

CREATE TABLE qiita.per_study_tags (
study_tag_id bigint NOT NULL,
study_id bigint NOT NULL,
CONSTRAINT pk_per_study_tags PRIMARY KEY ( study_tag_id, study_id )
) ;
Loading