site.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.wraps import get_app_model
  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('custom_disclaimer', type=str, required=False, location='json')
  24. parser.add_argument('customize_token_strategy', type=str, choices=['must', 'allow', 'not_allow'],
  25. required=False,
  26. location='json')
  27. parser.add_argument('prompt_public', type=bool, required=False, location='json')
  28. parser.add_argument('show_workflow_steps', type=bool, required=False, location='json')
  29. return parser.parse_args()
  30. class AppSite(Resource):
  31. @setup_required
  32. @login_required
  33. @account_initialization_required
  34. @get_app_model
  35. @marshal_with(app_site_fields)
  36. def post(self, app_model):
  37. args = parse_app_site_args()
  38. # The role of the current user in the ta table must be editor, admin, or owner
  39. if not current_user.is_editor:
  40. raise Forbidden()
  41. site = db.session.query(Site). \
  42. filter(Site.app_id == app_model.id). \
  43. one_or_404()
  44. for attr_name in [
  45. 'title',
  46. 'icon',
  47. 'icon_background',
  48. 'description',
  49. 'default_language',
  50. 'customize_domain',
  51. 'copyright',
  52. 'privacy_policy',
  53. 'custom_disclaimer',
  54. 'customize_token_strategy',
  55. 'prompt_public',
  56. 'show_workflow_steps'
  57. ]:
  58. value = args.get(attr_name)
  59. if value is not None:
  60. setattr(site, attr_name, value)
  61. db.session.commit()
  62. return site
  63. class AppSiteAccessTokenReset(Resource):
  64. @setup_required
  65. @login_required
  66. @account_initialization_required
  67. @get_app_model
  68. @marshal_with(app_site_fields)
  69. def post(self, app_model):
  70. # The role of the current user in the ta table must be admin or owner
  71. if not current_user.is_admin_or_owner:
  72. raise Forbidden()
  73. site = db.session.query(Site).filter(Site.app_id == app_model.id).first()
  74. if not site:
  75. raise NotFound
  76. site.code = Site.generate_code(16)
  77. db.session.commit()
  78. return site
  79. api.add_resource(AppSite, '/apps/<uuid:app_id>/site')
  80. api.add_resource(AppSiteAccessTokenReset, '/apps/<uuid:app_id>/site/access-token-reset')