wraps.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. 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. documents_upload_quota = features.documents_upload_quota
  43. annotation_quota_limit = features.annotation_quota_limit
  44. if resource == 'members' and 0 < members.limit <= members.size:
  45. abort(403, error_msg)
  46. elif resource == 'apps' and 0 < apps.limit <= apps.size:
  47. abort(403, error_msg)
  48. elif resource == 'vector_space' and 0 < vector_space.limit <= vector_space.size:
  49. abort(403, error_msg)
  50. elif resource == 'documents' and 0 < documents_upload_quota.limit <= documents_upload_quota.size:
  51. # The api of file upload is used in the multiple places, 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, error_msg)
  55. else:
  56. return view(*args, **kwargs)
  57. elif resource == 'workspace_custom' and not features.can_replace_logo:
  58. abort(403, error_msg)
  59. elif resource == 'annotation' and 0 < annotation_quota_limit.limit < annotation_quota_limit.size:
  60. abort(403, error_msg)
  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. error_msg: str = "To unlock this feature and elevate your Dify experience, please upgrade to a paid plan."):
  68. def interceptor(view):
  69. @wraps(view)
  70. def decorated(*args, **kwargs):
  71. features = FeatureService.get_features(current_user.current_tenant_id)
  72. if features.billing.enabled:
  73. if resource == 'add_segment':
  74. if features.billing.subscription.plan == 'sandbox':
  75. abort(403, error_msg)
  76. else:
  77. return view(*args, **kwargs)
  78. return view(*args, **kwargs)
  79. return decorated
  80. return interceptor
  81. def cloud_utm_record(view):
  82. @wraps(view)
  83. def decorated(*args, **kwargs):
  84. try:
  85. features = FeatureService.get_features(current_user.current_tenant_id)
  86. if features.billing.enabled:
  87. utm_info = request.cookies.get('utm_info')
  88. if utm_info:
  89. utm_info = json.loads(utm_info)
  90. OperationService.record_utm(current_user.current_tenant_id, utm_info)
  91. except Exception as e:
  92. pass
  93. return view(*args, **kwargs)
  94. return decorated