installed_app.py 4.9 KB

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