workspace.py 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. from flask_restful import Resource, reqparse
  2. from controllers.console.wraps import setup_required
  3. from controllers.inner_api import api
  4. from controllers.inner_api.wraps import inner_api_only
  5. from events.tenant_event import tenant_was_created
  6. from models.account import Account
  7. from services.account_service import TenantService
  8. class EnterpriseWorkspace(Resource):
  9. @setup_required
  10. @inner_api_only
  11. def post(self):
  12. parser = reqparse.RequestParser()
  13. parser.add_argument("name", type=str, required=True, location="json")
  14. parser.add_argument("owner_email", type=str, required=True, location="json")
  15. args = parser.parse_args()
  16. account = Account.query.filter_by(email=args["owner_email"]).first()
  17. if account is None:
  18. return {"message": "owner account not found."}, 404
  19. tenant = TenantService.create_tenant(args["name"], is_from_dashboard=True)
  20. TenantService.create_tenant_member(tenant, account, role="owner")
  21. tenant_was_created.send(tenant)
  22. return {"message": "enterprise workspace created."}
  23. api.add_resource(EnterpriseWorkspace, "/enterprise/workspace")