Skip to content

Commit 306edc0

Browse files
committed
refactor: replace hardcoded user plan strings with CloudPlan enum
- Create CloudPlan StrEnum with SANDBOX, PROFESSIONAL, and TEAM values - Replace all hardcoded 'sandbox' strings with CloudPlan.SANDBOX throughout backend - Update 18 production files and 4 test files for consistency - Use auto() for automatic lowercase value generation from enum names Fixes #27674
1 parent 4461df1 commit 306edc0

20 files changed

+66
-27
lines changed

api/controllers/console/billing/billing.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from controllers.console import console_ns
44
from controllers.console.wraps import account_initialization_required, only_edition_cloud, setup_required
5+
from enums.cloud_plan import CloudPlan
56
from libs.login import current_account_with_tenant, login_required
67
from services.billing_service import BillingService
78

@@ -16,7 +17,13 @@ def get(self):
1617
current_user, current_tenant_id = current_account_with_tenant()
1718
parser = (
1819
reqparse.RequestParser()
19-
.add_argument("plan", type=str, required=True, location="args", choices=["professional", "team"])
20+
.add_argument(
21+
"plan",
22+
type=str,
23+
required=True,
24+
location="args",
25+
choices=[CloudPlan.PROFESSIONAL, CloudPlan.TEAM],
26+
)
2027
.add_argument("interval", type=str, required=True, location="args", choices=["month", "year"])
2128
)
2229
args = parser.parse_args()

api/controllers/console/workspace/workspace.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
cloud_edition_billing_resource_check,
2222
setup_required,
2323
)
24+
from enums.cloud_plan import CloudPlan
2425
from extensions.ext_database import db
2526
from libs.helper import TimestampField
2627
from libs.login import current_account_with_tenant, login_required
@@ -83,7 +84,7 @@ def get(self):
8384
"name": tenant.name,
8485
"status": tenant.status,
8586
"created_at": tenant.created_at,
86-
"plan": features.billing.subscription.plan if features.billing.enabled else "sandbox",
87+
"plan": features.billing.subscription.plan if features.billing.enabled else CloudPlan.SANDBOX,
8788
"current": tenant.id == current_tenant_id if current_tenant_id else False,
8889
}
8990

api/controllers/console/wraps.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from configs import dify_config
1212
from controllers.console.workspace.error import AccountNotInitializedError
13+
from enums.cloud_plan import CloudPlan
1314
from extensions.ext_database import db
1415
from extensions.ext_redis import redis_client
1516
from libs.login import current_account_with_tenant
@@ -133,7 +134,7 @@ def decorated(*args: P.args, **kwargs: P.kwargs):
133134
features = FeatureService.get_features(current_tenant_id)
134135
if features.billing.enabled:
135136
if resource == "add_segment":
136-
if features.billing.subscription.plan == "sandbox":
137+
if features.billing.subscription.plan == CloudPlan.SANDBOX:
137138
abort(
138139
403,
139140
"To unlock this feature and elevate your Dify experience, please upgrade to a paid plan.",

api/controllers/service_api/wraps.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from sqlalchemy.orm import Session
1414
from werkzeug.exceptions import Forbidden, NotFound, Unauthorized
1515

16+
from enums.cloud_plan import CloudPlan
1617
from extensions.ext_database import db
1718
from extensions.ext_redis import redis_client
1819
from libs.datetime_utils import naive_utc_now
@@ -138,7 +139,7 @@ def decorated(*args: P.args, **kwargs: P.kwargs):
138139
features = FeatureService.get_features(api_token.tenant_id)
139140
if features.billing.enabled:
140141
if resource == "add_segment":
141-
if features.billing.subscription.plan == "sandbox":
142+
if features.billing.subscription.plan == CloudPlan.SANDBOX:
142143
raise Forbidden(
143144
"To unlock this feature and elevate your Dify experience, please upgrade to a paid plan."
144145
)

api/core/app/apps/pipeline/pipeline_generator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from core.workflow.repositories.workflow_execution_repository import WorkflowExecutionRepository
4141
from core.workflow.repositories.workflow_node_execution_repository import WorkflowNodeExecutionRepository
4242
from core.workflow.variable_loader import DUMMY_VARIABLE_LOADER, VariableLoader
43+
from enums.cloud_plan import CloudPlan
4344
from extensions.ext_database import db
4445
from extensions.ext_redis import redis_client
4546
from libs.flask_utils import preserve_flask_contexts
@@ -255,7 +256,7 @@ def generate(
255256
json_text = json.dumps(text)
256257
upload_file = FileService(db.engine).upload_text(json_text, name, user.id, dataset.tenant_id)
257258
features = FeatureService.get_features(dataset.tenant_id)
258-
if features.billing.enabled and features.billing.subscription.plan == "sandbox":
259+
if features.billing.enabled and features.billing.subscription.plan == CloudPlan.SANDBOX:
259260
tenant_pipeline_task_key = f"tenant_pipeline_task:{dataset.tenant_id}"
260261
tenant_self_pipeline_task_queue = f"tenant_self_pipeline_task_queue:{dataset.tenant_id}"
261262

api/enums/__init__.py

Whitespace-only changes.

api/enums/cloud_plan.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from enum import StrEnum, auto
2+
3+
4+
class CloudPlan(StrEnum):
5+
"""
6+
Enum representing user plan types in the cloud platform.
7+
8+
SANDBOX: Free/default plan with limited features
9+
PROFESSIONAL: Professional paid plan
10+
TEAM: Team collaboration paid plan
11+
"""
12+
13+
SANDBOX = auto()
14+
PROFESSIONAL = auto()
15+
TEAM = auto()

api/schedule/clean_messages.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import app
99
from configs import dify_config
10+
from enums.cloud_plan import CloudPlan
1011
from extensions.ext_database import db
1112
from extensions.ext_redis import redis_client
1213
from models.model import (
@@ -63,7 +64,7 @@ def clean_messages():
6364
plan = features.billing.subscription.plan
6465
else:
6566
plan = plan_cache.decode()
66-
if plan == "sandbox":
67+
if plan == CloudPlan.SANDBOX:
6768
# clean related message
6869
db.session.query(MessageFeedback).where(MessageFeedback.message_id == message.id).delete(
6970
synchronize_session=False

api/schedule/clean_unused_datasets_task.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import app
1010
from configs import dify_config
1111
from core.rag.index_processor.index_processor_factory import IndexProcessorFactory
12+
from enums.cloud_plan import CloudPlan
1213
from extensions.ext_database import db
1314
from extensions.ext_redis import redis_client
1415
from models.dataset import Dataset, DatasetAutoDisableLog, DatasetQuery, Document
@@ -35,7 +36,7 @@ def clean_unused_datasets_task():
3536
},
3637
{
3738
"clean_day": datetime.datetime.now() - datetime.timedelta(days=dify_config.PLAN_PRO_CLEAN_DAY_SETTING),
38-
"plan_filter": "sandbox",
39+
"plan_filter": CloudPlan.SANDBOX,
3940
"add_logs": False,
4041
},
4142
]

api/schedule/mail_clean_document_notify_task.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import app
99
from configs import dify_config
10+
from enums.cloud_plan import CloudPlan
1011
from extensions.ext_database import db
1112
from extensions.ext_mail import mail
1213
from libs.email_i18n import EmailType, get_email_i18n_service
@@ -45,7 +46,7 @@ def mail_clean_document_notify_task():
4546
for tenant_id, tenant_dataset_auto_disable_logs in dataset_auto_disable_logs_map.items():
4647
features = FeatureService.get_features(tenant_id)
4748
plan = features.billing.subscription.plan
48-
if plan != "sandbox":
49+
if plan != CloudPlan.SANDBOX:
4950
knowledge_details = []
5051
# check tenant
5152
tenant = db.session.query(Tenant).where(Tenant.id == tenant_id).first()

0 commit comments

Comments
 (0)