installed_app.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. from datetime import datetime, timezone
  2. from flask_login import current_user
  3. from flask_restful import Resource, inputs, marshal_with, reqparse
  4. from sqlalchemy import and_
  5. from werkzeug.exceptions import BadRequest, Forbidden, NotFound
  6. from controllers.console import api
  7. from controllers.console.explore.wraps import InstalledAppResource
  8. from controllers.console.wraps import account_initialization_required, cloud_edition_billing_resource_check
  9. from extensions.ext_database import db
  10. from fields.installed_app_fields import installed_app_list_fields
  11. from libs.login import login_required
  12. from models.model import App, InstalledApp, RecommendedApp
  13. from services.account_service import TenantService
  14. class InstalledAppsListApi(Resource):
  15. @login_required
  16. @account_initialization_required
  17. @marshal_with(installed_app_list_fields)
  18. def get(self):
  19. current_tenant_id = current_user.current_tenant_id
  20. installed_apps = db.session.query(InstalledApp).filter(
  21. InstalledApp.tenant_id == current_tenant_id
  22. ).all()
  23. current_user.role = TenantService.get_user_role(current_user, current_user.current_tenant)
  24. installed_apps = [
  25. {
  26. 'id': installed_app.id,
  27. 'app': installed_app.app,
  28. 'app_owner_tenant_id': installed_app.app_owner_tenant_id,
  29. 'is_pinned': installed_app.is_pinned,
  30. 'last_used_at': installed_app.last_used_at,
  31. 'editable': current_user.role in ["owner", "admin"],
  32. 'uninstallable': current_tenant_id == installed_app.app_owner_tenant_id
  33. }
  34. for installed_app in installed_apps
  35. ]
  36. installed_apps.sort(key=lambda app: (-app['is_pinned'],
  37. app['last_used_at'] is None,
  38. -app['last_used_at'].timestamp() if app['last_used_at'] is not None else 0))
  39. return {'installed_apps': installed_apps}
  40. @login_required
  41. @account_initialization_required
  42. @cloud_edition_billing_resource_check('apps')
  43. def post(self):
  44. parser = reqparse.RequestParser()
  45. parser.add_argument('app_id', type=str, required=True, help='Invalid app_id')
  46. args = parser.parse_args()
  47. recommended_app = RecommendedApp.query.filter(RecommendedApp.app_id == args['app_id']).first()
  48. if recommended_app is None:
  49. raise NotFound('App not found')
  50. current_tenant_id = current_user.current_tenant_id
  51. app = db.session.query(App).filter(
  52. App.id == args['app_id']
  53. ).first()
  54. if app is None:
  55. raise NotFound('App not found')
  56. if not app.is_public:
  57. raise Forbidden('You can\'t install a non-public app')
  58. installed_app = InstalledApp.query.filter(and_(
  59. InstalledApp.app_id == args['app_id'],
  60. InstalledApp.tenant_id == current_tenant_id
  61. )).first()
  62. if installed_app is None:
  63. # todo: position
  64. recommended_app.install_count += 1
  65. new_installed_app = InstalledApp(
  66. app_id=args['app_id'],
  67. tenant_id=current_tenant_id,
  68. app_owner_tenant_id=app.tenant_id,
  69. is_pinned=False,
  70. last_used_at=datetime.now(timezone.utc).replace(tzinfo=None)
  71. )
  72. db.session.add(new_installed_app)
  73. db.session.commit()
  74. return {'message': 'App installed successfully'}
  75. class InstalledAppApi(InstalledAppResource):
  76. """
  77. update and delete an installed app
  78. use InstalledAppResource to apply default decorators and get installed_app
  79. """
  80. def delete(self, installed_app):
  81. if installed_app.app_owner_tenant_id == current_user.current_tenant_id:
  82. raise BadRequest('You can\'t uninstall an app owned by the current tenant')
  83. db.session.delete(installed_app)
  84. db.session.commit()
  85. return {'result': 'success', 'message': 'App uninstalled successfully'}
  86. def patch(self, installed_app):
  87. parser = reqparse.RequestParser()
  88. parser.add_argument('is_pinned', type=inputs.boolean)
  89. args = parser.parse_args()
  90. commit_args = False
  91. if 'is_pinned' in args:
  92. installed_app.is_pinned = args['is_pinned']
  93. commit_args = True
  94. if commit_args:
  95. db.session.commit()
  96. return {'result': 'success', 'message': 'App info updated successfully'}
  97. api.add_resource(InstalledAppsListApi, '/installed-apps')
  98. api.add_resource(InstalledAppApi, '/installed-apps/<uuid:installed_app_id>')