def check_seats_save_team(sender, instance, **kwargs): """ Validate, increase or decrease the amount of used seats based on the roles """ instance.full_clean() if os.getenv('APP_BRANCH') == DEMO_BRANCH: return user = instance.workflow_user.user org = instance.workflow_user.organization if ROLE_ORGANIZATION_ADMIN in user.groups.values_list('name', flat=True): return used_seats = org.chargebee_used_seats count = WorkflowTeam.objects.filter( workflow_user=instance.workflow_user, role__name__in=[ROLE_PROGRAM_ADMIN, ROLE_PROGRAM_TEAM]).count() sub_id = org.chargebee_subscription_id if not sub_id: logger.info('The organization {} does not have a ' 'subscription'.format(org.name)) return user_gained_seat = False # If the user is a Program Admin or Member # They should have a seat in the subscription if count == 0 and instance.role.name in [ ROLE_PROGRAM_ADMIN, ROLE_PROGRAM_TEAM ]: user_gained_seat = True used_seats += 1 elif count == 1 and instance.role.name == ROLE_VIEW_ONLY: used_seats -= 1 org.chargebee_used_seats = used_seats org.save() # Load subscription data from ChargeBee try: result = Subscription.retrieve(sub_id) subscription = result.subscription except APIError as e: logger.warn(e) else: # Validate the amount of available seats based on the subscription available_seats = subscription.plan_quantity if org.chargebee_used_seats > available_seats and user_gained_seat: extra_context = { 'used_seats': used_seats, 'available_seats': available_seats, 'payment_portal_url': settings.PAYMENT_PORTAL_URL } notify_excess_org_admin(org, extra_context)
def check_connection(self, logger, config: Mapping[str, Any]) -> Tuple[bool, any]: # Configure the Chargebee Python SDK chargebee.configure( api_key=config["site_api_token"], site=config["site"], ) try: # Get one subscription to test connection Subscription.list( # Set limit # to test on a small dataset params={ "limit": 1, }, ) return True, None except Exception as err: # Should catch all exceptions # which are already handled by # Chargebee Python wrapper # https://github.com/chargebee/chargebee-python/blob/5346d833781de78a9eedbf9d12502f52c617c2d2/chargebee/http_request.py return False, str(err)
def check_seats_save_user_groups(sender, instance, **kwargs): """ Validate, increase or decrease the amount of used seats based on the roles """ if (os.getenv('APP_BRANCH') == DEMO_BRANCH or kwargs['action'] not in ['post_add', 'post_remove'] or kwargs['model'] != Group): return try: tola_user = TolaUser.objects.get(user=instance) except TolaUser.DoesNotExist as e: logger.info(e) else: changed_groups = Group.objects.values_list( 'name', flat=True).filter(id__in=kwargs['pk_set']) count = WorkflowTeam.objects.filter( workflow_user=tola_user, role__name__in=[ROLE_PROGRAM_ADMIN, ROLE_PROGRAM_TEAM]).count() # Update the amount of used seats org = tola_user.organization used_seats = org.chargebee_used_seats user_gained_seats = False # If the user is an Org Admin, he's able to edit the program. # Therefore, he should have a seat in the subscription if count == 0 and ROLE_ORGANIZATION_ADMIN in changed_groups: if kwargs['action'] == 'post_add': user_gained_seats = True used_seats += 1 elif kwargs['action'] == 'post_remove': used_seats -= 1 org.chargebee_used_seats = used_seats org.save() # Load subscription data from ChargeBee sub_id = org.chargebee_subscription_id if not sub_id: logger.info('The organization {} does not have a ' 'subscription'.format(tola_user.organization.name)) return try: result = Subscription.retrieve(sub_id) subscription = result.subscription except APIError as e: logger.warn(e) else: # Validate the amount of available seats based on the subscription available_seats = subscription.plan_quantity if org.chargebee_used_seats > available_seats \ and user_gained_seats: extra_context = { 'used_seats': used_seats, 'available_seats': available_seats, 'payment_portal_url': settings.PAYMENT_PORTAL_URL } notify_excess_org_admin(org, extra_context)
def __init__(self, values): self.subscription = Subscription(values) self.subscription.status = 'active' self.subscription.plan_quantity = 1