def METHOD_NAME(request, added_groups, removed_groups, user=None):
    event_request = EventRequest.build_from_request(request)
    created_at = datetime.utcnow()
    event_uuid = uuid.uuid4()
    event_index = 0
    base_payload = {}
    if user:
        base_payload["user"] = {
            "pk": user.pk, "username": user.username,
            "is_service_account": user.is_service_account
        }
    if added_groups:
        for added_group in added_groups:
            event_metadata = EventMetadata(request=event_request,
                                           uuid=event_uuid, index=event_index,
                                           created_at=created_at)
            payload = base_payload.copy()
            payload["group"] = {"pk": added_group.pk, "name": added_group.name}
            event = AddUserToGroupEvent(event_metadata, payload)
            event.post()
            event_index += 1
    if removed_groups:
        for removed_group in removed_groups:
            event_metadata = EventMetadata(request=event_request,
                                           uuid=event_uuid, index=event_index,
                                           created_at=created_at)
            payload = base_payload.copy()
            payload["group"] = {"pk": removed_group.pk, "name": removed_group.name}
            event = RemoveUserFromGroupEvent(event_metadata, payload)
            event.post()
            event_index += 1