2024-07-21 02:11:40 +09:00
|
|
|
from flask import request
|
2025-05-06 11:58:49 +08:00
|
|
|
from flask_restful import Resource, 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
|
|
|
|
|
|
|
from . import api
|
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
|
|
|
|
|
|
|
|
|
|
|
|
class SetupApi(Resource):
|
|
|
|
def get(self):
|
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()
|
|
|
|
if setup_status:
|
2024-08-26 15:29:10 +08:00
|
|
|
return {"step": "finished", "setup_at": setup_status.setup_at.isoformat()}
|
|
|
|
return {"step": "not_started"}
|
|
|
|
return {"step": "finished"}
|
2023-05-15 08:51:32 +08:00
|
|
|
|
|
|
|
@only_edition_self_hosted
|
|
|
|
def post(self):
|
|
|
|
# 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
|
2023-05-15 08:51:32 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
|
|
|
|
api.add_resource(SetupApi, "/setup")
|