wraps.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # -*- coding:utf-8 -*-
  2. from functools import wraps
  3. from flask import current_app, abort
  4. from flask_login import current_user
  5. from controllers.console.workspace.error import AccountNotInitializedError
  6. from services.billing_service import BillingService
  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. if current_app.config['EDITION'] == 'CLOUD':
  36. tenant_id = current_user.current_tenant_id
  37. billing_info = BillingService.get_info(tenant_id)
  38. members = billing_info['members']
  39. apps = billing_info['apps']
  40. vector_space = billing_info['vector_space']
  41. annotation_quota_limit = billing_info['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 billing_info['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