wraps.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import json
  2. from functools import wraps
  3. from flask import abort, current_app, request
  4. from flask_login import current_user
  5. from controllers.console.workspace.error import AccountNotInitializedError
  6. from services.feature_service import FeatureService
  7. from services.operation_service import OperationService
  8. def account_initialization_required(view):
  9. @wraps(view)
  10. def decorated(*args, **kwargs):
  11. # check account initialization
  12. account = current_user
  13. if account.status == 'uninitialized':
  14. raise AccountNotInitializedError()
  15. return view(*args, **kwargs)
  16. return decorated
  17. def only_edition_cloud(view):
  18. @wraps(view)
  19. def decorated(*args, **kwargs):
  20. if current_app.config['EDITION'] != 'CLOUD':
  21. abort(404)
  22. return view(*args, **kwargs)
  23. return decorated
  24. def only_edition_self_hosted(view):
  25. @wraps(view)
  26. def decorated(*args, **kwargs):
  27. if current_app.config['EDITION'] != 'SELF_HOSTED':
  28. abort(404)
  29. return view(*args, **kwargs)
  30. return decorated
  31. def cloud_edition_billing_resource_check(resource: str,
  32. error_msg: str = "You have reached the limit of your subscription."):
  33. def interceptor(view):
  34. @wraps(view)
  35. def decorated(*args, **kwargs):
  36. features = FeatureService.get_features(current_user.current_tenant_id)
  37. if features.billing.enabled:
  38. members = features.members
  39. apps = features.apps
  40. vector_space = features.vector_space
  41. annotation_quota_limit = features.annotation_quota_limit
  42. if resource == 'members' and 0 < members.limit <= members.size:
  43. abort(403, error_msg)
  44. elif resource == 'apps' and 0 < apps.limit <= apps.size:
  45. abort(403, error_msg)
  46. elif resource == 'vector_space' and 0 < vector_space.limit <= vector_space.size:
  47. abort(403, error_msg)
  48. elif resource == 'workspace_custom' and not features.can_replace_logo:
  49. abort(403, error_msg)
  50. elif resource == 'annotation' and 0 < annotation_quota_limit.limit < annotation_quota_limit.size:
  51. abort(403, error_msg)
  52. else:
  53. return view(*args, **kwargs)
  54. return view(*args, **kwargs)
  55. return decorated
  56. return interceptor
  57. def cloud_utm_record(view):
  58. @wraps(view)
  59. def decorated(*args, **kwargs):
  60. try:
  61. features = FeatureService.get_features(current_user.current_tenant_id)
  62. if features.billing.enabled:
  63. utm_info = request.cookies.get('utm_info')
  64. if utm_info:
  65. utm_info = json.loads(utm_info)
  66. OperationService.record_utm(current_user.current_tenant_id, utm_info)
  67. except Exception as e:
  68. pass
  69. return view(*args, **kwargs)
  70. return decorated