|
@@ -8,6 +8,7 @@ from werkzeug.exceptions import NotFound
|
|
|
from extensions.ext_database import db
|
|
|
from models.dataset import Dataset
|
|
|
from models.model import App, Tag, TagBinding
|
|
|
+from services.errors.tag import TagNameDuplicateError
|
|
|
|
|
|
|
|
|
class TagService:
|
|
@@ -25,6 +26,12 @@ class TagService:
|
|
|
return results
|
|
|
|
|
|
@staticmethod
|
|
|
+ def get_tag_by_tag_name(tag_type: str, tenant_id: str, tag_name: str) -> Optional[Tag]:
|
|
|
+ tag: Optional[Tag] = db.session.query(Tag).filter(Tag.type == tag_type, Tag.tenant_id == tenant_id,
|
|
|
+ Tag.name == tag_name).first()
|
|
|
+ return tag
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
def get_target_ids_by_tag_ids(tag_type: str, current_tenant_id: str, tag_ids: list) -> list:
|
|
|
tags = (
|
|
|
db.session.query(Tag)
|
|
@@ -61,13 +68,37 @@ class TagService:
|
|
|
return tags or []
|
|
|
|
|
|
@staticmethod
|
|
|
+ def get_tag(tag_id: str):
|
|
|
+ tag = db.session.query(Tag).filter(Tag.id == tag_id).first()
|
|
|
+ if not tag:
|
|
|
+ raise NotFound("Tag not found")
|
|
|
+ return tag
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def get_page_tags(page, per_page, tag_type, tenant_id):
|
|
|
+ query = (
|
|
|
+ db.session.query(Tag)
|
|
|
+ .filter(Tag.tenant_id == tenant_id, Tag.type == tag_type)
|
|
|
+ .order_by(Tag.created_at.desc())
|
|
|
+ )
|
|
|
+ tags = query.paginate(page=page, per_page=per_page, error_out=False)
|
|
|
+ return tags.items, tags.total
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
def save_tags(args: dict) -> Tag:
|
|
|
+ name = args["name"]
|
|
|
+ type = args["type"]
|
|
|
+ tenant_id = current_user.current_tenant_id
|
|
|
+ tag = TagService.get_tag_by_tag_name(type, tenant_id, name)
|
|
|
+ if tag:
|
|
|
+ raise TagNameDuplicateError(f"Tag with name {name} already exists.")
|
|
|
+
|
|
|
tag = Tag(
|
|
|
id=str(uuid.uuid4()),
|
|
|
- name=args["name"],
|
|
|
- type=args["type"],
|
|
|
+ name=name,
|
|
|
+ type=type,
|
|
|
created_by=current_user.id,
|
|
|
- tenant_id=current_user.current_tenant_id,
|
|
|
+ tenant_id=tenant_id,
|
|
|
)
|
|
|
db.session.add(tag)
|
|
|
db.session.commit()
|
|
@@ -138,7 +169,7 @@ class TagService:
|
|
|
|
|
|
@staticmethod
|
|
|
def check_target_exists(type: str, target_id: str):
|
|
|
- if type == "knowledge":
|
|
|
+ if type in {"knowledge", "knowledge_category"}:
|
|
|
dataset = (
|
|
|
db.session.query(Dataset)
|
|
|
.filter(Dataset.tenant_id == current_user.current_tenant_id, Dataset.id == target_id)
|