wraps.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # -*- coding:utf-8 -*-
  2. from functools import wraps
  3. from controllers.console.workspace.error import AccountNotInitializedError
  4. from flask import abort, current_app
  5. from flask_login import current_user
  6. from services.feature_service import FeatureService
  7. def account_initialization_required(view):
  8. @wraps(view)
  9. def decorated(*args, **kwargs):
  10. # check account initialization
  11. account = current_user
  12. if account.status == 'uninitialized':
  13. raise AccountNotInitializedError()
  14. return view(*args, **kwargs)
  15. return decorated
  16. def only_edition_cloud(view):
  17. @wraps(view)
  18. def decorated(*args, **kwargs):
  19. if current_app.config['EDITION'] != 'CLOUD':
  20. abort(404)
  21. return view(*args, **kwargs)
  22. return decorated
  23. def only_edition_self_hosted(view):
  24. @wraps(view)
  25. def decorated(*args, **kwargs):
  26. if current_app.config['EDITION'] != 'SELF_HOSTED':
  27. abort(404)
  28. return view(*args, **kwargs)
  29. return decorated
  30. def cloud_edition_billing_resource_check(resource: str,
  31. error_msg: str = "You have reached the limit of your subscription."):
  32. def interceptor(view):
  33. @wraps(view)
  34. def decorated(*args, **kwargs):
  35. features = FeatureService.get_features(current_user.current_tenant_id)
  36. if features.billing.enabled:
  37. members = features.members
  38. apps = features.apps
  39. vector_space = features.vector_space
  40. annotation_quota_limit = features.annotation_quota_limit
  41. if resource == 'members' and 0 < members.limit <= members.size:
  42. abort(403, error_msg)
  43. elif resource == 'apps' and 0 < apps.limit <= apps.size:
  44. abort(403, error_msg)
  45. elif resource == 'vector_space' and 0 < vector_space.limit <= vector_space.size:
  46. abort(403, error_msg)
  47. elif resource == 'workspace_custom' and not features.can_replace_logo:
  48. abort(403, error_msg)
  49. elif resource == 'annotation' and 0 < annotation_quota_limit.limit < annotation_quota_limit.size:
  50. abort(403, error_msg)
  51. else:
  52. return view(*args, **kwargs)
  53. return view(*args, **kwargs)
  54. return decorated
  55. return interceptor