Skip to content
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

Add tag management commands #3168

Merged
merged 3 commits into from
Aug 11, 2019
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
8 changes: 5 additions & 3 deletions redash/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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")


Expand Down
64 changes: 64 additions & 0 deletions redash/cli/queries.py
Original file line number Diff line number Diff line change
@@ -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.")