-
Couldn't load subscription status.
- Fork 79
Adding tags #2088
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding tags #2088
Changes from 4 commits
56b7da5
0c136c6
a9bc599
0af93ff
56d8b0a
fd0b879
d6f5223
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -475,11 +475,23 @@ def get_tags(cls): | |
| 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""" | ||
| sql = """SELECT qiita.user_level.name AS user_level, | ||
| array_agg(study_tag) | ||
| FROM qiita.study_tags | ||
| LEFT JOIN qiita.qiita_user USING (email) | ||
| LEFT JOIN qiita.user_level USING (user_level_id) | ||
| GROUP BY qiita.user_level.name""" | ||
|
|
||
| qdb.sql_connection.TRN.add(sql) | ||
| return qdb.sql_connection.TRN.execute_fetchindex() | ||
| results = dict(qdb.sql_connection.TRN.execute_fetchindex()) | ||
| # when the system is empty, | ||
| # it's possible to get an empty dict, fixing | ||
| if 'admin' not in results: | ||
| results['admin'] = [] | ||
| if 'user' not in results: | ||
| results['user'] = [] | ||
|
|
||
| return results | ||
|
|
||
| @classmethod | ||
| def insert_tags(cls, user, tags): | ||
|
|
@@ -493,14 +505,15 @@ def insert_tags(cls, user, tags): | |
| The list of tags to add | ||
| """ | ||
| with qdb.sql_connection.TRN: | ||
| email = user.email | ||
| sql = """INSERT INTO qiita.study_tags (email, study_tag) | ||
| VALUES (%s, %s)""" | ||
| sql_args = [[user.email, tag] for tag in tags] | ||
| SELECT %s, %s WHERE NOT EXISTS ( | ||
| SELECT 1 FROM qiita.study_tags WHERE study_tag = %s)""" | ||
| sql_args = [[email, tag, tag] for tag in tags] | ||
|
|
||
| qdb.sql_connection.TRN.add(sql, sql_args, many=True) | ||
| qdb.sql_connection.TRN.execute() | ||
|
|
||
|
|
||
| # --- Attributes --- | ||
| @property | ||
| def title(self): | ||
|
|
@@ -967,40 +980,12 @@ def tags(self): | |
| The study tags | ||
| """ | ||
| with qdb.sql_connection.TRN: | ||
| sql = """SELECT study_tag_id, study_tag | ||
| sql = """SELECT study_tag | ||
| FROM qiita.study_tags | ||
| LEFT JOIN qiita.per_study_tags USING (study_tag_id) | ||
| LEFT JOIN qiita.per_study_tags USING (study_tag) | ||
| 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): | ||
| """Sets the tags of the study | ||
|
|
||
| Parameters | ||
| ---------- | ||
| tag_ids : list of int | ||
| 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() | ||
| return [t[0] for t in qdb.sql_connection.TRN.execute_fetchindex()] | ||
|
|
||
| # --- methods --- | ||
| def artifacts(self, dtype=None, artifact_type=None): | ||
|
|
@@ -1152,6 +1137,75 @@ def unshare(self, user): | |
| qdb.sql_connection.TRN.add(sql, [self._id, user.id]) | ||
| qdb.sql_connection.TRN.execute() | ||
|
|
||
| def add_tags(self, user, tags): | ||
| """Sets the tags of the study | ||
|
|
||
| Parameters | ||
| ---------- | ||
| user: User object | ||
| The user to unshare the study with | ||
|
||
| tags : list of str | ||
| The tags to add to the study | ||
|
||
|
|
||
| Returns | ||
| ------- | ||
| str | ||
| Warnings during insertion | ||
| """ | ||
| message = '' | ||
| # converting to set just to facilitate operations | ||
| system_tags_admin = set(self.get_tags()['admin']) | ||
| user_level = user.level | ||
| current_tags = set(self.tags) | ||
| to_delete = current_tags - set(tags) | ||
| to_add = set(tags) - current_tags | ||
|
|
||
| if to_delete or to_add: | ||
| with qdb.sql_connection.TRN: | ||
| if to_delete: | ||
| if user_level != 'admin': | ||
| admin_tags = to_delete & system_tags_admin | ||
| if admin_tags: | ||
| message += 'You cannot remove: %s' % ', '.join( | ||
| admin_tags) | ||
| to_delete = to_delete - admin_tags | ||
|
|
||
| if to_delete: | ||
| sql = """DELETE FROM qiita.per_study_tags | ||
| WHERE study_id = %s AND study_tag IN %s""" | ||
| qdb.sql_connection.TRN.add( | ||
| sql, [self._id, tuple(to_delete)]) | ||
|
|
||
| if to_add: | ||
| if user_level != 'admin': | ||
| admin_tags = to_add & system_tags_admin | ||
| if admin_tags: | ||
| message += ('Only admins can assing: ' | ||
|
||
| '%s' % ', '.join(admin_tags)) | ||
| to_add = to_add - admin_tags | ||
|
|
||
| if to_add: | ||
| self.insert_tags(user, to_add) | ||
|
|
||
| sql = """INSERT INTO qiita.per_study_tags | ||
| (study_tag, study_id) | ||
| SELECT %s, %s | ||
| WHERE | ||
| NOT EXISTS ( | ||
| SELECT study_tag, study_id | ||
| FROM qiita.per_study_tags | ||
| WHERE study_tag = %s | ||
| AND study_id = %s | ||
| )""" | ||
| sql_args = [[t, self._id, t, self._id] for t in to_add] | ||
| qdb.sql_connection.TRN.add(sql, sql_args, many=True) | ||
|
|
||
| qdb.sql_connection.TRN.execute() | ||
| else: | ||
| message = 'No changes in the tags.' | ||
|
|
||
| return message | ||
|
|
||
|
|
||
| class StudyPerson(qdb.base.QiitaObject): | ||
| r"""Object handling information pertaining to people involved in a study | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| -- Mar 16, 2017 | ||
| -- Changing tagging system structure, now study_tag will be the index | ||
|
|
||
| -- dropping all not required constrints, indexes and columns | ||
| ALTER TABLE qiita.study_tags DROP CONSTRAINT fk_study_tags; | ||
| DROP INDEX qiita.idx_study_tag_id; | ||
| ALTER TABLE qiita.study_tags DROP CONSTRAINT pk_study_tag; | ||
| ALTER TABLE qiita.study_tags DROP CONSTRAINT pk_study_tag_id; | ||
| ALTER TABLE qiita.study_tags DROP COLUMN study_tag_id; | ||
| ALTER TABLE qiita.per_study_tags ADD COLUMN study_tag varchar NOT NULL; | ||
| ALTER TABLE qiita.per_study_tags DROP CONSTRAINT pk_per_study_tags; | ||
| ALTER TABLE qiita.per_study_tags DROP COLUMN study_tag_id; | ||
|
|
||
| -- adding new restrictions | ||
| ALTER TABLE qiita.study_tags ADD CONSTRAINT pk_study_tags PRIMARY KEY ( study_tag ); | ||
| ALTER TABLE qiita.study_tags ADD CONSTRAINT fk_email FOREIGN KEY ( email ) REFERENCES qiita.qiita_user( email ); | ||
| ALTER TABLE qiita.per_study_tags ADD CONSTRAINT fk_study_tags FOREIGN KEY ( study_tag ) REFERENCES qiita.study_tags( study_tag ); | ||
| ALTER TABLE qiita.per_study_tags ADD CONSTRAINT fk_study_id FOREIGN KEY ( study_id ) REFERENCES qiita.study( study_id ); | ||
| ALTER TABLE qiita.per_study_tags ADD CONSTRAINT pk_per_study_tags PRIMARY KEY ( study_tag, study_id); | ||
|
|
||
| -- New structure: | ||
| -- CREATE TABLE qiita.study_tags ( | ||
| -- email varchar NOT NULL, | ||
| -- study_tag varchar NOT NULL, | ||
| -- ) ; | ||
| -- | ||
| -- CREATE TABLE qiita.per_study_tags ( | ||
| -- study_tag varchar NOT NULL, | ||
| -- study_id bigint NOT NULL, | ||
| -- ) ; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be named
update_tags, given that it can add and remove tags?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
k