<commit_before>from django.utils.http import urlencode
from django.urls import reverse, NoReverseMatch


def make_filter_url(url_name, data=None, get_active_filters=None):
    if data is None:
        data = {}
    data = dict(data)
    url_kwargs = {}

    if get_active_filters is None:
        active_filters = {}
    else:
        active_filters = get_active_filters(data)

    for key in active_filters:
        url_kwargs[key] = data.pop(key)

    query_string = ''
    data = {k: v for k, v in data.items() if v}
    if data:
        query_string = '?' + urlencode(data)
    try:
        return reverse(url_name, kwargs=url_kwargs) + query_string
    except NoReverseMatch:
        return ''


def get_facet_with_label(info, model=None, attr='name'):
    pks = [item['key'] for item in info['buckets']]
    objs = {
        o.pk: o for o in model.objects.filter(pk__in=pks)
    }
    for item in info['buckets']:
        yield {
            'label': getattr(objs[item['key']], attr),
            'id': item['key'],
            'count': item['doc_count']
        }


def key_getter(item):
    return item['key']


class FakeObject():
    def __getattr__(self, name):
        return ''


def resolve_facet(data, getter=None, label_getter=None,
                query_param=None, model=None, make_url=None):
    if getter is None:
        getter = key_getter

    if label_getter is None:
        label_getter = key_getter

    def resolve(key, info):
        query_key = query_param or key
        if model is not None:
            pks = [item['key'] for item in info['buckets']]
            objs = {
                str(o.pk): o for o in model._default_manager.filter(pk__in=pks)
            }
            for item in info['buckets']:
                if item['key'] in objs:
                    item['object'] = objs[item['key']]
                else:
                    item['object'] = FakeObject()
        for item in info['buckets']:
            item['active'] = getter(item) == data.get(query_key)
            item['label'] = label_getter(item)
            d = data.copy()
            d[query_key] = getter(item)
            item['url'] = make_url(d)
            d.pop(query_key)
            item['clear_url'] = make_url(d)
        return info<commit_msg>Fix facet display with different types of pks<commit_after>from django.utils.http import urlencode
from django.urls import reverse, NoReverseMatch


def make_filter_url(url_name, data=None, get_active_filters=None):
    if data is None:
        data = {}
    data = dict(data)
    url_kwargs = {}

    if get_active_filters is None:
        active_filters = {}
    else:
        active_filters = get_active_filters(data)

    for key in active_filters:
        url_kwargs[key] = data.pop(key)

    query_string = ''
    data = {k: v for k, v in data.items() if v}
    if data:
        query_string = '?' + urlencode(data)
    try:
        return reverse(url_name, kwargs=url_kwargs) + query_string
    except NoReverseMatch:
        return ''


def get_facet_with_label(info, model=None, attr='name'):
    pks = [item['key'] for item in info['buckets']]
    objs = {
        o.pk: o for o in model.objects.filter(pk__in=pks)
    }
    for item in info['buckets']:
        yield {
            'label': getattr(objs[item['key']], attr),
            'id': item['key'],
            'count': item['doc_count']
        }


def key_getter(item):
    return item['key']


class FakeObject():
    def __getattr__(self, name):
        return ''


def resolve_facet(data, getter=None, label_getter=None,
                query_param=None, model=None, make_url=None):
    if getter is None:
        getter = key_getter

    if label_getter is None:
        label_getter = key_getter

    def resolve(key, info):
        query_key = query_param or key
        if model is not None:
            pks = [item['key'] for item in info['buckets']]
            objs = {
                str(o.pk): o for o in model._default_manager.filter(pk__in=pks)
            }
            for item in info['buckets']:
                item_key = str(item['key'])
                if item_key in objs:
                    item['object'] = objs[item_key]
                else:
                    item['object'] = FakeObject()
        for item in info['buckets']:
            item['active'] = getter(item) == data.get(query_key)
            item['label'] = label_getter(item)
            d = data.copy()
            d[query_key] = getter(item)
            item['url'] = make_url(d)
            d.pop(query_key)
            item['clear_url'] = make_url(d)
        return info
{'fraction_non_alphanumeric': 0.11842677096914385, 'fraction_numerical': 0.0, 'mean_word_length': 2.0954942837928714, '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': '3492404', 'n_tokens_mistral': 1493, 'n_tokens_neox': 1448, 'n_words': 356}