wraps.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. 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. else:
  48. return view(*args, **kwargs)
  49. return view(*args, **kwargs)
  50. return decorated
  51. return interceptor