<commit_before>#
# Copyright (c) 2018 Red Hat
# Licensed under The MIT License (MIT)
# http://opensource.org/licenses/MIT
#

import django_filters
from django.db.models import Q

from pdc.apps.common.filters import CaseInsensitiveBooleanFilter, MultiValueFilter, value_is_not_empty
from pdc.apps.module.models import Module
from pdc.apps.package.models import RPM


class ModuleComponentFilter(MultiValueFilter):
    """
    Multi-value filter for modules with an RPM with given name and branch.
    """
    @value_is_not_empty
    def _filter(self, qs, name, values):
        query = Q()

        for value in values:
            try:
                name, branch = value.split('/', 2)
                filters = {'name': name, 'srpm_commit_branch': branch}
            except ValueError:
                filters = {'name': value}

            rpms = RPM.objects.filter(**filters)
            query |= Q(rpms__in=rpms)

        qs = qs.filter(query)

        return qs
<commit_msg>Make multiple component filter more effective<commit_after>#
# Copyright (c) 2018 Red Hat
# Licensed under The MIT License (MIT)
# http://opensource.org/licenses/MIT
#

import django_filters
from django.db.models import Q

from pdc.apps.common.filters import CaseInsensitiveBooleanFilter, MultiValueFilter, value_is_not_empty
from pdc.apps.module.models import Module
from pdc.apps.package.models import RPM


class ModuleComponentFilter(MultiValueFilter):
    """
    Multi-value filter for modules with an RPM with given name and branch.
    """
    @value_is_not_empty
    def _filter(self, qs, name, values):
        query = Q()

        for value in values:
            try:
                name, branch = value.split('/', 2)
                filters = {'name': name, 'srpm_commit_branch': branch}
            except ValueError:
                filters = {'name': value}

            query |= Q(**filters)

        rpms = RPM.objects.filter(query)
        qs = qs.filter(rpms__in=rpms)

        return qs

{'fraction_non_alphanumeric': 0.0964824120603015, 'fraction_numerical': 0.005025125628140704, 'mean_word_length': 2.62, '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': '6243970', 'n_tokens_mistral': 596, 'n_tokens_neox': 565, 'n_words': 169}