installed_app.py 4.6 KB

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