From 63a0fcce804b8ed181b90aaad0b35d2826b0cbb2 Mon Sep 17 00:00:00 2001 From: ariarijp Date: Wed, 5 Dec 2018 23:31:22 +0900 Subject: [PATCH] Add tag management commands --- redash/cli/__init__.py | 8 ++++-- redash/cli/queries.py | 64 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 redash/cli/queries.py diff --git a/redash/cli/__init__.py b/redash/cli/__init__.py index 316e426d5d..1b3fc512c1 100644 --- a/redash/cli/__init__.py +++ b/redash/cli/__init__.py @@ -1,11 +1,12 @@ from __future__ import print_function + import click import simplejson -from flask.cli import FlaskGroup, run_command from flask import current_app +from flask.cli import FlaskGroup, run_command -from redash import create_app, settings, __version__ -from redash.cli import users, groups, database, data_sources, organization +from redash import __version__, create_app, settings +from redash.cli import data_sources, database, groups, organization, queries, users from redash.monitor import get_status @@ -31,6 +32,7 @@ def manager(): manager.add_command(groups.manager, "groups") manager.add_command(data_sources.manager, "ds") manager.add_command(organization.manager, "org") +manager.add_command(queries.manager, "queries") manager.add_command(run_command, "runserver") diff --git a/redash/cli/queries.py b/redash/cli/queries.py new file mode 100644 index 0000000000..00810b749e --- /dev/null +++ b/redash/cli/queries.py @@ -0,0 +1,64 @@ +from click import argument +from flask.cli import AppGroup +from sqlalchemy.orm.exc import NoResultFound + +manager = AppGroup(help="Queries management commands.") + + +@manager.command() +@argument('query_id') +@argument('tag') +def add_tag(query_id, tag): + from redash import models + + query_id = int(query_id) + + try: + q = models.Query.get_by_id(query_id) + except NoResultFound: + print("Query not found.") + exit(1) + + tags = q.tags + if tags is None: + tags = [] + tags.append(tag) + q.tags = list(set(tags)) + + models.db.session.add(q) + models.db.session.commit() + + print("Tag added.") + + +@manager.command() +@argument('query_id') +@argument('tag') +def remove_tag(query_id, tag): + from redash import models + + query_id = int(query_id) + + try: + q = models.Query.get_by_id(query_id) + except NoResultFound: + print("Query not found.") + exit(1) + + tags = q.tags + if tags is None: + print("Tag is empty.") + exit(1) + + try: + tags.remove(tag) + except ValueError: + print("Tag not found.") + exit(1) + + q.tags = list(set(tags)) + + models.db.session.add(q) + models.db.session.commit() + + print("Tag removed.")