wraps.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import json
  2. from functools import wraps
  3. from flask import abort, request
  4. from flask_login import current_user
  5. from configs import dify_config
  6. from controllers.console.workspace.error import AccountNotInitializedError
  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 dify_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 dify_config.EDITION != "SELF_HOSTED":
  29. abort(404)
  30. return view(*args, **kwargs)
  31. return decorated
  32. def cloud_edition_billing_resource_check(
  33. resource: str, error_msg: str = "You have reached the limit of your subscription."
  34. ):
  35. def interceptor(view):
  36. @wraps(view)
  37. def decorated(*args, **kwargs):
  38. features = FeatureService.get_features(current_user.current_tenant_id)
  39. if features.billing.enabled:
  40. members = features.members
  41. apps = features.apps
  42. vector_space = features.vector_space
  43. documents_upload_quota = features.documents_upload_quota
  44. annotation_quota_limit = features.annotation_quota_limit
  45. if resource == "members" and 0 < members.limit <= members.size:
  46. abort(403, error_msg)
  47. elif resource == "apps" and 0 < apps.limit <= apps.size:
  48. abort(403, error_msg)
  49. elif resource == "vector_space" and 0 < vector_space.limit <= vector_space.size:
  50. abort(403, error_msg)
  51. elif resource == "documents" and 0 < documents_upload_quota.limit <= documents_upload_quota.size:
  52. # The api of file upload is used in the multiple places, so we need to check the source of the request from datasets
  53. source = request.args.get("source")
  54. if source == "datasets":
  55. abort(403, error_msg)
  56. else:
  57. return view(*args, **kwargs)
  58. elif resource == "workspace_custom" and not features.can_replace_logo:
  59. abort(403, error_msg)
  60. elif resource == "annotation" and 0 < annotation_quota_limit.limit < annotation_quota_limit.size:
  61. abort(403, error_msg)
  62. else:
  63. return view(*args, **kwargs)
  64. return view(*args, **kwargs)
  65. return decorated
  66. return interceptor
  67. def cloud_edition_billing_knowledge_limit_check(
  68. resource: str,
  69. error_msg: str = "To unlock this feature and elevate your Dify experience, please upgrade to a paid plan.",
  70. ):
  71. def interceptor(view):
  72. @wraps(view)
  73. def decorated(*args, **kwargs):
  74. features = FeatureService.get_features(current_user.current_tenant_id)
  75. if features.billing.enabled:
  76. if resource == "add_segment":
  77. if features.billing.subscription.plan == "sandbox":
  78. abort(403, error_msg)
  79. else:
  80. return view(*args, **kwargs)
  81. return view(*args, **kwargs)
  82. return decorated
  83. return interceptor
  84. def cloud_utm_record(view):
  85. @wraps(view)
  86. def decorated(*args, **kwargs):
  87. try:
  88. features = FeatureService.get_features(current_user.current_tenant_id)
  89. if features.billing.enabled:
  90. utm_info = request.cookies.get("utm_info")
  91. if utm_info:
  92. utm_info = json.loads(utm_info)
  93. OperationService.record_utm(current_user.current_tenant_id, utm_info)
  94. except Exception as e:
  95. pass
  96. return view(*args, **kwargs)
  97. return decorated