From 598b59c0c692d7936ea1036c5e990a4ea924aa51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 24 Aug 2017 11:19:07 +0200 Subject: [PATCH] Orgs: gracefully handle 'not enough seats' error --- pillar/api/organizations/patch.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/pillar/api/organizations/patch.py b/pillar/api/organizations/patch.py index 100a00b8..3cb9fedb 100644 --- a/pillar/api/organizations/patch.py +++ b/pillar/api/organizations/patch.py @@ -24,6 +24,7 @@ class OrganizationPatchHandler(patch_handler.AbstractPatchHandler): The calling user must be admin of the organization. """ + from . import NotEnoughSeats self._assert_is_admin(org_id) @@ -40,7 +41,13 @@ class OrganizationPatchHandler(patch_handler.AbstractPatchHandler): log.info('User %s uses PATCH to add users to organization %s', current_user().user_id, org_id) - org_doc = current_app.org_manager.assign_users(org_id, emails) + try: + org_doc = current_app.org_manager.assign_users(org_id, emails) + except NotEnoughSeats: + resp = jsonify({'_message': f'Not enough seats to assign {len(emails)} users'}) + resp.status_code = 422 + return resp + return jsonify(org_doc) @authorization.require_login() @@ -49,7 +56,7 @@ class OrganizationPatchHandler(patch_handler.AbstractPatchHandler): The calling user must be admin of the organization. """ - + from . import NotEnoughSeats self._assert_is_admin(org_id) # Do some basic validation. @@ -61,7 +68,13 @@ class OrganizationPatchHandler(patch_handler.AbstractPatchHandler): user_oid = str2id(user_id) log.info('User %s uses PATCH to add user %s to organization %s', current_user().user_id, user_oid, org_id) - org_doc = current_app.org_manager.assign_single_user(org_id, user_id=user_oid) + try: + org_doc = current_app.org_manager.assign_single_user(org_id, user_id=user_oid) + except NotEnoughSeats: + resp = jsonify({'_message': f'Not enough seats to assign this user'}) + resp.status_code = 422 + return resp + return jsonify(org_doc) @authorization.require_login()