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

chore: Singularize tag models #25819

Merged
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
4 changes: 2 additions & 2 deletions superset/charts/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from superset import app
from superset.common.chart_data import ChartDataResultFormat, ChartDataResultType
from superset.db_engine_specs.base import builtin_time_grains
from superset.tags.models import TagTypes
from superset.tags.models import TagType
from superset.utils import pandas_postprocessing, schema as utils
from superset.utils.core import (
AnnotationType,
Expand Down Expand Up @@ -146,7 +146,7 @@
class TagSchema(Schema):
id = fields.Int()
name = fields.String()
type = fields.Enum(TagTypes, by_value=True)
type = fields.Enum(TagType, by_value=True)


class ChartEntityResponseSchema(Schema):
Expand Down
26 changes: 13 additions & 13 deletions superset/common/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from sqlalchemy.sql import and_, func, join, literal, select

from superset.extensions import db
from superset.tags.models import ObjectTypes, TagTypes
from superset.tags.models import ObjectType, TagType


def add_types_to_charts(
Expand All @@ -35,7 +35,7 @@ def add_types_to_charts(
[
tag.c.id.label("tag_id"),
slices.c.id.label("object_id"),
literal(ObjectTypes.chart.name).label("object_type"),
literal(ObjectType.chart.name).label("object_type"),
]
)
.select_from(
Expand Down Expand Up @@ -67,7 +67,7 @@ def add_types_to_dashboards(
[
tag.c.id.label("tag_id"),
dashboard_table.c.id.label("object_id"),
literal(ObjectTypes.dashboard.name).label("object_type"),
literal(ObjectType.dashboard.name).label("object_type"),
]
)
.select_from(
Expand Down Expand Up @@ -99,7 +99,7 @@ def add_types_to_saved_queries(
[
tag.c.id.label("tag_id"),
saved_query.c.id.label("object_id"),
literal(ObjectTypes.query.name).label("object_type"),
literal(ObjectType.query.name).label("object_type"),
]
)
.select_from(
Expand Down Expand Up @@ -131,7 +131,7 @@ def add_types_to_datasets(
[
tag.c.id.label("tag_id"),
tables.c.id.label("object_id"),
literal(ObjectTypes.dataset.name).label("object_type"),
literal(ObjectType.dataset.name).label("object_type"),
]
)
.select_from(
Expand Down Expand Up @@ -221,9 +221,9 @@ def add_types(metadata: MetaData) -> None:

# add a tag for each object type
insert = tag.insert()
for type_ in ObjectTypes.__members__:
for type_ in ObjectType.__members__:
with contextlib.suppress(IntegrityError): # already exists
db.session.execute(insert, name=f"type:{type_}", type=TagTypes.type)
db.session.execute(insert, name=f"type:{type_}", type=TagType.type)

add_types_to_charts(metadata, tag, tagged_object, columns)
add_types_to_dashboards(metadata, tag, tagged_object, columns)
Expand All @@ -241,7 +241,7 @@ def add_owners_to_charts(
[
tag.c.id.label("tag_id"),
slices.c.id.label("object_id"),
literal(ObjectTypes.chart.name).label("object_type"),
literal(ObjectType.chart.name).label("object_type"),
]
)
.select_from(
Expand Down Expand Up @@ -277,7 +277,7 @@ def add_owners_to_dashboards(
[
tag.c.id.label("tag_id"),
dashboard_table.c.id.label("object_id"),
literal(ObjectTypes.dashboard.name).label("object_type"),
literal(ObjectType.dashboard.name).label("object_type"),
]
)
.select_from(
Expand Down Expand Up @@ -313,7 +313,7 @@ def add_owners_to_saved_queries(
[
tag.c.id.label("tag_id"),
saved_query.c.id.label("object_id"),
literal(ObjectTypes.query.name).label("object_type"),
literal(ObjectType.query.name).label("object_type"),
]
)
.select_from(
Expand Down Expand Up @@ -349,7 +349,7 @@ def add_owners_to_datasets(
[
tag.c.id.label("tag_id"),
tables.c.id.label("object_id"),
literal(ObjectTypes.dataset.name).label("object_type"),
literal(ObjectType.dataset.name).label("object_type"),
]
)
.select_from(
Expand Down Expand Up @@ -444,7 +444,7 @@ def add_owners(metadata: MetaData) -> None:
insert = tag.insert()
for (id_,) in db.session.execute(ids):
with contextlib.suppress(IntegrityError): # already exists
db.session.execute(insert, name=f"owner:{id_}", type=TagTypes.owner)
db.session.execute(insert, name=f"owner:{id_}", type=TagType.owner)
add_owners_to_charts(metadata, tag, tagged_object, columns)
add_owners_to_dashboards(metadata, tag, tagged_object, columns)
add_owners_to_saved_queries(metadata, tag, tagged_object, columns)
Expand Down Expand Up @@ -482,7 +482,7 @@ def add_favorites(metadata: MetaData) -> None:
insert = tag.insert()
for (id_,) in db.session.execute(ids):
with contextlib.suppress(IntegrityError): # already exists
db.session.execute(insert, name=f"favorited_by:{id_}", type=TagTypes.type)
db.session.execute(insert, name=f"favorited_by:{id_}", type=TagType.type)
favstars = (
select(
[
Expand Down
30 changes: 15 additions & 15 deletions superset/daos/tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
from superset.tags.commands.utils import to_object_type
from superset.tags.models import (
get_tag,
ObjectTypes,
ObjectType,
Tag,
TaggedObject,
TagTypes,
TagType,
user_favorite_tag_table,
)
from superset.utils.core import get_user_id
Expand All @@ -56,15 +56,15 @@ def validate_tag_name(tag_name: str) -> bool:

@staticmethod
def create_custom_tagged_objects(
object_type: ObjectTypes, object_id: int, tag_names: list[str]
object_type: ObjectType, object_id: int, tag_names: list[str]
) -> None:
tagged_objects = []
for name in tag_names:
if not TagDAO.validate_tag_name(name):
raise DAOCreateFailedError(
message="Invalid Tag Name (cannot contain ':' or ',')"
)
type_ = TagTypes.custom
type_ = TagType.custom
tag_name = name.strip()
tag = TagDAO.get_by_name(tag_name, type_)
tagged_objects.append(
Expand All @@ -76,7 +76,7 @@ def create_custom_tagged_objects(

@staticmethod
def delete_tagged_object(
object_type: ObjectTypes, object_id: int, tag_name: str
object_type: ObjectType, object_id: int, tag_name: str
) -> None:
"""
deletes a tagged object by the object_id, object_type, and tag_name
Expand Down Expand Up @@ -128,7 +128,7 @@ def delete_tags(tag_names: list[str]) -> None:
raise DAODeleteFailedError(exception=ex) from ex

@staticmethod
def get_by_name(name: str, type_: TagTypes = TagTypes.custom) -> Tag:
def get_by_name(name: str, type_: TagType = TagType.custom) -> Tag:
"""
returns a tag if one exists by that name, none otherwise.
important!: Creates a tag by that name if the tag is not found.
Expand All @@ -152,7 +152,7 @@ def find_by_name(name: str) -> Tag:

@staticmethod
def find_tagged_object(
object_type: ObjectTypes, object_id: int, tag_id: int
object_type: ObjectType, object_id: int, tag_id: int
) -> TaggedObject:
"""
returns a tagged object if one exists by that name, none otherwise.
Expand Down Expand Up @@ -185,7 +185,7 @@ def get_tagged_objects_for_tags(
TaggedObject,
and_(
TaggedObject.object_id == Dashboard.id,
TaggedObject.object_type == ObjectTypes.dashboard,
TaggedObject.object_type == ObjectType.dashboard,
),
)
.join(Tag, TaggedObject.tag_id == Tag.id)
Expand All @@ -195,7 +195,7 @@ def get_tagged_objects_for_tags(
results.extend(
{
"id": obj.id,
"type": ObjectTypes.dashboard.name,
"type": ObjectType.dashboard.name,
"name": obj.dashboard_title,
"url": obj.url,
"changed_on": obj.changed_on,
Expand All @@ -215,7 +215,7 @@ def get_tagged_objects_for_tags(
TaggedObject,
and_(
TaggedObject.object_id == Slice.id,
TaggedObject.object_type == ObjectTypes.chart,
TaggedObject.object_type == ObjectType.chart,
),
)
.join(Tag, TaggedObject.tag_id == Tag.id)
Expand All @@ -224,7 +224,7 @@ def get_tagged_objects_for_tags(
results.extend(
{
"id": obj.id,
"type": ObjectTypes.chart.name,
"type": ObjectType.chart.name,
"name": obj.slice_name,
"url": obj.url,
"changed_on": obj.changed_on,
Expand All @@ -244,7 +244,7 @@ def get_tagged_objects_for_tags(
TaggedObject,
and_(
TaggedObject.object_id == SavedQuery.id,
TaggedObject.object_type == ObjectTypes.query,
TaggedObject.object_type == ObjectType.query,
),
)
.join(Tag, TaggedObject.tag_id == Tag.id)
Expand All @@ -253,7 +253,7 @@ def get_tagged_objects_for_tags(
results.extend(
{
"id": obj.id,
"type": ObjectTypes.query.name,
"type": ObjectType.query.name,
"name": obj.label,
"url": obj.url(),
"changed_on": obj.changed_on,
Expand Down Expand Up @@ -363,7 +363,7 @@ def favorited_ids(tags: list[Tag]) -> list[int]:

@staticmethod
def create_tag_relationship(
objects_to_tag: list[tuple[ObjectTypes, int]],
objects_to_tag: list[tuple[ObjectType, int]],
tag: Tag,
bulk_create: bool = False,
) -> None:
Expand All @@ -373,7 +373,7 @@ def create_tag_relationship(
and an id, and creates a TaggedObject for each one, associating it with
the provided tag. All created TaggedObjects are collected in a list.
Args:
objects_to_tag (List[Tuple[ObjectTypes, int]]): A list of tuples, each
objects_to_tag (List[Tuple[ObjectType, int]]): A list of tuples, each
containing an ObjectType and an id, representing the objects to be tagged.

tag (Tag): The tag to be associated with the specified objects.
Expand Down
4 changes: 2 additions & 2 deletions superset/dashboards/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from marshmallow.validate import Length, ValidationError

from superset.exceptions import SupersetException
from superset.tags.models import TagTypes
from superset.tags.models import TagType
from superset.utils import core as utils

get_delete_ids_schema = {"type": "array", "items": {"type": "integer"}}
Expand Down Expand Up @@ -169,7 +169,7 @@ class RolesSchema(Schema):
class TagSchema(Schema):
id = fields.Int()
name = fields.String()
type = fields.Enum(TagTypes, by_value=True)
type = fields.Enum(TagType, by_value=True)


class DashboardGetResponseSchema(Schema):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from sqlalchemy import Column, DateTime, Enum, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base, declared_attr

from superset.tags.models import ObjectTypes, TagTypes
from superset.tags.models import ObjectType, TagType
from superset.utils.core import get_user_id

Base = declarative_base()
Expand Down Expand Up @@ -77,7 +77,7 @@ class Tag(Base, AuditMixinNullable):

id = Column(Integer, primary_key=True)
name = Column(String(250), unique=True)
type = Column(Enum(TagTypes))
type = Column(Enum(TagType))


class TaggedObject(Base, AuditMixinNullable):
Expand All @@ -86,7 +86,7 @@ class TaggedObject(Base, AuditMixinNullable):
id = Column(Integer, primary_key=True)
tag_id = Column(Integer, ForeignKey("tag.id"))
object_id = Column(Integer)
object_type = Column(Enum(ObjectTypes))
object_type = Column(Enum(ObjectType))


class User(Base):
Expand Down
6 changes: 3 additions & 3 deletions superset/tags/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
TagUpdateFailedError,
)
from superset.tags.commands.update import UpdateTagCommand
from superset.tags.models import ObjectTypes, Tag
from superset.tags.models import ObjectType, Tag
from superset.tags.schemas import (
delete_tags_schema,
openapi_spec_methods_override,
Expand Down Expand Up @@ -364,7 +364,7 @@ def put(self, pk: int) -> Response:
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.add_objects",
log_to_statsd=False,
)
def add_objects(self, object_type: ObjectTypes, object_id: int) -> Response:
def add_objects(self, object_type: ObjectType, object_id: int) -> Response:
"""Add tags to an object. Create new tags if they do not already exist.
---
post:
Expand Down Expand Up @@ -429,7 +429,7 @@ def add_objects(self, object_type: ObjectTypes, object_id: int) -> Response:
log_to_statsd=True,
)
def delete_object(
self, object_type: ObjectTypes, object_id: int, tag: str
self, object_type: ObjectType, object_id: int, tag: str
) -> Response:
"""Delete a tagged object.
---
Expand Down
6 changes: 3 additions & 3 deletions superset/tags/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
from superset.exceptions import SupersetSecurityException
from superset.tags.commands.exceptions import TagCreateFailedError, TagInvalidError
from superset.tags.commands.utils import to_object_model, to_object_type
from superset.tags.models import ObjectTypes, TagTypes
from superset.tags.models import ObjectType, TagType

logger = logging.getLogger(__name__)


class CreateCustomTagCommand(CreateMixin, BaseCommand):
def __init__(self, object_type: ObjectTypes, object_id: int, tags: list[str]):
def __init__(self, object_type: ObjectType, object_id: int, tags: list[str]):
self._object_type = object_type
self._object_id = object_id
self._tags = tags
Expand Down Expand Up @@ -76,7 +76,7 @@ def run(self) -> tuple[set[tuple[str, int]], set[tuple[str, int]]]:

try:
tag_name = self._properties["name"]
tag = TagDAO.get_by_name(tag_name.strip(), TagTypes.custom)
tag = TagDAO.get_by_name(tag_name.strip(), TagType.custom)
TagDAO.create_tag_relationship(
objects_to_tag=self._properties.get("objects_to_tag", []),
tag=tag,
Expand Down
4 changes: 2 additions & 2 deletions superset/tags/commands/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
TagNotFoundError,
)
from superset.tags.commands.utils import to_object_type
from superset.tags.models import ObjectTypes
from superset.tags.models import ObjectType
from superset.views.base import DeleteMixin

logger = logging.getLogger(__name__)


class DeleteTaggedObjectCommand(DeleteMixin, BaseCommand):
def __init__(self, object_type: ObjectTypes, object_id: int, tag: str):
def __init__(self, object_type: ObjectType, object_id: int, tag: str):
self._object_type = object_type
self._object_id = object_id
self._tag = tag
Expand Down
Loading
Loading