2024-02-01 15:03:56 +08:00
|
|
|
import os
|
2024-02-01 18:11:57 +08:00
|
|
|
|
2024-07-21 01:11:40 +08:00
|
|
|
from flask import session
|
2024-02-01 15:03:56 +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-09-11 16:40:52 +08:00
|
|
|
from libs.helper import StrLen
|
2024-02-01 15:03:56 +08:00
|
|
|
from models.model import DifySetup
|
|
|
|
from services.account_service import TenantService
|
|
|
|
|
|
|
|
from . import api
|
|
|
|
from .error import AlreadySetupError, InitValidateFailedError
|
|
|
|
from .wraps import only_edition_self_hosted
|
|
|
|
|
|
|
|
|
|
|
|
class InitValidateAPI(Resource):
|
|
|
|
def get(self):
|
|
|
|
init_status = get_init_validate_status()
|
|
|
|
if init_status:
|
2024-08-26 15:29:10 +08:00
|
|
|
return {"status": "finished"}
|
|
|
|
return {"status": "not_started"}
|
2024-02-01 15:03:56 +08:00
|
|
|
|
|
|
|
@only_edition_self_hosted
|
|
|
|
def post(self):
|
|
|
|
# is tenant created
|
|
|
|
tenant_count = TenantService.get_tenant_count()
|
|
|
|
if tenant_count > 0:
|
|
|
|
raise AlreadySetupError()
|
|
|
|
|
|
|
|
parser = reqparse.RequestParser()
|
2024-09-11 16:40:52 +08:00
|
|
|
parser.add_argument("password", type=StrLen(30), required=True, location="json")
|
2024-08-26 15:29:10 +08:00
|
|
|
input_password = parser.parse_args()["password"]
|
2024-02-01 15:03:56 +08:00
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
if input_password != os.environ.get("INIT_PASSWORD"):
|
|
|
|
session["is_init_validated"] = False
|
2024-02-01 15:03:56 +08:00
|
|
|
raise InitValidateFailedError()
|
2024-08-26 15:29:10 +08:00
|
|
|
|
|
|
|
session["is_init_validated"] = True
|
|
|
|
return {"result": "success"}, 201
|
|
|
|
|
2024-02-01 15:03:56 +08:00
|
|
|
|
|
|
|
def get_init_validate_status():
|
2024-08-26 15:29:10 +08:00
|
|
|
if dify_config.EDITION == "SELF_HOSTED":
|
|
|
|
if os.environ.get("INIT_PASSWORD"):
|
|
|
|
return session.get("is_init_validated") or DifySetup.query.first()
|
|
|
|
|
2024-02-01 15:03:56 +08:00
|
|
|
return True
|
|
|
|
|
2024-08-26 15:29:10 +08:00
|
|
|
|
|
|
|
api.add_resource(InitValidateAPI, "/init")
|