wraps.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. documents_upload_quota = features.documents_upload_quota
  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 == 'documents' and 0 < documents_upload_quota.limit <= documents_upload_quota.size:
  50. # The api of file upload is used in the multiple places, so we need to check the source of the request from datasets
  51. source = request.args.get('source')
  52. if source == 'datasets':
  53. abort(403, error_msg)
  54. else:
  55. return view(*args, **kwargs)
  56. elif resource == 'workspace_custom' and not features.can_replace_logo:
  57. abort(403, error_msg)
  58. elif resource == 'annotation' and 0 < annotation_quota_limit.limit < annotation_quota_limit.size:
  59. abort(403, error_msg)
  60. else:
  61. return view(*args, **kwargs)
  62. return view(*args, **kwargs)
  63. return decorated
  64. return interceptor
  65. def cloud_utm_record(view):
  66. @wraps(view)
  67. def decorated(*args, **kwargs):
  68. try:
  69. features = FeatureService.get_features(current_user.current_tenant_id)
  70. if features.billing.enabled:
  71. utm_info = request.cookies.get('utm_info')
  72. if utm_info:
  73. utm_info = json.loads(utm_info)
  74. OperationService.record_utm(current_user.current_tenant_id, utm_info)
  75. except Exception as e:
  76. pass
  77. return view(*args, **kwargs)
  78. return decorated