wraps.py 3.0 KB

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