installed_app.py 4.9 KB

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