workspace.py 4.6 KB

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