workspace.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # -*- coding:utf-8 -*-
  2. import logging
  3. from flask import request
  4. from flask_login import current_user
  5. from core.login.login import login_required
  6. from flask_restful import Resource, fields, marshal_with, reqparse, marshal, inputs
  7. from flask_restful.inputs import int_range
  8. from controllers.console import api
  9. from controllers.console.admin import admin_required
  10. from controllers.console.setup import setup_required
  11. from controllers.console.error import AccountNotLinkTenantError
  12. from controllers.console.wraps import account_initialization_required
  13. from libs.helper import TimestampField
  14. from extensions.ext_database import db
  15. from models.account import Tenant
  16. from services.account_service import TenantService
  17. from services.workspace_service import WorkspaceService
  18. provider_fields = {
  19. 'provider_name': fields.String,
  20. 'provider_type': fields.String,
  21. 'is_valid': fields.Boolean,
  22. 'token_is_set': fields.Boolean,
  23. }
  24. tenant_fields = {
  25. 'id': fields.String,
  26. 'name': fields.String,
  27. 'plan': fields.String,
  28. 'status': fields.String,
  29. 'created_at': TimestampField,
  30. 'role': fields.String,
  31. 'providers': fields.List(fields.Nested(provider_fields)),
  32. 'in_trial': fields.Boolean,
  33. 'trial_end_reason': fields.String,
  34. }
  35. tenants_fields = {
  36. 'id': fields.String,
  37. 'name': fields.String,
  38. 'plan': fields.String,
  39. 'status': fields.String,
  40. 'created_at': TimestampField,
  41. 'current': fields.Boolean
  42. }
  43. workspace_fields = {
  44. 'id': fields.String,
  45. 'name': fields.String,
  46. 'status': fields.String,
  47. 'created_at': TimestampField
  48. }
  49. class TenantListApi(Resource):
  50. @setup_required
  51. @login_required
  52. @account_initialization_required
  53. def get(self):
  54. tenants = TenantService.get_join_tenants(current_user)
  55. for tenant in tenants:
  56. if tenant.id == current_user.current_tenant_id:
  57. tenant.current = True # Set current=True for current tenant
  58. return {'workspaces': marshal(tenants, tenants_fields)}, 200
  59. class WorkspaceListApi(Resource):
  60. @setup_required
  61. @admin_required
  62. def get(self):
  63. parser = reqparse.RequestParser()
  64. parser.add_argument('page', type=inputs.int_range(1, 99999), required=False, default=1, location='args')
  65. parser.add_argument('limit', type=inputs.int_range(1, 100), required=False, default=20, location='args')
  66. args = parser.parse_args()
  67. tenants = db.session.query(Tenant).order_by(Tenant.created_at.desc())\
  68. .paginate(page=args['page'], per_page=args['limit'])
  69. has_more = False
  70. if len(tenants.items) == args['limit']:
  71. current_page_first_tenant = tenants[-1]
  72. rest_count = db.session.query(Tenant).filter(
  73. Tenant.created_at < current_page_first_tenant.created_at,
  74. Tenant.id != current_page_first_tenant.id
  75. ).count()
  76. if rest_count > 0:
  77. has_more = True
  78. total = db.session.query(Tenant).count()
  79. return {
  80. 'data': marshal(tenants.items, workspace_fields),
  81. 'has_more': has_more,
  82. 'limit': args['limit'],
  83. 'page': args['page'],
  84. 'total': total
  85. }, 200
  86. class TenantApi(Resource):
  87. @setup_required
  88. @login_required
  89. @account_initialization_required
  90. @marshal_with(tenant_fields)
  91. def get(self):
  92. if request.path == '/info':
  93. logging.warning('Deprecated URL /info was used.')
  94. tenant = current_user.current_tenant
  95. return WorkspaceService.get_tenant_info(tenant), 200
  96. class SwitchWorkspaceApi(Resource):
  97. @setup_required
  98. @login_required
  99. @account_initialization_required
  100. def post(self):
  101. parser = reqparse.RequestParser()
  102. parser.add_argument('tenant_id', type=str, required=True, location='json')
  103. args = parser.parse_args()
  104. # check if tenant_id is valid, 403 if not
  105. try:
  106. TenantService.switch_tenant(current_user, args['tenant_id'])
  107. except Exception:
  108. raise AccountNotLinkTenantError("Account not link tenant")
  109. new_tenant = db.session.query(Tenant).get(args['tenant_id']) # Get new tenant
  110. return {'result': 'success', 'new_tenant': marshal(WorkspaceService.get_tenant_info(new_tenant), tenant_fields)}
  111. api.add_resource(TenantListApi, '/workspaces') # GET for getting all tenants
  112. api.add_resource(WorkspaceListApi, '/all-workspaces') # GET for getting all tenants
  113. api.add_resource(TenantApi, '/workspaces/current', endpoint='workspaces_current') # GET for getting current tenant info
  114. api.add_resource(TenantApi, '/info', endpoint='info') # Deprecated
  115. api.add_resource(SwitchWorkspaceApi, '/workspaces/switch') # POST for switching tenant