installed_app.py 4.9 KB

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