Skip to content

Commit

Permalink
Add tag management commands (#3168)
Browse files Browse the repository at this point in the history
  • Loading branch information
ariarijp authored and arikfr committed Aug 11, 2019
1 parent aa2bd00 commit 8abaf89
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
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 Down Expand Up @@ -33,6 +34,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.")

0 comments on commit 8abaf89

Please sign in to comment.