workspace.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # -*- coding:utf-8 -*-
  2. import logging
  3. from flask import request
  4. from flask_login import login_required, current_user
  5. from flask_restful import Resource, fields, marshal_with, reqparse, marshal
  6. from controllers.console import api
  7. from controllers.console.setup import setup_required
  8. from controllers.console.error import AccountNotLinkTenantError
  9. from controllers.console.wraps import account_initialization_required
  10. from libs.helper import TimestampField
  11. from extensions.ext_database import db
  12. from models.account import Tenant
  13. from services.account_service import TenantService
  14. from services.workspace_service import WorkspaceService
  15. provider_fields = {
  16. 'provider_name': fields.String,
  17. 'provider_type': fields.String,
  18. 'is_valid': fields.Boolean,
  19. 'token_is_set': fields.Boolean,
  20. }
  21. tenant_fields = {
  22. 'id': fields.String,
  23. 'name': fields.String,
  24. 'plan': fields.String,
  25. 'status': fields.String,
  26. 'created_at': TimestampField,
  27. 'role': fields.String,
  28. 'providers': fields.List(fields.Nested(provider_fields)),
  29. 'in_trail': fields.Boolean,
  30. 'trial_end_reason': fields.String,
  31. }
  32. tenants_fields = {
  33. 'id': fields.String,
  34. 'name': fields.String,
  35. 'plan': fields.String,
  36. 'status': fields.String,
  37. 'created_at': TimestampField,
  38. 'current': fields.Boolean
  39. }
  40. class TenantListApi(Resource):
  41. @setup_required
  42. @login_required
  43. @account_initialization_required
  44. def get(self):
  45. tenants = TenantService.get_join_tenants(current_user)
  46. for tenant in tenants:
  47. if tenant.id == current_user.current_tenant_id:
  48. tenant.current = True # Set current=True for current tenant
  49. return {'workspaces': marshal(tenants, tenants_fields)}, 200
  50. class TenantApi(Resource):
  51. @setup_required
  52. @login_required
  53. @account_initialization_required
  54. @marshal_with(tenant_fields)
  55. def get(self):
  56. if request.path == '/info':
  57. logging.warning('Deprecated URL /info was used.')
  58. tenant = current_user.current_tenant
  59. return WorkspaceService.get_tenant_info(tenant), 200
  60. class SwitchWorkspaceApi(Resource):
  61. @setup_required
  62. @login_required
  63. @account_initialization_required
  64. def post(self):
  65. parser = reqparse.RequestParser()
  66. parser.add_argument('tenant_id', type=str, required=True, location='json')
  67. args = parser.parse_args()
  68. # check if tenant_id is valid, 403 if not
  69. try:
  70. TenantService.switch_tenant(current_user, args['tenant_id'])
  71. except Exception:
  72. raise AccountNotLinkTenantError("Account not link tenant")
  73. new_tenant = db.session.query(Tenant).get(args['tenant_id']) # Get new tenant
  74. return {'result': 'success', 'new_tenant': marshal(WorkspaceService.get_tenant_info(new_tenant), tenant_fields)}
  75. api.add_resource(TenantListApi, '/workspaces') # GET for getting all tenants
  76. api.add_resource(TenantApi, '/workspaces/current', endpoint='workspaces_current') # GET for getting current tenant info
  77. api.add_resource(TenantApi, '/info', endpoint='info') # Deprecated
  78. api.add_resource(SwitchWorkspaceApi, '/workspaces/switch') # POST for switching tenant