<commit_before># -*- coding: utf-8 -*-
# This file is part of Shuup.
#
# Copyright (c) 2012-2016, Shoop Commerce 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.db.models import ManyToManyField

from shuup.apps.provides import get_provide_objects
from shuup.core.models import Category, Product, ProductType, ShopProduct


def get_matching_for_product(shop_product, provide_category):
    """
    Get matching ids for shop product based on provide category

    For example:
    matching_ids = get_matching_for_product(shop_product, "campaign_catalog_filter")

    :param shop_product: A Shop Product
    :type shop_product: shuup.core.models.ShopProduct
    :param provide_category: Provide category name
    :type provide_category: str
    :return: list of collected item ids
    :rtype: list[int]
    """
    collected = set()
    matcher = ProductCampaignMatcher(shop_product)
    for item in get_provide_objects(provide_category):
        for obj in item._meta.model.objects.all():
            if matcher.matches(obj):
                collected.add(obj.pk)
    return collected


class ProductCampaignMatcher(object):
    """
    A class to match shop product to existing campaigns
    """

    def __init__(self, shop_product, **kwargs):
        """
        :param shop_product: a Shop Product to find matches for<commit_msg>Add skippable_classes param to matcher<commit_after># -*- coding: utf-8 -*-
# This file is part of Shuup.
#
# Copyright (c) 2012-2016, Shoop Commerce 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.db.models import ManyToManyField

from shuup.apps.provides import get_provide_objects
from shuup.core.models import Category, Product, ProductType, ShopProduct


def get_matching_for_product(shop_product, provide_category, skippable_classes=None):
    """
    Get matching ids for shop product based on provide category

    For example:
    matching_ids = get_matching_for_product(shop_product, "campaign_catalog_filter")

    :param shop_product: A Shop Product
    :type shop_product: shuup.core.models.ShopProduct
    :param provide_category: Provide category name
    :type provide_category: str
    :param skip: Classes to skip
    :type skip: None or list
    :return: list of collected item ids
    :rtype: list[int]
    """
    collected = set()
    matcher = ProductCampaignMatcher(shop_product)
    for item in get_provide_objects(provide_category):
        if skippable_classes:
            objects = item._meta.model.objects.not_instance_of(*skippable_classes).all()
        else:
            objects = item._meta.model.objects.all()
        for obj in objects:
            if matcher.matches(obj):
                collected.add(obj.pk)
    return collected


class ProductCampaignMatcher(object):
    """
    A class to match shop product to existing campaigns
    """

    def __init__(self, shop_product, **kwargs):
        """
        :param shop_product: a Shop Product to find matches for
{'fraction_non_alphanumeric': 0.07813484562066793, 'fraction_numerical': 0.00630119722747322, 'mean_word_length': 3.4844632768361583, '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': '5694829', 'n_tokens_mistral': 918, 'n_tokens_neox': 882, 'n_words': 297}