2025-09-08 11:40:00 +09:00
|
|
|
from collections.abc import Callable
|
2023-05-25 15:54:45 +08:00
|
|
|
from functools import wraps
|
2025-09-08 11:40:00 +09:00
|
|
|
from typing import ParamSpec, TypeVar
|
2023-05-25 15:54:45 +08:00
|
|
|
|
2024-02-06 13:21:13 +08:00
|
|
|
from flask import request
|
2025-12-05 13:05:53 +09:00
|
|
|
from flask_restx import Resource
|
|
|
|
|
from pydantic import BaseModel, Field, field_validator
|
2025-02-17 17:05:13 +08:00
|
|
|
from sqlalchemy import select
|
|
|
|
|
from sqlalchemy.orm import Session
|
2024-02-06 13:21:13 +08:00
|
|
|
from werkzeug.exceptions import NotFound, Unauthorized
|
|
|
|
|
|
2025-09-08 11:40:00 +09:00
|
|
|
P = ParamSpec("P")
|
|
|
|
|
R = TypeVar("R")
|
2024-10-22 11:01:32 +08:00
|
|
|
from configs import dify_config
|
2024-02-01 18:11:57 +08:00
|
|
|
from constants.languages import supported_language
|
2025-11-24 11:04:11 +09:00
|
|
|
from controllers.console import console_ns
|
2023-05-25 15:54:45 +08:00
|
|
|
from controllers.console.wraps import only_edition_cloud
|
|
|
|
|
from extensions.ext_database import db
|
2025-10-19 21:29:04 +08:00
|
|
|
from libs.token import extract_access_token
|
2024-01-12 12:34:01 +08:00
|
|
|
from models.model import App, InstalledApp, RecommendedApp
|
2023-05-25 15:54:45 +08:00
|
|
|
|
2025-12-05 13:05:53 +09:00
|
|
|
DEFAULT_REF_TEMPLATE_SWAGGER_2_0 = "#/definitions/{model}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class InsertExploreAppPayload(BaseModel):
|
|
|
|
|
app_id: str = Field(...)
|
|
|
|
|
desc: str | None = None
|
|
|
|
|
copyright: str | None = None
|
|
|
|
|
privacy_policy: str | None = None
|
|
|
|
|
custom_disclaimer: str | None = None
|
|
|
|
|
language: str = Field(...)
|
|
|
|
|
category: str = Field(...)
|
|
|
|
|
position: int = Field(...)
|
|
|
|
|
|
|
|
|
|
@field_validator("language")
|
|
|
|
|
@classmethod
|
|
|
|
|
def validate_language(cls, value: str) -> str:
|
|
|
|
|
return supported_language(value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console_ns.schema_model(
|
|
|
|
|
InsertExploreAppPayload.__name__,
|
|
|
|
|
InsertExploreAppPayload.model_json_schema(ref_template=DEFAULT_REF_TEMPLATE_SWAGGER_2_0),
|
|
|
|
|
)
|
|
|
|
|
|
2023-05-25 15:54:45 +08:00
|
|
|
|
2025-09-08 11:40:00 +09:00
|
|
|
def admin_required(view: Callable[P, R]):
|
2023-05-25 15:54:45 +08:00
|
|
|
@wraps(view)
|
2025-09-08 11:40:00 +09:00
|
|
|
def decorated(*args: P.args, **kwargs: P.kwargs):
|
2024-10-22 11:01:32 +08:00
|
|
|
if not dify_config.ADMIN_API_KEY:
|
2024-08-26 15:29:10 +08:00
|
|
|
raise Unauthorized("API key is invalid.")
|
2023-05-25 15:54:45 +08:00
|
|
|
|
2025-10-19 21:29:04 +08:00
|
|
|
auth_token = extract_access_token(request)
|
|
|
|
|
if not auth_token:
|
2024-08-26 15:29:10 +08:00
|
|
|
raise Unauthorized("Authorization header is missing.")
|
2024-12-19 19:30:51 +09:00
|
|
|
if auth_token != dify_config.ADMIN_API_KEY:
|
2024-08-26 15:29:10 +08:00
|
|
|
raise Unauthorized("API key is invalid.")
|
2023-05-25 15:54:45 +08:00
|
|
|
|
|
|
|
|
return view(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
return decorated
|
|
|
|
|
|
|
|
|
|
|
2025-09-10 12:15:47 +08:00
|
|
|
@console_ns.route("/admin/insert-explore-apps")
|
2023-05-25 15:54:45 +08:00
|
|
|
class InsertExploreAppListApi(Resource):
|
2025-11-24 11:04:11 +09:00
|
|
|
@console_ns.doc("insert_explore_app")
|
|
|
|
|
@console_ns.doc(description="Insert or update an app in the explore list")
|
2025-12-05 13:05:53 +09:00
|
|
|
@console_ns.expect(console_ns.models[InsertExploreAppPayload.__name__])
|
2025-11-24 11:04:11 +09:00
|
|
|
@console_ns.response(200, "App updated successfully")
|
|
|
|
|
@console_ns.response(201, "App inserted successfully")
|
|
|
|
|
@console_ns.response(404, "App not found")
|
2023-05-25 15:54:45 +08:00
|
|
|
@only_edition_cloud
|
|
|
|
|
@admin_required
|
|
|
|
|
def post(self):
|
2025-12-05 13:05:53 +09:00
|
|
|
payload = InsertExploreAppPayload.model_validate(console_ns.payload)
|
|
|
|
|
|
|
|
|
|
app = db.session.execute(select(App).where(App.id == payload.app_id)).scalar_one_or_none()
|
2023-05-25 15:54:45 +08:00
|
|
|
if not app:
|
2025-12-05 13:05:53 +09:00
|
|
|
raise NotFound(f"App '{payload.app_id}' is not found")
|
2023-05-25 15:54:45 +08:00
|
|
|
|
|
|
|
|
site = app.site
|
|
|
|
|
if not site:
|
2025-12-05 13:05:53 +09:00
|
|
|
desc = payload.desc or ""
|
|
|
|
|
copy_right = payload.copyright or ""
|
|
|
|
|
privacy_policy = payload.privacy_policy or ""
|
|
|
|
|
custom_disclaimer = payload.custom_disclaimer or ""
|
2023-05-25 15:54:45 +08:00
|
|
|
else:
|
2025-12-05 13:05:53 +09:00
|
|
|
desc = site.description or payload.desc or ""
|
|
|
|
|
copy_right = site.copyright or payload.copyright or ""
|
|
|
|
|
privacy_policy = site.privacy_policy or payload.privacy_policy or ""
|
|
|
|
|
custom_disclaimer = site.custom_disclaimer or payload.custom_disclaimer or ""
|
2023-05-25 15:54:45 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
with Session(db.engine) as session:
|
|
|
|
|
recommended_app = session.execute(
|
2025-12-05 13:05:53 +09:00
|
|
|
select(RecommendedApp).where(RecommendedApp.app_id == payload.app_id)
|
2025-02-17 17:05:13 +08:00
|
|
|
).scalar_one_or_none()
|
2023-05-25 15:54:45 +08:00
|
|
|
|
2025-06-20 20:03:30 +08:00
|
|
|
if not recommended_app:
|
|
|
|
|
recommended_app = RecommendedApp(
|
|
|
|
|
app_id=app.id,
|
|
|
|
|
description=desc,
|
|
|
|
|
copyright=copy_right,
|
|
|
|
|
privacy_policy=privacy_policy,
|
|
|
|
|
custom_disclaimer=custom_disclaimer,
|
2025-12-05 13:05:53 +09:00
|
|
|
language=payload.language,
|
|
|
|
|
category=payload.category,
|
|
|
|
|
position=payload.position,
|
2025-06-20 20:03:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
db.session.add(recommended_app)
|
|
|
|
|
|
|
|
|
|
app.is_public = True
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
return {"result": "success"}, 201
|
|
|
|
|
else:
|
|
|
|
|
recommended_app.description = desc
|
|
|
|
|
recommended_app.copyright = copy_right
|
|
|
|
|
recommended_app.privacy_policy = privacy_policy
|
|
|
|
|
recommended_app.custom_disclaimer = custom_disclaimer
|
2025-12-05 13:05:53 +09:00
|
|
|
recommended_app.language = payload.language
|
|
|
|
|
recommended_app.category = payload.category
|
|
|
|
|
recommended_app.position = payload.position
|
2025-06-20 20:03:30 +08:00
|
|
|
|
|
|
|
|
app.is_public = True
|
|
|
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
|
|
return {"result": "success"}, 200
|
2023-05-25 15:54:45 +08:00
|
|
|
|
|
|
|
|
|
2025-09-10 12:15:47 +08:00
|
|
|
@console_ns.route("/admin/insert-explore-apps/<uuid:app_id>")
|
2023-05-25 15:54:45 +08:00
|
|
|
class InsertExploreAppApi(Resource):
|
2025-11-24 11:04:11 +09:00
|
|
|
@console_ns.doc("delete_explore_app")
|
|
|
|
|
@console_ns.doc(description="Remove an app from the explore list")
|
|
|
|
|
@console_ns.doc(params={"app_id": "Application ID to remove"})
|
|
|
|
|
@console_ns.response(204, "App removed successfully")
|
2023-05-25 15:54:45 +08:00
|
|
|
@only_edition_cloud
|
|
|
|
|
@admin_required
|
|
|
|
|
def delete(self, app_id):
|
2025-02-17 17:05:13 +08:00
|
|
|
with Session(db.engine) as session:
|
|
|
|
|
recommended_app = session.execute(
|
2025-07-24 01:57:45 +09:00
|
|
|
select(RecommendedApp).where(RecommendedApp.app_id == str(app_id))
|
2025-02-17 17:05:13 +08:00
|
|
|
).scalar_one_or_none()
|
|
|
|
|
|
2023-05-25 15:54:45 +08:00
|
|
|
if not recommended_app:
|
2024-08-26 15:29:10 +08:00
|
|
|
return {"result": "success"}, 204
|
2023-05-25 15:54:45 +08:00
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
with Session(db.engine) as session:
|
2025-07-24 01:57:45 +09:00
|
|
|
app = session.execute(select(App).where(App.id == recommended_app.app_id)).scalar_one_or_none()
|
2025-02-17 17:05:13 +08:00
|
|
|
|
2023-05-25 15:54:45 +08:00
|
|
|
if app:
|
|
|
|
|
app.is_public = False
|
|
|
|
|
|
2025-02-17 17:05:13 +08:00
|
|
|
with Session(db.engine) as session:
|
2025-09-02 11:59:38 +08:00
|
|
|
installed_apps = (
|
|
|
|
|
session.execute(
|
|
|
|
|
select(InstalledApp).where(
|
|
|
|
|
InstalledApp.app_id == recommended_app.app_id,
|
|
|
|
|
InstalledApp.tenant_id != InstalledApp.app_owner_tenant_id,
|
|
|
|
|
)
|
2025-02-17 17:05:13 +08:00
|
|
|
)
|
2025-09-02 11:59:38 +08:00
|
|
|
.scalars()
|
|
|
|
|
.all()
|
|
|
|
|
)
|
2023-05-25 15:54:45 +08:00
|
|
|
|
2025-09-02 11:59:38 +08:00
|
|
|
for installed_app in installed_apps:
|
|
|
|
|
session.delete(installed_app)
|
2023-05-25 15:54:45 +08:00
|
|
|
|
|
|
|
|
db.session.delete(recommended_app)
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
return {"result": "success"}, 204
|