installed_app.py 5.0 KB

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