| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 | # -*- coding:utf-8 -*-from functools import wrapsfrom extensions.ext_database import dbfrom flask import current_app, requestfrom flask_restful import Resource, reqparsefrom libs.helper import email, str_lenfrom libs.password import valid_passwordfrom models.model import DifySetupfrom services.account_service import AccountService, RegisterService, TenantServicefrom . import apifrom .error import AlreadySetupError, NotSetupErrorfrom .wraps import only_edition_self_hostedclass SetupApi(Resource):    def get(self):        if current_app.config['EDITION'] == 'SELF_HOSTED':            setup_status = get_setup_status()            if setup_status:                return {                    'step': 'finished',                    'setup_at': setup_status.setup_at.isoformat()                }            return {'step': 'not_start'}        return {'step': 'finished'}    @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()        parser = reqparse.RequestParser()        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')        args = parser.parse_args()        # Register        account = RegisterService.register(            email=args['email'],            name=args['name'],            password=args['password']        )        setup()        AccountService.update_last_login(account, request)        return {'result': 'success'}, 201def setup():    dify_setup = DifySetup(        version=current_app.config['CURRENT_VERSION']    )    db.session.add(dify_setup)def setup_required(view):    @wraps(view)    def decorated(*args, **kwargs):        # check setup        if not get_setup_status():            raise NotSetupError()        return view(*args, **kwargs)    return decorateddef get_setup_status():    if current_app.config['EDITION'] == 'SELF_HOSTED':        return DifySetup.query.first()    else:        return Trueapi.add_resource(SetupApi, '/setup')
 |