site.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # -*- coding:utf-8 -*-
  2. from flask_login import current_user
  3. from core.login.login import login_required
  4. from flask_restful import Resource, reqparse, fields, marshal_with
  5. from werkzeug.exceptions import NotFound, Forbidden
  6. from controllers.console import api
  7. from controllers.console.app import _get_app
  8. from controllers.console.setup import setup_required
  9. from controllers.console.wraps import account_initialization_required
  10. from libs.helper import supported_language
  11. from extensions.ext_database import db
  12. from models.model import Site
  13. app_site_fields = {
  14. 'app_id': fields.String,
  15. 'access_token': fields.String(attribute='code'),
  16. 'code': fields.String,
  17. 'title': fields.String,
  18. 'icon': fields.String,
  19. 'icon_background': fields.String,
  20. 'description': fields.String,
  21. 'default_language': fields.String,
  22. 'customize_domain': fields.String,
  23. 'copyright': fields.String,
  24. 'privacy_policy': fields.String,
  25. 'customize_token_strategy': fields.String,
  26. 'prompt_public': fields.Boolean
  27. }
  28. def parse_app_site_args():
  29. parser = reqparse.RequestParser()
  30. parser.add_argument('title', type=str, required=False, location='json')
  31. parser.add_argument('icon', type=str, required=False, location='json')
  32. parser.add_argument('icon_background', type=str, required=False, location='json')
  33. parser.add_argument('description', type=str, required=False, location='json')
  34. parser.add_argument('default_language', type=supported_language, required=False, location='json')
  35. parser.add_argument('customize_domain', type=str, required=False, location='json')
  36. parser.add_argument('copyright', type=str, required=False, location='json')
  37. parser.add_argument('privacy_policy', type=str, required=False, location='json')
  38. parser.add_argument('customize_token_strategy', type=str, choices=['must', 'allow', 'not_allow'],
  39. required=False,
  40. location='json')
  41. parser.add_argument('prompt_public', type=bool, required=False, location='json')
  42. return parser.parse_args()
  43. class AppSite(Resource):
  44. @setup_required
  45. @login_required
  46. @account_initialization_required
  47. @marshal_with(app_site_fields)
  48. def post(self, app_id):
  49. args = parse_app_site_args()
  50. app_id = str(app_id)
  51. app_model = _get_app(app_id)
  52. # The role of the current user in the ta table must be admin or owner
  53. if current_user.current_tenant.current_role not in ['admin', 'owner']:
  54. raise Forbidden()
  55. site = db.session.query(Site). \
  56. filter(Site.app_id == app_model.id). \
  57. one_or_404()
  58. for attr_name in [
  59. 'title',
  60. 'icon',
  61. 'icon_background',
  62. 'description',
  63. 'default_language',
  64. 'customize_domain',
  65. 'copyright',
  66. 'privacy_policy',
  67. 'customize_token_strategy',
  68. 'prompt_public'
  69. ]:
  70. value = args.get(attr_name)
  71. if value is not None:
  72. setattr(site, attr_name, value)
  73. if attr_name == 'title':
  74. app_model.name = value
  75. elif attr_name == 'icon':
  76. app_model.icon = value
  77. elif attr_name == 'icon_background':
  78. app_model.icon_background = value
  79. db.session.commit()
  80. return site
  81. class AppSiteAccessTokenReset(Resource):
  82. @setup_required
  83. @login_required
  84. @account_initialization_required
  85. @marshal_with(app_site_fields)
  86. def post(self, app_id):
  87. app_id = str(app_id)
  88. app_model = _get_app(app_id)
  89. # The role of the current user in the ta table must be admin or owner
  90. if current_user.current_tenant.current_role not in ['admin', 'owner']:
  91. raise Forbidden()
  92. site = db.session.query(Site).filter(Site.app_id == app_model.id).first()
  93. if not site:
  94. raise NotFound
  95. site.code = Site.generate_code(16)
  96. db.session.commit()
  97. return site
  98. api.add_resource(AppSite, '/apps/<uuid:app_id>/site')
  99. api.add_resource(AppSiteAccessTokenReset, '/apps/<uuid:app_id>/site/access-token-reset')