admin.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import os
  2. from functools import wraps
  3. from flask import request
  4. from flask_restful import Resource, reqparse
  5. from werkzeug.exceptions import NotFound, Unauthorized
  6. from controllers.console import api
  7. from controllers.console.wraps import only_edition_cloud
  8. from extensions.ext_database import db
  9. from models.model import RecommendedApp, App, InstalledApp
  10. def admin_required(view):
  11. @wraps(view)
  12. def decorated(*args, **kwargs):
  13. if not os.getenv('ADMIN_API_KEY'):
  14. raise Unauthorized('API key is invalid.')
  15. auth_header = request.headers.get('Authorization')
  16. if auth_header is None:
  17. raise Unauthorized('Authorization header is missing.')
  18. if ' ' not in auth_header:
  19. raise Unauthorized('Invalid Authorization header format. Expected \'Bearer <api-key>\' format.')
  20. auth_scheme, auth_token = auth_header.split(None, 1)
  21. auth_scheme = auth_scheme.lower()
  22. if auth_scheme != 'bearer':
  23. raise Unauthorized('Invalid Authorization header format. Expected \'Bearer <api-key>\' format.')
  24. if os.getenv('ADMIN_API_KEY') != auth_token:
  25. raise Unauthorized('API key is invalid.')
  26. return view(*args, **kwargs)
  27. return decorated
  28. class InsertExploreAppListApi(Resource):
  29. @only_edition_cloud
  30. @admin_required
  31. def post(self):
  32. parser = reqparse.RequestParser()
  33. parser.add_argument('app_id', type=str, required=True, nullable=False, location='json')
  34. parser.add_argument('desc', type=str, location='json')
  35. parser.add_argument('copyright', type=str, location='json')
  36. parser.add_argument('privacy_policy', type=str, location='json')
  37. parser.add_argument('language', type=str, required=True, nullable=False, choices=['en-US', 'zh-Hans'],
  38. location='json')
  39. parser.add_argument('category', type=str, required=True, nullable=False, location='json')
  40. parser.add_argument('position', type=int, required=True, nullable=False, location='json')
  41. args = parser.parse_args()
  42. app = App.query.filter(App.id == args['app_id']).first()
  43. if not app:
  44. raise NotFound('App not found')
  45. site = app.site
  46. if not site:
  47. desc = args['desc'] if args['desc'] else ''
  48. copy_right = args['copyright'] if args['copyright'] else ''
  49. privacy_policy = args['privacy_policy'] if args['privacy_policy'] else ''
  50. else:
  51. desc = site.description if (site.description if not args['desc'] else args['desc']) else ''
  52. copy_right = site.copyright if (site.copyright if not args['copyright'] else args['copyright']) else ''
  53. privacy_policy = site.privacy_policy \
  54. if (site.privacy_policy if not args['privacy_policy'] else args['privacy_policy']) else ''
  55. recommended_app = RecommendedApp.query.filter(RecommendedApp.app_id == args['app_id']).first()
  56. if not recommended_app:
  57. recommended_app = RecommendedApp(
  58. app_id=app.id,
  59. description=desc,
  60. copyright=copy_right,
  61. privacy_policy=privacy_policy,
  62. language=args['language'],
  63. category=args['category'],
  64. position=args['position']
  65. )
  66. db.session.add(recommended_app)
  67. app.is_public = True
  68. db.session.commit()
  69. return {'result': 'success'}, 201
  70. else:
  71. recommended_app.description = desc
  72. recommended_app.copyright = copy_right
  73. recommended_app.privacy_policy = privacy_policy
  74. recommended_app.language = args['language']
  75. recommended_app.category = args['category']
  76. recommended_app.position = args['position']
  77. app.is_public = True
  78. db.session.commit()
  79. return {'result': 'success'}, 200
  80. class InsertExploreAppApi(Resource):
  81. @only_edition_cloud
  82. @admin_required
  83. def delete(self, app_id):
  84. recommended_app = RecommendedApp.query.filter(RecommendedApp.app_id == str(app_id)).first()
  85. if not recommended_app:
  86. return {'result': 'success'}, 204
  87. app = App.query.filter(App.id == recommended_app.app_id).first()
  88. if app:
  89. app.is_public = False
  90. installed_apps = InstalledApp.query.filter(
  91. InstalledApp.app_id == recommended_app.app_id,
  92. InstalledApp.tenant_id != InstalledApp.app_owner_tenant_id
  93. ).all()
  94. for installed_app in installed_apps:
  95. db.session.delete(installed_app)
  96. db.session.delete(recommended_app)
  97. db.session.commit()
  98. return {'result': 'success'}, 204
  99. api.add_resource(InsertExploreAppListApi, '/admin/insert-explore-apps')
  100. api.add_resource(InsertExploreAppApi, '/admin/insert-explore-apps/<uuid:app_id>')