Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. from flask_login import current_user
  2. from flask_restx import Resource, fields, marshal_with, reqparse
  3. from werkzeug.exceptions import Forbidden, NotFound
  4. from constants.languages import supported_language
  5. from controllers.console import api, console_ns
  6. from controllers.console.app.wraps import get_app_model
  7. from controllers.console.wraps import account_initialization_required, setup_required
  8. from extensions.ext_database import db
  9. from fields.app_fields import app_site_fields
  10. from libs.datetime_utils import naive_utc_now
  11. from libs.login import login_required
  12. from models import Account, Site
  13. def parse_app_site_args():
  14. parser = reqparse.RequestParser()
  15. parser.add_argument("title", type=str, required=False, location="json")
  16. parser.add_argument("icon_type", type=str, required=False, location="json")
  17. parser.add_argument("icon", type=str, required=False, location="json")
  18. parser.add_argument("icon_background", type=str, required=False, location="json")
  19. parser.add_argument("description", type=str, required=False, location="json")
  20. parser.add_argument("default_language", type=supported_language, required=False, location="json")
  21. parser.add_argument("chat_color_theme", type=str, required=False, location="json")
  22. parser.add_argument("chat_color_theme_inverted", type=bool, required=False, location="json")
  23. parser.add_argument("customize_domain", type=str, required=False, location="json")
  24. parser.add_argument("copyright", type=str, required=False, location="json")
  25. parser.add_argument("privacy_policy", type=str, required=False, location="json")
  26. parser.add_argument("custom_disclaimer", type=str, required=False, location="json")
  27. parser.add_argument(
  28. "customize_token_strategy", type=str, choices=["must", "allow", "not_allow"], required=False, location="json"
  29. )
  30. parser.add_argument("prompt_public", type=bool, required=False, location="json")
  31. parser.add_argument("show_workflow_steps", type=bool, required=False, location="json")
  32. parser.add_argument("use_icon_as_answer_icon", type=bool, required=False, location="json")
  33. return parser.parse_args()
  34. @console_ns.route("/apps/<uuid:app_id>/site")
  35. class AppSite(Resource):
  36. @api.doc("update_app_site")
  37. @api.doc(description="Update application site configuration")
  38. @api.doc(params={"app_id": "Application ID"})
  39. @api.expect(
  40. api.model(
  41. "AppSiteRequest",
  42. {
  43. "title": fields.String(description="Site title"),
  44. "icon_type": fields.String(description="Icon type"),
  45. "icon": fields.String(description="Icon"),
  46. "icon_background": fields.String(description="Icon background color"),
  47. "description": fields.String(description="Site description"),
  48. "default_language": fields.String(description="Default language"),
  49. "chat_color_theme": fields.String(description="Chat color theme"),
  50. "chat_color_theme_inverted": fields.Boolean(description="Inverted chat color theme"),
  51. "customize_domain": fields.String(description="Custom domain"),
  52. "copyright": fields.String(description="Copyright text"),
  53. "privacy_policy": fields.String(description="Privacy policy"),
  54. "custom_disclaimer": fields.String(description="Custom disclaimer"),
  55. "customize_token_strategy": fields.String(
  56. enum=["must", "allow", "not_allow"], description="Token strategy"
  57. ),
  58. "prompt_public": fields.Boolean(description="Make prompt public"),
  59. "show_workflow_steps": fields.Boolean(description="Show workflow steps"),
  60. "use_icon_as_answer_icon": fields.Boolean(description="Use icon as answer icon"),
  61. },
  62. )
  63. )
  64. @api.response(200, "Site configuration updated successfully", app_site_fields)
  65. @api.response(403, "Insufficient permissions")
  66. @api.response(404, "App not found")
  67. @setup_required
  68. @login_required
  69. @account_initialization_required
  70. @get_app_model
  71. @marshal_with(app_site_fields)
  72. def post(self, app_model):
  73. args = parse_app_site_args()
  74. # The role of the current user in the ta table must be editor, admin, or owner
  75. if not current_user.is_editor:
  76. raise Forbidden()
  77. site = db.session.query(Site).where(Site.app_id == app_model.id).first()
  78. if not site:
  79. raise NotFound
  80. for attr_name in [
  81. "title",
  82. "icon_type",
  83. "icon",
  84. "icon_background",
  85. "description",
  86. "default_language",
  87. "chat_color_theme",
  88. "chat_color_theme_inverted",
  89. "customize_domain",
  90. "copyright",
  91. "privacy_policy",
  92. "custom_disclaimer",
  93. "customize_token_strategy",
  94. "prompt_public",
  95. "show_workflow_steps",
  96. "use_icon_as_answer_icon",
  97. ]:
  98. value = args.get(attr_name)
  99. if value is not None:
  100. setattr(site, attr_name, value)
  101. if not isinstance(current_user, Account):
  102. raise ValueError("current_user must be an Account instance")
  103. site.updated_by = current_user.id
  104. site.updated_at = naive_utc_now()
  105. db.session.commit()
  106. return site
  107. @console_ns.route("/apps/<uuid:app_id>/site/access-token-reset")
  108. class AppSiteAccessTokenReset(Resource):
  109. @api.doc("reset_app_site_access_token")
  110. @api.doc(description="Reset access token for application site")
  111. @api.doc(params={"app_id": "Application ID"})
  112. @api.response(200, "Access token reset successfully", app_site_fields)
  113. @api.response(403, "Insufficient permissions (admin/owner required)")
  114. @api.response(404, "App or site not found")
  115. @setup_required
  116. @login_required
  117. @account_initialization_required
  118. @get_app_model
  119. @marshal_with(app_site_fields)
  120. def post(self, app_model):
  121. # The role of the current user in the ta table must be admin or owner
  122. if not current_user.is_admin_or_owner:
  123. raise Forbidden()
  124. site = db.session.query(Site).where(Site.app_id == app_model.id).first()
  125. if not site:
  126. raise NotFound
  127. site.code = Site.generate_code(16)
  128. if not isinstance(current_user, Account):
  129. raise ValueError("current_user must be an Account instance")
  130. site.updated_by = current_user.id
  131. site.updated_at = naive_utc_now()
  132. db.session.commit()
  133. return site