<commit_before># -*- coding: utf-8 -*-
# This file is part of Shuup.
#
# Copyright (c) 2012-2020, Shoop Commerce Ltd. All rights reserved.
#
# This source code is licensed under the OSL-3.0 license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import unicode_literals

import datetime
import json

import six
from django.core.paginator import EmptyPage, Paginator
from django.db.models import Manager, Q, QuerySet
from django.http.response import HttpResponse, JsonResponse
from django.template.defaultfilters import yesno
from django.utils.encoding import force_text
from django.utils.html import escape
from django.utils.translation import ugettext_lazy as _
from easy_thumbnails.files import get_thumbnailer
from filer.models import Image

from shuup.admin.utils.urls import get_model_url, NoModelUrl
from shuup.apps.provides import get_provide_objects
from shuup.core.models import ProductMedia
from shuup.utils.dates import try_parse_datetime
from shuup.utils.i18n import format_money, get_locally_formatted_datetime
from shuup.utils.importing import load
from shuup.utils.money import Money
from shuup.utils.objects import compact
from shuup.utils.serialization import ExtendedJSONEncoder


def maybe_callable(thing, context=None):
    """
    If `thing` is callable, return it.
    If `thing` names a callable attribute of `context`, return it.
    """
    if callable(thing):
        return thing

    if isinstance(thing, six.string_types):
        thing = getattr(context, thing, None)
        if callable(thing):
            return thing

    return None


def maybe_call(thing, context, args=None, kwargs=None):
    """
    If `thing` is callable, call it with args and kwargs and return the value.
    If `thing` names a callable attribute of `context`, call it with args and kwargs and return the value.
    Otherwise return `thing`.
    """
    func = maybe_callable(context=context, thing=thing)
    if func:
        thing = func(*(args or ()), **(kwargs or {}))
    return thing


class Filter(object):
    type = None
    filter_field = None

    def get_filter_field(self, column, context):
        if self.filter_field:
            return self.filter_field
        if column.context and not maybe_callable(column.display, context=context):
            return column.display
        return column.id

    def to_json(self, context):
        return None

    def filter_queryset(self, queryset, column, value, context):
        return queryset  # pragma: no cover


class ChoicesFilter(Filter):
    type = "choices"

    def __init__(self, choices=None, filter_field=None, default=None):
        self.filter_field = filter_field
        self.choices = choices
        self.default = default

    def _flatten_choices(self, context):
        if not self.choices:
            return None
        choices = maybe_call(self.choices, context=context)
        if isinstance(choices, QuerySet):
            choices = [(c.pk, c) for c in choices]
        return [("_all", "---------")] + [
            (force_text(value, strings_only=True), force_text(display).title())
            for (value, display)<commit_msg>Admin: show picotable item choices as is<commit_after># -*- coding: utf-8 -*-
# This file is part of Shuup.
#
# Copyright (c) 2012-2020, Shoop Commerce Ltd. All rights reserved.
#
# This source code is licensed under the OSL-3.0 license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import unicode_literals

import datetime
import json

import six
from django.core.paginator import EmptyPage, Paginator
from django.db.models import Manager, Q, QuerySet
from django.http.response import HttpResponse, JsonResponse
from django.template.defaultfilters import yesno
from django.utils.encoding import force_text
from django.utils.html import escape
from django.utils.translation import ugettext_lazy as _
from easy_thumbnails.files import get_thumbnailer
from filer.models import Image

from shuup.admin.utils.urls import get_model_url, NoModelUrl
from shuup.apps.provides import get_provide_objects
from shuup.core.models import ProductMedia
from shuup.utils.dates import try_parse_datetime
from shuup.utils.i18n import format_money, get_locally_formatted_datetime
from shuup.utils.importing import load
from shuup.utils.money import Money
from shuup.utils.objects import compact
from shuup.utils.serialization import ExtendedJSONEncoder


def maybe_callable(thing, context=None):
    """
    If `thing` is callable, return it.
    If `thing` names a callable attribute of `context`, return it.
    """
    if callable(thing):
        return thing

    if isinstance(thing, six.string_types):
        thing = getattr(context, thing, None)
        if callable(thing):
            return thing

    return None


def maybe_call(thing, context, args=None, kwargs=None):
    """
    If `thing` is callable, call it with args and kwargs and return the value.
    If `thing` names a callable attribute of `context`, call it with args and kwargs and return the value.
    Otherwise return `thing`.
    """
    func = maybe_callable(context=context, thing=thing)
    if func:
        thing = func(*(args or ()), **(kwargs or {}))
    return thing


class Filter(object):
    type = None
    filter_field = None

    def get_filter_field(self, column, context):
        if self.filter_field:
            return self.filter_field
        if column.context and not maybe_callable(column.display, context=context):
            return column.display
        return column.id

    def to_json(self, context):
        return None

    def filter_queryset(self, queryset, column, value, context):
        return queryset  # pragma: no cover


class ChoicesFilter(Filter):
    type = "choices"

    def __init__(self, choices=None, filter_field=None, default=None):
        self.filter_field = filter_field
        self.choices = choices
        self.default = default

    def _flatten_choices(self, context):
        if not self.choices:
            return None
        choices = maybe_call(self.choices, context=context)
        if isinstance(choices, QuerySet):
            choices = [(c.pk, c) for c in choices]
        return [("_all", "---------")] + [
            (force_text(value, strings_only=True), force_text(display))
            for (value, display)
{'fraction_non_alphanumeric': 0.08442274373917152, 'fraction_numerical': 0.004095133091825485, 'mean_word_length': 3.3433652530779754, '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': '3372831', 'n_tokens_mistral': 1838, 'n_tokens_neox': 1794, 'n_words': 607}