| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from functools import wraps | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from flask import current_app, request | 
					
						
							|  |  |  | from flask_restful import Resource, reqparse | 
					
						
							| 
									
										
										
										
											2024-02-06 13:21:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | from extensions.ext_database import db | 
					
						
							| 
									
										
										
										
											2023-05-15 08:51:32 +08:00
										 |  |  | from libs.helper import email, str_len | 
					
						
							|  |  |  | from libs.password import valid_password | 
					
						
							| 
									
										
										
										
											2024-01-12 12:34:01 +08:00
										 |  |  | from models.model import DifySetup | 
					
						
							|  |  |  | from services.account_service import AccountService, 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): | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |         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() | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2024-02-01 15:03:56 +08:00
										 |  |  |             return {'step': 'not_started'} | 
					
						
							| 
									
										
										
										
											2023-08-07 13:19:47 +08:00
										 |  |  |         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-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() | 
					
						
							|  |  |  |         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'}, 201 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def 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 | 
					
						
							| 
									
										
										
										
											2024-02-01 15:03:56 +08:00
										 |  |  |         if not get_init_validate_status(): | 
					
						
							|  |  |  |             raise NotInitValidateError() | 
					
						
							|  |  |  |          | 
					
						
							|  |  |  |         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(): | 
					
						
							|  |  |  |     if current_app.config['EDITION'] == 'SELF_HOSTED': | 
					
						
							|  |  |  |         return DifySetup.query.first() | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         return True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | api.add_resource(SetupApi, '/setup') |