wraps.py 3.0 KB

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