site.py 3.8 KB

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