2023-05-15 08:51:32 +08:00
|
|
|
from functools import wraps
|
|
|
|
|
2024-07-21 01:11:40 +08:00
|
|
|
from flask import request
|
2024-01-12 12:34:01 +08:00
|
|
|
from flask_restful import Resource, reqparse
|
2024-02-06 13:21:13 +08:00
|
|
|
|
2024-07-21 01:11:40 +08:00
|
|
|
from configs import dify_config
|
2024-06-21 12:39:07 +08:00
|
|
|
from libs.helper import email, get_remote_ip, str_len
|
2023-05-15 08:51:32 +08:00
|
|
|
from libs.password import valid_password
|
2024-01-12 12:34:01 +08:00
|
|
|
from models.model import DifySetup
|
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-02-01 18:11:57 +08:00
|
|
|
from .error import AlreadySetupError, NotInitValidateError, NotSetupError
|
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")
|
|
|
|
parser.add_argument("name", type=str_len(30), required=True, location="json")
|
|
|
|
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-08-26 15:29:10 +08:00
|
|
|
email=args["email"], name=args["name"], password=args["password"], ip_address=get_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 setup_required(view):
|
|
|
|
@wraps(view)
|
|
|
|
def decorated(*args, **kwargs):
|
|
|
|
# check setup
|
2024-02-01 15:03:56 +08:00
|
|
|
if not get_init_validate_status():
|
|
|
|
raise NotInitValidateError()
|
2024-08-26 15:29:10 +08:00
|
|
|
|
2024-02-01 15:03:56 +08:00
|
|
|
elif not get_setup_status():
|
2023-05-15 08:51:32 +08:00
|
|
|
raise NotSetupError()
|
|
|
|
|
|
|
|
return view(*args, **kwargs)
|
|
|
|
|
|
|
|
return decorated
|
|
|
|
|
|
|
|
|
|
|
|
def get_setup_status():
|
2024-08-26 15:29:10 +08:00
|
|
|
if dify_config.EDITION == "SELF_HOSTED":
|
2023-05-15 08:51:32 +08:00
|
|
|
return DifySetup.query.first()
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
|
|
|
|
api.add_resource(SetupApi, "/setup")
|