dify/api/services/workspace_service.py

48 lines
1.6 KiB
Python
Raw Normal View History

2023-08-15 13:35:47 +08:00
from flask_login import current_user
2023-05-15 08:51:32 +08:00
from extensions.ext_database import db
2023-12-18 16:25:37 +08:00
from models.account import Tenant, TenantAccountJoin, TenantAccountJoinRole
from models.provider import Provider
2023-05-15 08:51:32 +08:00
2023-12-18 16:25:37 +08:00
from services.billing_service import BillingService
from services.account_service import TenantService
2023-05-15 08:51:32 +08:00
class WorkspaceService:
@classmethod
def get_tenant_info(cls, tenant: Tenant):
2023-08-15 13:35:47 +08:00
if not tenant:
return None
2023-05-15 08:51:32 +08:00
tenant_info = {
'id': tenant.id,
'name': tenant.name,
'plan': tenant.plan,
'status': tenant.status,
'created_at': tenant.created_at,
'providers': [],
2023-08-15 13:35:47 +08:00
'in_trail': True,
'trial_end_reason': None,
'role': 'normal',
2023-05-15 08:51:32 +08:00
}
2023-08-15 13:35:47 +08:00
# Get role of user
tenant_account_join = db.session.query(TenantAccountJoin).filter(
TenantAccountJoin.tenant_id == tenant.id,
TenantAccountJoin.account_id == current_user.id
).first()
tenant_info['role'] = tenant_account_join.role
2023-12-18 16:25:37 +08:00
billing_info = BillingService.get_info(tenant_info['id'])
if billing_info['can_replace_logo'] and TenantService.has_roles(tenant, [TenantAccountJoinRole.OWNER, TenantAccountJoinRole.ADMIN]):
tenant_info['custom_config'] = tenant.custom_config_dict
2023-05-15 08:51:32 +08:00
# Get providers
providers = db.session.query(Provider).filter(
Provider.tenant_id == tenant.id
).all()
# Add providers to the tenant info
tenant_info['providers'] = providers
return tenant_info