installed_app.py 4.6 KB

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