# Buggy code
def get_files_to_transfer(channel_id, node_ids, exclude_node_ids, available):
    files_to_transfer = LocalFile.objects.filter(
        files__contentnode__channel_id=channel_id, available=available
    )

    if node_ids:
        leaf_node_ids = _get_leaves_ids(node_ids)
        files_to_transfer = files_to_transfer.filter(
            files__contentnode__in=leaf_node_ids
        )

    if exclude_node_ids:
        exclude_leaf_node_ids = _get_leaves_ids(exclude_node_ids)
        files_to_transfer = files_to_transfer.exclude(
            files__contentnode__in=exclude_leaf_node_ids
        )

    # Make sure the files are unique, to avoid duplicating downloads
    files_to_transfer = files_to_transfer.distinct()

    total_bytes_to_transfer = (
        files_to_transfer.aggregate(Sum("file_size"))["file_size__sum"] or 0
    )

    return files_to_transfer, total_bytes_to_transfer


# Traceback (OperationalError)
�[33mWARNING  Job d416c174faeb4648b22bd1420a75f2a0 raised an exception: Traceback (most recent call last):
File "c:\python27\lib\site-packages\kolibri\dist\iceqube\worker\backends\inmem.py", line 75, in handle_finished_future
result = future.result()
File "c:\python27\lib\site-packages\kolibri\dist\concurrent\futures\_base.py", line 455, in result
return self.__get_result()
File "c:\python27\lib\site-packages\kolibri\dist\concurrent\futures\thread.py", line 63, in run
result = self.fn(*self.args, **self.kwargs)
File "c:\python27\lib\site-packages\kolibri\dist\iceqube\worker\backends\inmem.py", line 149, in wrap
raise e
OperationalError: too many SQL variables
�[0m
WARNING:iceqube.worker.backends.base:Job d416c174faeb4648b22bd1420a75f2a0 raised an exception: Traceback (most recent call last):
File "c:\python27\lib\site-packages\kolibri\dist\iceqube\worker\backends\inmem.py", line 75, in handle_finished_future
result = future.result()
File "c:\python27\lib\site-packages\kolibri\dist\concurrent\futures\_base.py", line 455, in result
return self.__get_result()
File "c:\python27\lib\site-packages\kolibri\dist\concurrent\futures\thread.py", line 63, in run
result = self.fn(*self.args, **self.kwargs)
File "c:\python27\lib\site-packages\kolibri\dist\iceqube\worker\backends\inmem.py", line 149, in wrap
raise e
OperationalError: too many SQL variables

# Fixed code
def get_files_to_transfer(channel_id, node_ids, exclude_node_ids, available):
    files_to_transfer = LocalFile.objects.filter(
        files__contentnode__channel_id=channel_id, available=available
    )

    if node_ids:
        leaf_node_ids = _get_leaf_node_ids(node_ids)
        files_to_transfer = files_to_transfer.filter(
            files__contentnode__in=leaf_node_ids
        )

    if exclude_node_ids:
        exclude_leaf_node_ids = _get_leaf_node_ids(exclude_node_ids)
        files_to_transfer = files_to_transfer.exclude(
            files__contentnode__in=exclude_leaf_node_ids
        )

    # Make sure the files are unique, to avoid duplicating downloads
    files_to_transfer = files_to_transfer.distinct()

    total_bytes_to_transfer = (
        files_to_transfer.aggregate(Sum("file_size"))["file_size__sum"] or 0
    )

    return files_to_transfer, total_bytes_to_transfer