wraps.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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(resource: str):
  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, "The number of members has reached the limit of your subscription.")
  45. elif resource == "apps" and 0 < apps.limit <= apps.size:
  46. abort(403, "The number of apps has reached the limit of your subscription.")
  47. elif resource == "vector_space" and 0 < vector_space.limit <= vector_space.size:
  48. abort(403, "The capacity of the vector space has reached the limit of your subscription.")
  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,
  51. # so we need to check the source of the request from datasets
  52. source = request.args.get("source")
  53. if source == "datasets":
  54. abort(403, "The number of documents has reached the limit of your subscription.")
  55. else:
  56. return view(*args, **kwargs)
  57. elif resource == "workspace_custom" and not features.can_replace_logo:
  58. abort(403, "The workspace custom feature has reached the limit of your subscription.")
  59. elif resource == "annotation" and 0 < annotation_quota_limit.limit < annotation_quota_limit.size:
  60. abort(403, "The annotation quota has reached the limit of your subscription.")
  61. else:
  62. return view(*args, **kwargs)
  63. return view(*args, **kwargs)
  64. return decorated
  65. return interceptor
  66. def cloud_edition_billing_knowledge_limit_check(resource: str):
  67. def interceptor(view):
  68. @wraps(view)
  69. def decorated(*args, **kwargs):
  70. features = FeatureService.get_features(current_user.current_tenant_id)
  71. if features.billing.enabled:
  72. if resource == "add_segment":
  73. if features.billing.subscription.plan == "sandbox":
  74. abort(
  75. 403,
  76. "To unlock this feature and elevate your Dify experience, please upgrade to a paid plan.",
  77. )
  78. else:
  79. return view(*args, **kwargs)
  80. return view(*args, **kwargs)
  81. return decorated
  82. return interceptor
  83. def cloud_utm_record(view):
  84. @wraps(view)
  85. def decorated(*args, **kwargs):
  86. try:
  87. features = FeatureService.get_features(current_user.current_tenant_id)
  88. if features.billing.enabled:
  89. utm_info = request.cookies.get("utm_info")
  90. if utm_info:
  91. utm_info = json.loads(utm_info)
  92. OperationService.record_utm(current_user.current_tenant_id, utm_info)
  93. except Exception as e:
  94. pass
  95. return view(*args, **kwargs)
  96. return decorated