fengmiguoji commited on
Commit
ce5f18e
·
verified ·
1 Parent(s): 85bbf66

Upload api\controllers\console\wraps.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. api//controllers//console//wraps.py +161 -0
api//controllers//console//wraps.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ from functools import wraps
4
+
5
+ from flask import abort, request
6
+ from flask_login import current_user # type: ignore
7
+
8
+ from configs import dify_config
9
+ from controllers.console.workspace.error import AccountNotInitializedError
10
+ from extensions.ext_database import db
11
+ from models.model import DifySetup
12
+ from services.feature_service import FeatureService, LicenseStatus
13
+ from services.operation_service import OperationService
14
+
15
+ from .error import NotInitValidateError, NotSetupError, UnauthorizedAndForceLogout
16
+
17
+
18
+ def account_initialization_required(view):
19
+ @wraps(view)
20
+ def decorated(*args, **kwargs):
21
+ # check account initialization
22
+ account = current_user
23
+
24
+ if account.status == "uninitialized":
25
+ raise AccountNotInitializedError()
26
+
27
+ return view(*args, **kwargs)
28
+
29
+ return decorated
30
+
31
+
32
+ def only_edition_cloud(view):
33
+ @wraps(view)
34
+ def decorated(*args, **kwargs):
35
+ if dify_config.EDITION != "CLOUD":
36
+ abort(404)
37
+
38
+ return view(*args, **kwargs)
39
+
40
+ return decorated
41
+
42
+
43
+ def only_edition_self_hosted(view):
44
+ @wraps(view)
45
+ def decorated(*args, **kwargs):
46
+ if dify_config.EDITION != "SELF_HOSTED":
47
+ abort(404)
48
+
49
+ return view(*args, **kwargs)
50
+
51
+ return decorated
52
+
53
+
54
+ def cloud_edition_billing_resource_check(resource: str):
55
+ def interceptor(view):
56
+ @wraps(view)
57
+ def decorated(*args, **kwargs):
58
+ features = FeatureService.get_features(current_user.current_tenant_id)
59
+ if features.billing.enabled:
60
+ members = features.members
61
+ apps = features.apps
62
+ vector_space = features.vector_space
63
+ documents_upload_quota = features.documents_upload_quota
64
+ annotation_quota_limit = features.annotation_quota_limit
65
+ if resource == "members" and 0 < members.limit <= members.size:
66
+ abort(403, "The number of members has reached the limit of your subscription.")
67
+ elif resource == "apps" and 0 < apps.limit <= apps.size:
68
+ abort(403, "The number of apps has reached the limit of your subscription.")
69
+ elif resource == "vector_space" and 0 < vector_space.limit <= vector_space.size:
70
+ abort(403, "The capacity of the vector space has reached the limit of your subscription.")
71
+ elif resource == "documents" and 0 < documents_upload_quota.limit <= documents_upload_quota.size:
72
+ # The api of file upload is used in the multiple places,
73
+ # so we need to check the source of the request from datasets
74
+ source = request.args.get("source")
75
+ if source == "datasets":
76
+ abort(403, "The number of documents has reached the limit of your subscription.")
77
+ else:
78
+ return view(*args, **kwargs)
79
+ elif resource == "workspace_custom" and not features.can_replace_logo:
80
+ abort(403, "The workspace custom feature has reached the limit of your subscription.")
81
+ elif resource == "annotation" and 0 < annotation_quota_limit.limit < annotation_quota_limit.size:
82
+ abort(403, "The annotation quota has reached the limit of your subscription.")
83
+ else:
84
+ return view(*args, **kwargs)
85
+
86
+ return view(*args, **kwargs)
87
+
88
+ return decorated
89
+
90
+ return interceptor
91
+
92
+
93
+ def cloud_edition_billing_knowledge_limit_check(resource: str):
94
+ def interceptor(view):
95
+ @wraps(view)
96
+ def decorated(*args, **kwargs):
97
+ features = FeatureService.get_features(current_user.current_tenant_id)
98
+ if features.billing.enabled:
99
+ if resource == "add_segment":
100
+ if features.billing.subscription.plan == "sandbox":
101
+ abort(
102
+ 403,
103
+ "To unlock this feature and elevate your Dify experience, please upgrade to a paid plan.",
104
+ )
105
+ else:
106
+ return view(*args, **kwargs)
107
+
108
+ return view(*args, **kwargs)
109
+
110
+ return decorated
111
+
112
+ return interceptor
113
+
114
+
115
+ def cloud_utm_record(view):
116
+ @wraps(view)
117
+ def decorated(*args, **kwargs):
118
+ try:
119
+ features = FeatureService.get_features(current_user.current_tenant_id)
120
+
121
+ if features.billing.enabled:
122
+ utm_info = request.cookies.get("utm_info")
123
+
124
+ if utm_info:
125
+ utm_info_dict: dict = json.loads(utm_info)
126
+ OperationService.record_utm(current_user.current_tenant_id, utm_info_dict)
127
+ except Exception as e:
128
+ pass
129
+ return view(*args, **kwargs)
130
+
131
+ return decorated
132
+
133
+
134
+ def setup_required(view):
135
+ @wraps(view)
136
+ def decorated(*args, **kwargs):
137
+ # check setup
138
+ if (
139
+ dify_config.EDITION == "SELF_HOSTED"
140
+ and os.environ.get("INIT_PASSWORD")
141
+ and not db.session.query(DifySetup).first()
142
+ ):
143
+ raise NotInitValidateError()
144
+ elif dify_config.EDITION == "SELF_HOSTED" and not db.session.query(DifySetup).first():
145
+ raise NotSetupError()
146
+
147
+ return view(*args, **kwargs)
148
+
149
+ return decorated
150
+
151
+
152
+ def enterprise_license_required(view):
153
+ @wraps(view)
154
+ def decorated(*args, **kwargs):
155
+ settings = FeatureService.get_system_features()
156
+ if settings.license.status in [LicenseStatus.INACTIVE, LicenseStatus.EXPIRED, LicenseStatus.LOST]:
157
+ raise UnauthorizedAndForceLogout("Your license is invalid. Please contact your administrator.")
158
+
159
+ return view(*args, **kwargs)
160
+
161
+ return decorated