orm filter -> where (#22801)
Signed-off-by: -LAN- <laipz8200@outlook.com> Co-authored-by: -LAN- <laipz8200@outlook.com> Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -4,7 +4,7 @@ from datetime import datetime
|
||||
from typing import Optional, cast
|
||||
|
||||
from flask_login import UserMixin # type: ignore
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.orm import Mapped, mapped_column, reconstructor
|
||||
|
||||
from models.base import Base
|
||||
@@ -119,7 +119,7 @@ class Account(UserMixin, Base):
|
||||
|
||||
@current_tenant.setter
|
||||
def current_tenant(self, tenant: "Tenant"):
|
||||
ta = db.session.query(TenantAccountJoin).filter_by(tenant_id=tenant.id, account_id=self.id).first()
|
||||
ta = db.session.scalar(select(TenantAccountJoin).filter_by(tenant_id=tenant.id, account_id=self.id).limit(1))
|
||||
if ta:
|
||||
self.role = TenantAccountRole(ta.role)
|
||||
self._current_tenant = tenant
|
||||
@@ -135,9 +135,9 @@ class Account(UserMixin, Base):
|
||||
tuple[Tenant, TenantAccountJoin],
|
||||
(
|
||||
db.session.query(Tenant, TenantAccountJoin)
|
||||
.filter(Tenant.id == tenant_id)
|
||||
.filter(TenantAccountJoin.tenant_id == Tenant.id)
|
||||
.filter(TenantAccountJoin.account_id == self.id)
|
||||
.where(Tenant.id == tenant_id)
|
||||
.where(TenantAccountJoin.tenant_id == Tenant.id)
|
||||
.where(TenantAccountJoin.account_id == self.id)
|
||||
.one_or_none()
|
||||
),
|
||||
)
|
||||
@@ -161,11 +161,11 @@ class Account(UserMixin, Base):
|
||||
def get_by_openid(cls, provider: str, open_id: str):
|
||||
account_integrate = (
|
||||
db.session.query(AccountIntegrate)
|
||||
.filter(AccountIntegrate.provider == provider, AccountIntegrate.open_id == open_id)
|
||||
.where(AccountIntegrate.provider == provider, AccountIntegrate.open_id == open_id)
|
||||
.one_or_none()
|
||||
)
|
||||
if account_integrate:
|
||||
return db.session.query(Account).filter(Account.id == account_integrate.account_id).one_or_none()
|
||||
return db.session.query(Account).where(Account.id == account_integrate.account_id).one_or_none()
|
||||
return None
|
||||
|
||||
# check current_user.current_tenant.current_role in ['admin', 'owner']
|
||||
@@ -211,7 +211,7 @@ class Tenant(Base):
|
||||
def get_accounts(self) -> list[Account]:
|
||||
return (
|
||||
db.session.query(Account)
|
||||
.filter(Account.id == TenantAccountJoin.account_id, TenantAccountJoin.tenant_id == self.id)
|
||||
.where(Account.id == TenantAccountJoin.account_id, TenantAccountJoin.tenant_id == self.id)
|
||||
.all()
|
||||
)
|
||||
|
||||
|
@@ -12,7 +12,7 @@ from datetime import datetime
|
||||
from json import JSONDecodeError
|
||||
from typing import Any, Optional, cast
|
||||
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.dialects.postgresql import JSONB
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
@@ -68,7 +68,7 @@ class Dataset(Base):
|
||||
@property
|
||||
def dataset_keyword_table(self):
|
||||
dataset_keyword_table = (
|
||||
db.session.query(DatasetKeywordTable).filter(DatasetKeywordTable.dataset_id == self.id).first()
|
||||
db.session.query(DatasetKeywordTable).where(DatasetKeywordTable.dataset_id == self.id).first()
|
||||
)
|
||||
if dataset_keyword_table:
|
||||
return dataset_keyword_table
|
||||
@@ -95,7 +95,7 @@ class Dataset(Base):
|
||||
def latest_process_rule(self):
|
||||
return (
|
||||
db.session.query(DatasetProcessRule)
|
||||
.filter(DatasetProcessRule.dataset_id == self.id)
|
||||
.where(DatasetProcessRule.dataset_id == self.id)
|
||||
.order_by(DatasetProcessRule.created_at.desc())
|
||||
.first()
|
||||
)
|
||||
@@ -104,19 +104,19 @@ class Dataset(Base):
|
||||
def app_count(self):
|
||||
return (
|
||||
db.session.query(func.count(AppDatasetJoin.id))
|
||||
.filter(AppDatasetJoin.dataset_id == self.id, App.id == AppDatasetJoin.app_id)
|
||||
.where(AppDatasetJoin.dataset_id == self.id, App.id == AppDatasetJoin.app_id)
|
||||
.scalar()
|
||||
)
|
||||
|
||||
@property
|
||||
def document_count(self):
|
||||
return db.session.query(func.count(Document.id)).filter(Document.dataset_id == self.id).scalar()
|
||||
return db.session.query(func.count(Document.id)).where(Document.dataset_id == self.id).scalar()
|
||||
|
||||
@property
|
||||
def available_document_count(self):
|
||||
return (
|
||||
db.session.query(func.count(Document.id))
|
||||
.filter(
|
||||
.where(
|
||||
Document.dataset_id == self.id,
|
||||
Document.indexing_status == "completed",
|
||||
Document.enabled == True,
|
||||
@@ -129,7 +129,7 @@ class Dataset(Base):
|
||||
def available_segment_count(self):
|
||||
return (
|
||||
db.session.query(func.count(DocumentSegment.id))
|
||||
.filter(
|
||||
.where(
|
||||
DocumentSegment.dataset_id == self.id,
|
||||
DocumentSegment.status == "completed",
|
||||
DocumentSegment.enabled == True,
|
||||
@@ -142,13 +142,13 @@ class Dataset(Base):
|
||||
return (
|
||||
db.session.query(Document)
|
||||
.with_entities(func.coalesce(func.sum(Document.word_count), 0))
|
||||
.filter(Document.dataset_id == self.id)
|
||||
.where(Document.dataset_id == self.id)
|
||||
.scalar()
|
||||
)
|
||||
|
||||
@property
|
||||
def doc_form(self):
|
||||
document = db.session.query(Document).filter(Document.dataset_id == self.id).first()
|
||||
document = db.session.query(Document).where(Document.dataset_id == self.id).first()
|
||||
if document:
|
||||
return document.doc_form
|
||||
return None
|
||||
@@ -169,7 +169,7 @@ class Dataset(Base):
|
||||
tags = (
|
||||
db.session.query(Tag)
|
||||
.join(TagBinding, Tag.id == TagBinding.tag_id)
|
||||
.filter(
|
||||
.where(
|
||||
TagBinding.target_id == self.id,
|
||||
TagBinding.tenant_id == self.tenant_id,
|
||||
Tag.tenant_id == self.tenant_id,
|
||||
@@ -185,14 +185,14 @@ class Dataset(Base):
|
||||
if self.provider != "external":
|
||||
return None
|
||||
external_knowledge_binding = (
|
||||
db.session.query(ExternalKnowledgeBindings).filter(ExternalKnowledgeBindings.dataset_id == self.id).first()
|
||||
db.session.query(ExternalKnowledgeBindings).where(ExternalKnowledgeBindings.dataset_id == self.id).first()
|
||||
)
|
||||
if not external_knowledge_binding:
|
||||
return None
|
||||
external_knowledge_api = (
|
||||
db.session.query(ExternalKnowledgeApis)
|
||||
.filter(ExternalKnowledgeApis.id == external_knowledge_binding.external_knowledge_api_id)
|
||||
.first()
|
||||
external_knowledge_api = db.session.scalar(
|
||||
select(ExternalKnowledgeApis).where(
|
||||
ExternalKnowledgeApis.id == external_knowledge_binding.external_knowledge_api_id
|
||||
)
|
||||
)
|
||||
if not external_knowledge_api:
|
||||
return None
|
||||
@@ -205,7 +205,7 @@ class Dataset(Base):
|
||||
|
||||
@property
|
||||
def doc_metadata(self):
|
||||
dataset_metadatas = db.session.query(DatasetMetadata).filter(DatasetMetadata.dataset_id == self.id).all()
|
||||
dataset_metadatas = db.session.query(DatasetMetadata).where(DatasetMetadata.dataset_id == self.id).all()
|
||||
|
||||
doc_metadata = [
|
||||
{
|
||||
@@ -408,7 +408,7 @@ class Document(Base):
|
||||
data_source_info_dict = json.loads(self.data_source_info)
|
||||
file_detail = (
|
||||
db.session.query(UploadFile)
|
||||
.filter(UploadFile.id == data_source_info_dict["upload_file_id"])
|
||||
.where(UploadFile.id == data_source_info_dict["upload_file_id"])
|
||||
.one_or_none()
|
||||
)
|
||||
if file_detail:
|
||||
@@ -441,24 +441,24 @@ class Document(Base):
|
||||
|
||||
@property
|
||||
def dataset(self):
|
||||
return db.session.query(Dataset).filter(Dataset.id == self.dataset_id).one_or_none()
|
||||
return db.session.query(Dataset).where(Dataset.id == self.dataset_id).one_or_none()
|
||||
|
||||
@property
|
||||
def segment_count(self):
|
||||
return db.session.query(DocumentSegment).filter(DocumentSegment.document_id == self.id).count()
|
||||
return db.session.query(DocumentSegment).where(DocumentSegment.document_id == self.id).count()
|
||||
|
||||
@property
|
||||
def hit_count(self):
|
||||
return (
|
||||
db.session.query(DocumentSegment)
|
||||
.with_entities(func.coalesce(func.sum(DocumentSegment.hit_count), 0))
|
||||
.filter(DocumentSegment.document_id == self.id)
|
||||
.where(DocumentSegment.document_id == self.id)
|
||||
.scalar()
|
||||
)
|
||||
|
||||
@property
|
||||
def uploader(self):
|
||||
user = db.session.query(Account).filter(Account.id == self.created_by).first()
|
||||
user = db.session.query(Account).where(Account.id == self.created_by).first()
|
||||
return user.name if user else None
|
||||
|
||||
@property
|
||||
@@ -475,7 +475,7 @@ class Document(Base):
|
||||
document_metadatas = (
|
||||
db.session.query(DatasetMetadata)
|
||||
.join(DatasetMetadataBinding, DatasetMetadataBinding.metadata_id == DatasetMetadata.id)
|
||||
.filter(
|
||||
.where(
|
||||
DatasetMetadataBinding.dataset_id == self.dataset_id, DatasetMetadataBinding.document_id == self.id
|
||||
)
|
||||
.all()
|
||||
@@ -687,26 +687,26 @@ class DocumentSegment(Base):
|
||||
|
||||
@property
|
||||
def dataset(self):
|
||||
return db.session.query(Dataset).filter(Dataset.id == self.dataset_id).first()
|
||||
return db.session.scalar(select(Dataset).where(Dataset.id == self.dataset_id))
|
||||
|
||||
@property
|
||||
def document(self):
|
||||
return db.session.query(Document).filter(Document.id == self.document_id).first()
|
||||
return db.session.scalar(select(Document).where(Document.id == self.document_id))
|
||||
|
||||
@property
|
||||
def previous_segment(self):
|
||||
return (
|
||||
db.session.query(DocumentSegment)
|
||||
.filter(DocumentSegment.document_id == self.document_id, DocumentSegment.position == self.position - 1)
|
||||
.first()
|
||||
return db.session.scalar(
|
||||
select(DocumentSegment).where(
|
||||
DocumentSegment.document_id == self.document_id, DocumentSegment.position == self.position - 1
|
||||
)
|
||||
)
|
||||
|
||||
@property
|
||||
def next_segment(self):
|
||||
return (
|
||||
db.session.query(DocumentSegment)
|
||||
.filter(DocumentSegment.document_id == self.document_id, DocumentSegment.position == self.position + 1)
|
||||
.first()
|
||||
return db.session.scalar(
|
||||
select(DocumentSegment).where(
|
||||
DocumentSegment.document_id == self.document_id, DocumentSegment.position == self.position + 1
|
||||
)
|
||||
)
|
||||
|
||||
@property
|
||||
@@ -717,7 +717,7 @@ class DocumentSegment(Base):
|
||||
if rules.parent_mode and rules.parent_mode != ParentMode.FULL_DOC:
|
||||
child_chunks = (
|
||||
db.session.query(ChildChunk)
|
||||
.filter(ChildChunk.segment_id == self.id)
|
||||
.where(ChildChunk.segment_id == self.id)
|
||||
.order_by(ChildChunk.position.asc())
|
||||
.all()
|
||||
)
|
||||
@@ -734,7 +734,7 @@ class DocumentSegment(Base):
|
||||
if rules.parent_mode:
|
||||
child_chunks = (
|
||||
db.session.query(ChildChunk)
|
||||
.filter(ChildChunk.segment_id == self.id)
|
||||
.where(ChildChunk.segment_id == self.id)
|
||||
.order_by(ChildChunk.position.asc())
|
||||
.all()
|
||||
)
|
||||
@@ -825,15 +825,15 @@ class ChildChunk(Base):
|
||||
|
||||
@property
|
||||
def dataset(self):
|
||||
return db.session.query(Dataset).filter(Dataset.id == self.dataset_id).first()
|
||||
return db.session.query(Dataset).where(Dataset.id == self.dataset_id).first()
|
||||
|
||||
@property
|
||||
def document(self):
|
||||
return db.session.query(Document).filter(Document.id == self.document_id).first()
|
||||
return db.session.query(Document).where(Document.id == self.document_id).first()
|
||||
|
||||
@property
|
||||
def segment(self):
|
||||
return db.session.query(DocumentSegment).filter(DocumentSegment.id == self.segment_id).first()
|
||||
return db.session.query(DocumentSegment).where(DocumentSegment.id == self.segment_id).first()
|
||||
|
||||
|
||||
class AppDatasetJoin(Base):
|
||||
@@ -1044,11 +1044,11 @@ class ExternalKnowledgeApis(Base):
|
||||
def dataset_bindings(self):
|
||||
external_knowledge_bindings = (
|
||||
db.session.query(ExternalKnowledgeBindings)
|
||||
.filter(ExternalKnowledgeBindings.external_knowledge_api_id == self.id)
|
||||
.where(ExternalKnowledgeBindings.external_knowledge_api_id == self.id)
|
||||
.all()
|
||||
)
|
||||
dataset_ids = [binding.dataset_id for binding in external_knowledge_bindings]
|
||||
datasets = db.session.query(Dataset).filter(Dataset.id.in_(dataset_ids)).all()
|
||||
datasets = db.session.query(Dataset).where(Dataset.id.in_(dataset_ids)).all()
|
||||
dataset_bindings = []
|
||||
for dataset in datasets:
|
||||
dataset_bindings.append({"id": dataset.id, "name": dataset.name})
|
||||
|
@@ -113,13 +113,13 @@ class App(Base):
|
||||
|
||||
@property
|
||||
def site(self):
|
||||
site = db.session.query(Site).filter(Site.app_id == self.id).first()
|
||||
site = db.session.query(Site).where(Site.app_id == self.id).first()
|
||||
return site
|
||||
|
||||
@property
|
||||
def app_model_config(self):
|
||||
if self.app_model_config_id:
|
||||
return db.session.query(AppModelConfig).filter(AppModelConfig.id == self.app_model_config_id).first()
|
||||
return db.session.query(AppModelConfig).where(AppModelConfig.id == self.app_model_config_id).first()
|
||||
|
||||
return None
|
||||
|
||||
@@ -128,7 +128,7 @@ class App(Base):
|
||||
if self.workflow_id:
|
||||
from .workflow import Workflow
|
||||
|
||||
return db.session.query(Workflow).filter(Workflow.id == self.workflow_id).first()
|
||||
return db.session.query(Workflow).where(Workflow.id == self.workflow_id).first()
|
||||
|
||||
return None
|
||||
|
||||
@@ -138,7 +138,7 @@ class App(Base):
|
||||
|
||||
@property
|
||||
def tenant(self):
|
||||
tenant = db.session.query(Tenant).filter(Tenant.id == self.tenant_id).first()
|
||||
tenant = db.session.query(Tenant).where(Tenant.id == self.tenant_id).first()
|
||||
return tenant
|
||||
|
||||
@property
|
||||
@@ -282,7 +282,7 @@ class App(Base):
|
||||
tags = (
|
||||
db.session.query(Tag)
|
||||
.join(TagBinding, Tag.id == TagBinding.tag_id)
|
||||
.filter(
|
||||
.where(
|
||||
TagBinding.target_id == self.id,
|
||||
TagBinding.tenant_id == self.tenant_id,
|
||||
Tag.tenant_id == self.tenant_id,
|
||||
@@ -296,7 +296,7 @@ class App(Base):
|
||||
@property
|
||||
def author_name(self):
|
||||
if self.created_by:
|
||||
account = db.session.query(Account).filter(Account.id == self.created_by).first()
|
||||
account = db.session.query(Account).where(Account.id == self.created_by).first()
|
||||
if account:
|
||||
return account.name
|
||||
|
||||
@@ -338,7 +338,7 @@ class AppModelConfig(Base):
|
||||
|
||||
@property
|
||||
def app(self):
|
||||
app = db.session.query(App).filter(App.id == self.app_id).first()
|
||||
app = db.session.query(App).where(App.id == self.app_id).first()
|
||||
return app
|
||||
|
||||
@property
|
||||
@@ -372,7 +372,7 @@ class AppModelConfig(Base):
|
||||
@property
|
||||
def annotation_reply_dict(self) -> dict:
|
||||
annotation_setting = (
|
||||
db.session.query(AppAnnotationSetting).filter(AppAnnotationSetting.app_id == self.app_id).first()
|
||||
db.session.query(AppAnnotationSetting).where(AppAnnotationSetting.app_id == self.app_id).first()
|
||||
)
|
||||
if annotation_setting:
|
||||
collection_binding_detail = annotation_setting.collection_binding_detail
|
||||
@@ -577,7 +577,7 @@ class RecommendedApp(Base):
|
||||
|
||||
@property
|
||||
def app(self):
|
||||
app = db.session.query(App).filter(App.id == self.app_id).first()
|
||||
app = db.session.query(App).where(App.id == self.app_id).first()
|
||||
return app
|
||||
|
||||
|
||||
@@ -601,12 +601,12 @@ class InstalledApp(Base):
|
||||
|
||||
@property
|
||||
def app(self):
|
||||
app = db.session.query(App).filter(App.id == self.app_id).first()
|
||||
app = db.session.query(App).where(App.id == self.app_id).first()
|
||||
return app
|
||||
|
||||
@property
|
||||
def tenant(self):
|
||||
tenant = db.session.query(Tenant).filter(Tenant.id == self.tenant_id).first()
|
||||
tenant = db.session.query(Tenant).where(Tenant.id == self.tenant_id).first()
|
||||
return tenant
|
||||
|
||||
|
||||
@@ -714,7 +714,7 @@ class Conversation(Base):
|
||||
model_config["configs"] = override_model_configs
|
||||
else:
|
||||
app_model_config = (
|
||||
db.session.query(AppModelConfig).filter(AppModelConfig.id == self.app_model_config_id).first()
|
||||
db.session.query(AppModelConfig).where(AppModelConfig.id == self.app_model_config_id).first()
|
||||
)
|
||||
if app_model_config:
|
||||
model_config = app_model_config.to_dict()
|
||||
@@ -737,21 +737,21 @@ class Conversation(Base):
|
||||
|
||||
@property
|
||||
def annotated(self):
|
||||
return db.session.query(MessageAnnotation).filter(MessageAnnotation.conversation_id == self.id).count() > 0
|
||||
return db.session.query(MessageAnnotation).where(MessageAnnotation.conversation_id == self.id).count() > 0
|
||||
|
||||
@property
|
||||
def annotation(self):
|
||||
return db.session.query(MessageAnnotation).filter(MessageAnnotation.conversation_id == self.id).first()
|
||||
return db.session.query(MessageAnnotation).where(MessageAnnotation.conversation_id == self.id).first()
|
||||
|
||||
@property
|
||||
def message_count(self):
|
||||
return db.session.query(Message).filter(Message.conversation_id == self.id).count()
|
||||
return db.session.query(Message).where(Message.conversation_id == self.id).count()
|
||||
|
||||
@property
|
||||
def user_feedback_stats(self):
|
||||
like = (
|
||||
db.session.query(MessageFeedback)
|
||||
.filter(
|
||||
.where(
|
||||
MessageFeedback.conversation_id == self.id,
|
||||
MessageFeedback.from_source == "user",
|
||||
MessageFeedback.rating == "like",
|
||||
@@ -761,7 +761,7 @@ class Conversation(Base):
|
||||
|
||||
dislike = (
|
||||
db.session.query(MessageFeedback)
|
||||
.filter(
|
||||
.where(
|
||||
MessageFeedback.conversation_id == self.id,
|
||||
MessageFeedback.from_source == "user",
|
||||
MessageFeedback.rating == "dislike",
|
||||
@@ -775,7 +775,7 @@ class Conversation(Base):
|
||||
def admin_feedback_stats(self):
|
||||
like = (
|
||||
db.session.query(MessageFeedback)
|
||||
.filter(
|
||||
.where(
|
||||
MessageFeedback.conversation_id == self.id,
|
||||
MessageFeedback.from_source == "admin",
|
||||
MessageFeedback.rating == "like",
|
||||
@@ -785,7 +785,7 @@ class Conversation(Base):
|
||||
|
||||
dislike = (
|
||||
db.session.query(MessageFeedback)
|
||||
.filter(
|
||||
.where(
|
||||
MessageFeedback.conversation_id == self.id,
|
||||
MessageFeedback.from_source == "admin",
|
||||
MessageFeedback.rating == "dislike",
|
||||
@@ -797,7 +797,7 @@ class Conversation(Base):
|
||||
|
||||
@property
|
||||
def status_count(self):
|
||||
messages = db.session.query(Message).filter(Message.conversation_id == self.id).all()
|
||||
messages = db.session.query(Message).where(Message.conversation_id == self.id).all()
|
||||
status_counts = {
|
||||
WorkflowExecutionStatus.RUNNING: 0,
|
||||
WorkflowExecutionStatus.SUCCEEDED: 0,
|
||||
@@ -824,19 +824,19 @@ class Conversation(Base):
|
||||
def first_message(self):
|
||||
return (
|
||||
db.session.query(Message)
|
||||
.filter(Message.conversation_id == self.id)
|
||||
.where(Message.conversation_id == self.id)
|
||||
.order_by(Message.created_at.asc())
|
||||
.first()
|
||||
)
|
||||
|
||||
@property
|
||||
def app(self):
|
||||
return db.session.query(App).filter(App.id == self.app_id).first()
|
||||
return db.session.query(App).where(App.id == self.app_id).first()
|
||||
|
||||
@property
|
||||
def from_end_user_session_id(self):
|
||||
if self.from_end_user_id:
|
||||
end_user = db.session.query(EndUser).filter(EndUser.id == self.from_end_user_id).first()
|
||||
end_user = db.session.query(EndUser).where(EndUser.id == self.from_end_user_id).first()
|
||||
if end_user:
|
||||
return end_user.session_id
|
||||
|
||||
@@ -845,7 +845,7 @@ class Conversation(Base):
|
||||
@property
|
||||
def from_account_name(self):
|
||||
if self.from_account_id:
|
||||
account = db.session.query(Account).filter(Account.id == self.from_account_id).first()
|
||||
account = db.session.query(Account).where(Account.id == self.from_account_id).first()
|
||||
if account:
|
||||
return account.name
|
||||
|
||||
@@ -1040,7 +1040,7 @@ class Message(Base):
|
||||
def user_feedback(self):
|
||||
feedback = (
|
||||
db.session.query(MessageFeedback)
|
||||
.filter(MessageFeedback.message_id == self.id, MessageFeedback.from_source == "user")
|
||||
.where(MessageFeedback.message_id == self.id, MessageFeedback.from_source == "user")
|
||||
.first()
|
||||
)
|
||||
return feedback
|
||||
@@ -1049,30 +1049,30 @@ class Message(Base):
|
||||
def admin_feedback(self):
|
||||
feedback = (
|
||||
db.session.query(MessageFeedback)
|
||||
.filter(MessageFeedback.message_id == self.id, MessageFeedback.from_source == "admin")
|
||||
.where(MessageFeedback.message_id == self.id, MessageFeedback.from_source == "admin")
|
||||
.first()
|
||||
)
|
||||
return feedback
|
||||
|
||||
@property
|
||||
def feedbacks(self):
|
||||
feedbacks = db.session.query(MessageFeedback).filter(MessageFeedback.message_id == self.id).all()
|
||||
feedbacks = db.session.query(MessageFeedback).where(MessageFeedback.message_id == self.id).all()
|
||||
return feedbacks
|
||||
|
||||
@property
|
||||
def annotation(self):
|
||||
annotation = db.session.query(MessageAnnotation).filter(MessageAnnotation.message_id == self.id).first()
|
||||
annotation = db.session.query(MessageAnnotation).where(MessageAnnotation.message_id == self.id).first()
|
||||
return annotation
|
||||
|
||||
@property
|
||||
def annotation_hit_history(self):
|
||||
annotation_history = (
|
||||
db.session.query(AppAnnotationHitHistory).filter(AppAnnotationHitHistory.message_id == self.id).first()
|
||||
db.session.query(AppAnnotationHitHistory).where(AppAnnotationHitHistory.message_id == self.id).first()
|
||||
)
|
||||
if annotation_history:
|
||||
annotation = (
|
||||
db.session.query(MessageAnnotation)
|
||||
.filter(MessageAnnotation.id == annotation_history.annotation_id)
|
||||
.where(MessageAnnotation.id == annotation_history.annotation_id)
|
||||
.first()
|
||||
)
|
||||
return annotation
|
||||
@@ -1080,11 +1080,9 @@ class Message(Base):
|
||||
|
||||
@property
|
||||
def app_model_config(self):
|
||||
conversation = db.session.query(Conversation).filter(Conversation.id == self.conversation_id).first()
|
||||
conversation = db.session.query(Conversation).where(Conversation.id == self.conversation_id).first()
|
||||
if conversation:
|
||||
return (
|
||||
db.session.query(AppModelConfig).filter(AppModelConfig.id == conversation.app_model_config_id).first()
|
||||
)
|
||||
return db.session.query(AppModelConfig).where(AppModelConfig.id == conversation.app_model_config_id).first()
|
||||
|
||||
return None
|
||||
|
||||
@@ -1100,7 +1098,7 @@ class Message(Base):
|
||||
def agent_thoughts(self):
|
||||
return (
|
||||
db.session.query(MessageAgentThought)
|
||||
.filter(MessageAgentThought.message_id == self.id)
|
||||
.where(MessageAgentThought.message_id == self.id)
|
||||
.order_by(MessageAgentThought.position.asc())
|
||||
.all()
|
||||
)
|
||||
@@ -1113,8 +1111,8 @@ class Message(Base):
|
||||
def message_files(self):
|
||||
from factories import file_factory
|
||||
|
||||
message_files = db.session.query(MessageFile).filter(MessageFile.message_id == self.id).all()
|
||||
current_app = db.session.query(App).filter(App.id == self.app_id).first()
|
||||
message_files = db.session.query(MessageFile).where(MessageFile.message_id == self.id).all()
|
||||
current_app = db.session.query(App).where(App.id == self.app_id).first()
|
||||
if not current_app:
|
||||
raise ValueError(f"App {self.app_id} not found")
|
||||
|
||||
@@ -1178,7 +1176,7 @@ class Message(Base):
|
||||
if self.workflow_run_id:
|
||||
from .workflow import WorkflowRun
|
||||
|
||||
return db.session.query(WorkflowRun).filter(WorkflowRun.id == self.workflow_run_id).first()
|
||||
return db.session.query(WorkflowRun).where(WorkflowRun.id == self.workflow_run_id).first()
|
||||
|
||||
return None
|
||||
|
||||
@@ -1253,7 +1251,7 @@ class MessageFeedback(Base):
|
||||
|
||||
@property
|
||||
def from_account(self):
|
||||
account = db.session.query(Account).filter(Account.id == self.from_account_id).first()
|
||||
account = db.session.query(Account).where(Account.id == self.from_account_id).first()
|
||||
return account
|
||||
|
||||
def to_dict(self):
|
||||
@@ -1335,12 +1333,12 @@ class MessageAnnotation(Base):
|
||||
|
||||
@property
|
||||
def account(self):
|
||||
account = db.session.query(Account).filter(Account.id == self.account_id).first()
|
||||
account = db.session.query(Account).where(Account.id == self.account_id).first()
|
||||
return account
|
||||
|
||||
@property
|
||||
def annotation_create_account(self):
|
||||
account = db.session.query(Account).filter(Account.id == self.account_id).first()
|
||||
account = db.session.query(Account).where(Account.id == self.account_id).first()
|
||||
return account
|
||||
|
||||
|
||||
@@ -1371,14 +1369,14 @@ class AppAnnotationHitHistory(Base):
|
||||
account = (
|
||||
db.session.query(Account)
|
||||
.join(MessageAnnotation, MessageAnnotation.account_id == Account.id)
|
||||
.filter(MessageAnnotation.id == self.annotation_id)
|
||||
.where(MessageAnnotation.id == self.annotation_id)
|
||||
.first()
|
||||
)
|
||||
return account
|
||||
|
||||
@property
|
||||
def annotation_create_account(self):
|
||||
account = db.session.query(Account).filter(Account.id == self.account_id).first()
|
||||
account = db.session.query(Account).where(Account.id == self.account_id).first()
|
||||
return account
|
||||
|
||||
|
||||
@@ -1404,7 +1402,7 @@ class AppAnnotationSetting(Base):
|
||||
|
||||
collection_binding_detail = (
|
||||
db.session.query(DatasetCollectionBinding)
|
||||
.filter(DatasetCollectionBinding.id == self.collection_binding_id)
|
||||
.where(DatasetCollectionBinding.id == self.collection_binding_id)
|
||||
.first()
|
||||
)
|
||||
return collection_binding_detail
|
||||
@@ -1470,7 +1468,7 @@ class AppMCPServer(Base):
|
||||
def generate_server_code(n):
|
||||
while True:
|
||||
result = generate_string(n)
|
||||
while db.session.query(AppMCPServer).filter(AppMCPServer.server_code == result).count() > 0:
|
||||
while db.session.query(AppMCPServer).where(AppMCPServer.server_code == result).count() > 0:
|
||||
result = generate_string(n)
|
||||
|
||||
return result
|
||||
@@ -1527,7 +1525,7 @@ class Site(Base):
|
||||
def generate_code(n):
|
||||
while True:
|
||||
result = generate_string(n)
|
||||
while db.session.query(Site).filter(Site.code == result).count() > 0:
|
||||
while db.session.query(Site).where(Site.code == result).count() > 0:
|
||||
result = generate_string(n)
|
||||
|
||||
return result
|
||||
@@ -1558,7 +1556,7 @@ class ApiToken(Base):
|
||||
def generate_api_key(prefix, n):
|
||||
while True:
|
||||
result = prefix + generate_string(n)
|
||||
if db.session.query(ApiToken).filter(ApiToken.token == result).count() > 0:
|
||||
if db.session.query(ApiToken).where(ApiToken.token == result).count() > 0:
|
||||
continue
|
||||
return result
|
||||
|
||||
|
@@ -153,11 +153,11 @@ class ApiToolProvider(Base):
|
||||
def user(self) -> Account | None:
|
||||
if not self.user_id:
|
||||
return None
|
||||
return db.session.query(Account).filter(Account.id == self.user_id).first()
|
||||
return db.session.query(Account).where(Account.id == self.user_id).first()
|
||||
|
||||
@property
|
||||
def tenant(self) -> Tenant | None:
|
||||
return db.session.query(Tenant).filter(Tenant.id == self.tenant_id).first()
|
||||
return db.session.query(Tenant).where(Tenant.id == self.tenant_id).first()
|
||||
|
||||
|
||||
class ToolLabelBinding(Base):
|
||||
@@ -223,11 +223,11 @@ class WorkflowToolProvider(Base):
|
||||
|
||||
@property
|
||||
def user(self) -> Account | None:
|
||||
return db.session.query(Account).filter(Account.id == self.user_id).first()
|
||||
return db.session.query(Account).where(Account.id == self.user_id).first()
|
||||
|
||||
@property
|
||||
def tenant(self) -> Tenant | None:
|
||||
return db.session.query(Tenant).filter(Tenant.id == self.tenant_id).first()
|
||||
return db.session.query(Tenant).where(Tenant.id == self.tenant_id).first()
|
||||
|
||||
@property
|
||||
def parameter_configurations(self) -> list[WorkflowToolParameterConfiguration]:
|
||||
@@ -235,7 +235,7 @@ class WorkflowToolProvider(Base):
|
||||
|
||||
@property
|
||||
def app(self) -> App | None:
|
||||
return db.session.query(App).filter(App.id == self.app_id).first()
|
||||
return db.session.query(App).where(App.id == self.app_id).first()
|
||||
|
||||
|
||||
class MCPToolProvider(Base):
|
||||
@@ -280,11 +280,11 @@ class MCPToolProvider(Base):
|
||||
)
|
||||
|
||||
def load_user(self) -> Account | None:
|
||||
return db.session.query(Account).filter(Account.id == self.user_id).first()
|
||||
return db.session.query(Account).where(Account.id == self.user_id).first()
|
||||
|
||||
@property
|
||||
def tenant(self) -> Tenant | None:
|
||||
return db.session.query(Tenant).filter(Tenant.id == self.tenant_id).first()
|
||||
return db.session.query(Tenant).where(Tenant.id == self.tenant_id).first()
|
||||
|
||||
@property
|
||||
def credentials(self) -> dict:
|
||||
|
@@ -26,7 +26,7 @@ class SavedMessage(Base):
|
||||
|
||||
@property
|
||||
def message(self):
|
||||
return db.session.query(Message).filter(Message.id == self.message_id).first()
|
||||
return db.session.query(Message).where(Message.id == self.message_id).first()
|
||||
|
||||
|
||||
class PinnedConversation(Base):
|
||||
|
@@ -343,7 +343,7 @@ class Workflow(Base):
|
||||
|
||||
return (
|
||||
db.session.query(WorkflowToolProvider)
|
||||
.filter(WorkflowToolProvider.tenant_id == self.tenant_id, WorkflowToolProvider.app_id == self.app_id)
|
||||
.where(WorkflowToolProvider.tenant_id == self.tenant_id, WorkflowToolProvider.app_id == self.app_id)
|
||||
.count()
|
||||
> 0
|
||||
)
|
||||
@@ -549,12 +549,12 @@ class WorkflowRun(Base):
|
||||
from models.model import Message
|
||||
|
||||
return (
|
||||
db.session.query(Message).filter(Message.app_id == self.app_id, Message.workflow_run_id == self.id).first()
|
||||
db.session.query(Message).where(Message.app_id == self.app_id, Message.workflow_run_id == self.id).first()
|
||||
)
|
||||
|
||||
@property
|
||||
def workflow(self):
|
||||
return db.session.query(Workflow).filter(Workflow.id == self.workflow_id).first()
|
||||
return db.session.query(Workflow).where(Workflow.id == self.workflow_id).first()
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
|
Reference in New Issue
Block a user