2024-07-21 02:11:40 +09:00
|
|
|
from flask import request
|
2025-09-10 12:15:47 +08:00
|
|
|
from flask_restx import Resource, fields, reqparse
|
2024-02-06 13:21:13 +08:00
|
|
|
|
2024-07-21 02:11:40 +09:00
|
|
|
from configs import dify_config
|
2024-10-12 23:46:30 +08:00
|
|
|
from libs.helper import StrLen, email, extract_remote_ip
|
2023-05-15 08:51:32 +08:00
|
|
|
from libs.password import valid_password
|
2025-02-17 17:05:13 +08:00
|
|
|
from models.model import DifySetup, db
|
2024-06-29 17:07:21 +08:00
|
|
|
from services.account_service import RegisterService, TenantService
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2025-09-10 12:15:47 +08:00
|
|
|
from . import api, console_ns
|
2024-11-01 15:51:22 +08:00
|
|
|
from .error import AlreadySetupError, NotInitValidateError
|
2024-02-01 15:03:56 +08:00
|
|
|
from .init_validate import get_init_validate_status
|
2023-05-15 08:51:32 +08:00
|
|
|
from .wraps import only_edition_self_hosted
|
|
|
|
|
|
|
|
|
|
|
2025-09-10 12:15:47 +08:00
|
|
|
@console_ns.route("/setup")
|
2023-05-15 08:51:32 +08:00
|
|
|
class SetupApi(Resource):
|
2025-09-10 12:15:47 +08:00
|
|
|
@api.doc("get_setup_status")
|
|
|
|
|
@api.doc(description="Get system setup status")
|
|
|
|
|
@api.response(
|
|
|
|
|
200,
|
|
|
|
|
"Success",
|
|
|
|
|
api.model(
|
|
|
|
|
"SetupStatusResponse",
|
|
|
|
|
{
|
|
|
|
|
"step": fields.String(description="Setup step status", enum=["not_started", "finished"]),
|
|
|
|
|
"setup_at": fields.String(description="Setup completion time (ISO format)", required=False),
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
)
|
2023-05-15 08:51:32 +08:00
|
|
|
def get(self):
|
2025-09-10 12:15:47 +08:00
|
|
|
"""Get system setup status"""
|
2024-08-26 15:29:10 +08:00
|
|
|
if dify_config.EDITION == "SELF_HOSTED":
|
2023-08-07 13:19:47 +08:00
|
|
|
setup_status = get_setup_status()
|
2025-09-10 12:15:47 +08:00
|
|
|
# Check if setup_status is a DifySetup object rather than a bool
|
|
|
|
|
if setup_status and not isinstance(setup_status, bool):
|
2024-08-26 15:29:10 +08:00
|
|
|
return {"step": "finished", "setup_at": setup_status.setup_at.isoformat()}
|
2025-09-10 12:15:47 +08:00
|
|
|
elif setup_status:
|
|
|
|
|
return {"step": "finished"}
|
2024-08-26 15:29:10 +08:00
|
|
|
return {"step": "not_started"}
|
|
|
|
|
return {"step": "finished"}
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2025-09-10 12:15:47 +08:00
|
|
|
@api.doc("setup_system")
|
|
|
|
|
@api.doc(description="Initialize system setup with admin account")
|
|
|
|
|
@api.expect(
|
|
|
|
|
api.model(
|
|
|
|
|
"SetupRequest",
|
|
|
|
|
{
|
|
|
|
|
"email": fields.String(required=True, description="Admin email address"),
|
|
|
|
|
"name": fields.String(required=True, description="Admin name (max 30 characters)"),
|
|
|
|
|
"password": fields.String(required=True, description="Admin password"),
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
@api.response(201, "Success", api.model("SetupResponse", {"result": fields.String(description="Setup result")}))
|
|
|
|
|
@api.response(400, "Already setup or validation failed")
|
2023-05-15 08:51:32 +08:00
|
|
|
@only_edition_self_hosted
|
|
|
|
|
def post(self):
|
2025-09-10 12:15:47 +08:00
|
|
|
"""Initialize system setup with admin account"""
|
2023-05-15 08:51:32 +08:00
|
|
|
# is set up
|
|
|
|
|
if get_setup_status():
|
|
|
|
|
raise AlreadySetupError()
|
|
|
|
|
|
|
|
|
|
# is tenant created
|
|
|
|
|
tenant_count = TenantService.get_tenant_count()
|
|
|
|
|
if tenant_count > 0:
|
|
|
|
|
raise AlreadySetupError()
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2024-02-01 15:03:56 +08:00
|
|
|
if not get_init_validate_status():
|
|
|
|
|
raise NotInitValidateError()
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
|
parser = reqparse.RequestParser()
|
2024-08-26 15:29:10 +08:00
|
|
|
parser.add_argument("email", type=email, required=True, location="json")
|
2024-09-11 16:40:52 +08:00
|
|
|
parser.add_argument("name", type=StrLen(30), required=True, location="json")
|
2024-08-26 15:29:10 +08:00
|
|
|
parser.add_argument("password", type=valid_password, required=True, location="json")
|
2023-05-15 08:51:32 +08:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
2024-06-29 17:07:21 +08:00
|
|
|
# setup
|
|
|
|
|
RegisterService.setup(
|
2024-10-12 23:46:30 +08:00
|
|
|
email=args["email"], name=args["name"], password=args["password"], ip_address=extract_remote_ip(request)
|
2023-05-15 08:51:32 +08:00
|
|
|
)
|
|
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
return {"result": "success"}, 201
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_setup_status():
|
2024-08-26 15:29:10 +08:00
|
|
|
if dify_config.EDITION == "SELF_HOSTED":
|
2025-02-17 17:05:13 +08:00
|
|
|
return db.session.query(DifySetup).first()
|
|
|
|
|
else:
|
|
|
|
|
return True
|