def handle_membership_updated(sender, instance, **kwargs): try: rocket = RocketChatConnection() is_pending = instance.status in (MEMBERSHIP_PENDING, MEMBERSHIP_INVITED_PENDING) if instance.id: old_instance = CosinnusGroupMembership.objects.get( pk=instance.id) was_pending = old_instance.status in ( MEMBERSHIP_PENDING, MEMBERSHIP_INVITED_PENDING) user_changed = instance.user_id != old_instance.user_id group_changed = instance.group_id != old_instance.group_id is_moderator_changed = instance.status != old_instance.status and \ (instance.status == MEMBERSHIP_ADMIN or old_instance.status == MEMBERSHIP_ADMIN) # Invalidate old membership if (is_pending and not was_pending) or user_changed or group_changed: rocket.groups_kick(old_instance) # Create new membership if (was_pending and not is_pending) or user_changed or group_changed: rocket.groups_invite(instance) # Update membership if not is_pending and is_moderator_changed: # Upgrade if not old_instance.status == MEMBERSHIP_ADMIN and instance.status == MEMBERSHIP_ADMIN: rocket.groups_add_moderator(instance) # Downgrade elif old_instance.status == MEMBERSHIP_ADMIN and not instance.status == MEMBERSHIP_ADMIN: rocket.groups_remove_moderator(instance) elif not is_pending: # Create new membership rocket.groups_invite(instance) if instance.status == MEMBERSHIP_ADMIN: rocket.groups_add_moderator(instance) except Exception as e: logger.exception(e)
def handle_membership_deleted(sender, instance, **kwargs): try: rocket = RocketChatConnection() rocket.groups_kick(instance) except Exception as e: logger.exception(e)