site.py 3.6 KB

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