<commit_before># This file is part of Shoop.
#
# Copyright (c) 2012-2016, Shoop Ltd. All rights reserved.
#
# This source code is licensed under the AGPLv3 license found in the
# LICENSE file in the root directory of this source tree.

from django.core.management.base import BaseCommand
from django.db.transaction import atomic

from shoop.core.models import (
    OrderLine, OrderLineType, ProductCrossSell, ProductCrossSellType
)
from shoop.front.utils.product_relations import \
    add_bought_with_relations_for_product


class Command(BaseCommand):

    @atomic
    def handle(self, *args, **options):
        # Clear all existing ProductCrossSell objects
        ProductCrossSell.objects.filter(type=ProductCrossSellType.BOUGHT_WITH).delete()

        # Handle all ordered products
        ordered_product_ids = OrderLine.objects.filter(type=OrderLineType.PRODUCT).values_list("pk", flat=True)
        for product_id in ordered_product_ids:
            add_bought_with_relations_for_product(product_id)<commit_msg>Front: Fix the ``compute_bought_with_relations`` management command<commit_after># This file is part of Shoop.
#
# Copyright (c) 2012-2016, Shoop Ltd. All rights reserved.
#
# This source code is licensed under the AGPLv3 license found in the
# LICENSE file in the root directory of this source tree.

from django.core.management.base import BaseCommand
from django.db.transaction import atomic

from shoop.core.models import (
    OrderLine, OrderLineType, ProductCrossSell, ProductCrossSellType
)
from shoop.front.utils.product_relations import \
    add_bought_with_relations_for_product


class Command(BaseCommand):

    @atomic
    def handle(self, *args, **options):
        # Clear all existing ProductCrossSell objects
        ProductCrossSell.objects.filter(type=ProductCrossSellType.BOUGHT_WITH).delete()

        # Handle all ordered products
        ordered_product_ids = OrderLine.objects.filter(type=OrderLineType.PRODUCT).values_list("product_id", flat=True)
        seen_product_ids = []
        for product_id in ordered_product_ids:
            if product_id in seen_product_ids:
                continue
            seen_product_ids.append(product_id)
            add_bought_with_relations_for_product(product_id)
{'fraction_non_alphanumeric': 0.07009760425909495, 'fraction_numerical': 0.007985803016858917, 'mean_word_length': 4.101809954751131, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3435608', 'n_tokens_mistral': 659, 'n_tokens_neox': 631, 'n_words': 173}