Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from flask import request
  2. from flask_restx import Resource, fields, reqparse
  3. from configs import dify_config
  4. from libs.helper import StrLen, email, extract_remote_ip
  5. from libs.password import valid_password
  6. from models.model import DifySetup, db
  7. from services.account_service import RegisterService, TenantService
  8. from . import api, console_ns
  9. from .error import AlreadySetupError, NotInitValidateError
  10. from .init_validate import get_init_validate_status
  11. from .wraps import only_edition_self_hosted
  12. @console_ns.route("/setup")
  13. class SetupApi(Resource):
  14. @api.doc("get_setup_status")
  15. @api.doc(description="Get system setup status")
  16. @api.response(
  17. 200,
  18. "Success",
  19. api.model(
  20. "SetupStatusResponse",
  21. {
  22. "step": fields.String(description="Setup step status", enum=["not_started", "finished"]),
  23. "setup_at": fields.String(description="Setup completion time (ISO format)", required=False),
  24. },
  25. ),
  26. )
  27. def get(self):
  28. """Get system setup status"""
  29. if dify_config.EDITION == "SELF_HOSTED":
  30. setup_status = get_setup_status()
  31. # Check if setup_status is a DifySetup object rather than a bool
  32. if setup_status and not isinstance(setup_status, bool):
  33. return {"step": "finished", "setup_at": setup_status.setup_at.isoformat()}
  34. elif setup_status:
  35. return {"step": "finished"}
  36. return {"step": "not_started"}
  37. return {"step": "finished"}
  38. @api.doc("setup_system")
  39. @api.doc(description="Initialize system setup with admin account")
  40. @api.expect(
  41. api.model(
  42. "SetupRequest",
  43. {
  44. "email": fields.String(required=True, description="Admin email address"),
  45. "name": fields.String(required=True, description="Admin name (max 30 characters)"),
  46. "password": fields.String(required=True, description="Admin password"),
  47. },
  48. )
  49. )
  50. @api.response(201, "Success", api.model("SetupResponse", {"result": fields.String(description="Setup result")}))
  51. @api.response(400, "Already setup or validation failed")
  52. @only_edition_self_hosted
  53. def post(self):
  54. """Initialize system setup with admin account"""
  55. # is set up
  56. if get_setup_status():
  57. raise AlreadySetupError()
  58. # is tenant created
  59. tenant_count = TenantService.get_tenant_count()
  60. if tenant_count > 0:
  61. raise AlreadySetupError()
  62. if not get_init_validate_status():
  63. raise NotInitValidateError()
  64. parser = reqparse.RequestParser()
  65. parser.add_argument("email", type=email, required=True, location="json")
  66. parser.add_argument("name", type=StrLen(30), required=True, location="json")
  67. parser.add_argument("password", type=valid_password, required=True, location="json")
  68. args = parser.parse_args()
  69. # setup
  70. RegisterService.setup(
  71. email=args["email"], name=args["name"], password=args["password"], ip_address=extract_remote_ip(request)
  72. )
  73. return {"result": "success"}, 201
  74. def get_setup_status():
  75. if dify_config.EDITION == "SELF_HOSTED":
  76. return db.session.query(DifySetup).first()
  77. else:
  78. return True