<commit_before>import os

from django.db import transaction

from WhatManager2.locking import LockModelTables
from WhatManager2.utils import norm_t_torrent, dummy_context_manager
from home.models import WhatTorrent, TransTorrent, TorrentAlreadyAddedException, LogEntry, \
    ReplicaSet


def add_torrent(request, instance, download_location, what_id, add_to_client=True):
    w_torrent = WhatTorrent.get_or_create(request, what_id=what_id)

    masters = list(ReplicaSet.get_what_master().transinstance_set.all())
    with LockModelTables(TransTorrent, LogEntry):
        try:
            TransTorrent.objects.get(instance__in=masters, info_hash=w_torrent.info_hash)
            raise TorrentAlreadyAddedException(u'Already added.')
        except TransTorrent.DoesNotExist:
            pass

        if add_to_client:
            manager = transaction.atomic
        else:
            manager = dummy_context_manager

        with manager():
            if True:
                m_torrent = TransTorrent(
                    instance=instance,
                    location=download_location,
                    what_torrent=w_torrent,
                    info_hash=w_torrent.info_hash,
                )
                m_torrent.save()

                if add_to_client:<commit_msg>Fix Already added error if it's syncing.<commit_after>import os

from django.db import transaction

from WhatManager2.locking import LockModelTables
from WhatManager2.utils import norm_t_torrent, dummy_context_manager
from home.models import WhatTorrent, TransTorrent, TorrentAlreadyAddedException, LogEntry, \
    ReplicaSet


def add_torrent(request, instance, download_location, what_id, add_to_client=True):
    w_torrent = WhatTorrent.get_or_create(request, what_id=what_id)

    masters = list(ReplicaSet.get_what_master().transinstance_set.all())
    with LockModelTables(TransTorrent, LogEntry):
        if add_to_client:
            try:
                TransTorrent.objects.get(instance__in=masters, info_hash=w_torrent.info_hash)
                raise TorrentAlreadyAddedException(u'Already added.')
            except TransTorrent.DoesNotExist:
                pass

        if add_to_client:
            manager = transaction.atomic
        else:
            manager = dummy_context_manager

        with manager():
            if True:
                m_torrent = TransTorrent(
                    instance=instance,
                    location=download_location,
                    what_torrent=w_torrent,
                    info_hash=w_torrent.info_hash,
                )
                m_torrent.save()

                if add_to_client:
{'fraction_non_alphanumeric': 0.06313799621928166, 'fraction_numerical': 0.0015122873345935729, 'mean_word_length': 2.4097938144329896, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4014672', 'n_tokens_mistral': 751, 'n_tokens_neox': 717, 'n_words': 139}