<commit_before>#
#    Autocomplete feature for admin panel
#
#    Most of the code has been written by <NAME>:
#    http://jannisleidel.com/2008/11/autocomplete-form-widget-foreignkey-model-fields/
#
#    to_string_function, Satchmo adaptation and some comments added by emes (<NAME>)
#
from django import forms
from django.db import models
from django.db.models.query import QuerySet
from django.conf import settings
from django.contrib import admin
from django.http import HttpResponse, HttpResponseNotFound
from django.utils.encoding import smart_str
from django.utils.safestring import mark_safe
try:
    from functools import update_wrapper
except ImportError:
    from django.utils.functional import update_wrapper
# Commenting out for Django 1.3 compatibility
# TODO: Need to find long term solution
#from django.contrib.admin.templatetags.admin_static import static

import operator

class ForeignKeySearchInput(forms.HiddenInput):
    """
    A Widget for displaying ForeignKeys in an autocomplete search input
    instead in a <select> box.
    """

    try:
        from django.utils.text import truncate_words
        to_string_function = lambda s: truncate_words(s, 14)
    except ImportError:
        from django.utils.text import Truncator
        to_string_function = lambda s: Truncator(s).words(14)

    class Media:
        css = {
            'all': ('css/jquery.autocomplete.css',)
        }
        js = (
            # The js/jquery.js script is referenced in admin/base_site.html template.
            # Requesting it here again would reset all the plugins loaded afterwards.
            'js/jquery.bgiframe.js',
            'js/jquery.ajaxQueue.js',
            'js/jquery.autocomplete.js'
        )

    def label_for_value(self, value):
        key = self.rel.get_related_field().name
        obj = self.rel.to._default_manager.get(**{key: value})
        return self.to_string_function(obj)

    def __init__(self, rel, search_fields, to_string_function, attrs=None):
        self.rel = rel
        self.search_fields = search_fields
        if to_string_function: self.to_string_function = to_string_function
        super(ForeignKeySearchInput, self).__init__(attrs)

    def render(self, name, value, attrs=None):
        if attrs is None:
            attrs = {}
        rendered = super(ForeignKeySearchInput, self).render(name, value, attrs)
        if value:
            label = self.label_for_value(value)
        else:
            label = u''
        return rendered + mark_safe(u'''
            <style type="text/css" media="screen">
                #lookup_%(name)s {
                    padding-right:16px;
                    background: url(
                        %(static_url)sadmin/img/admin/selector-search.gif
                    ) no-repeat right;<commit_msg>satchmo_utils.admin.py was missing jquery admin media<commit_after>#
#    Autocomplete feature for admin panel
#
#    Most of the code has been written by <NAME>:
#    http://jannisleidel.com/2008/11/autocomplete-form-widget-foreignkey-model-fields/
#
#    to_string_function, Satchmo adaptation and some comments added by emes (<NAME>)
#
from django import forms
from django.db import models
from django.db.models.query import QuerySet
from django.conf import settings
from django.contrib import admin
from django.http import HttpResponse, HttpResponseNotFound
from django.utils.encoding import smart_str
from django.utils.safestring import mark_safe
try:
    from functools import update_wrapper
except ImportError:
    from django.utils.functional import update_wrapper
# Commenting out for Django 1.3 compatibility
# TODO: Need to find long term solution
#from django.contrib.admin.templatetags.admin_static import static

import operator

class ForeignKeySearchInput(forms.HiddenInput):
    """
    A Widget for displaying ForeignKeys in an autocomplete search input
    instead in a <select> box.
    """

    try:
        from django.utils.text import truncate_words
        to_string_function = lambda s: truncate_words(s, 14)
    except ImportError:
        from django.utils.text import Truncator
        to_string_function = lambda s: Truncator(s).words(14)

    class Media:
        css = {
            'all': ('css/jquery.autocomplete.css',)
        }
        js = (
            # The js/jquery.js script is referenced in admin/base_site.html template.
            # Requesting it here again would reset all the plugins loaded afterwards.
            'js/jquery-1.4.2.js',
            'js/jquery.bgiframe.js',
            'js/jquery.ajaxQueue.js',
            'js/jquery.autocomplete.js'
        )

    def label_for_value(self, value):
        key = self.rel.get_related_field().name
        obj = self.rel.to._default_manager.get(**{key: value})
        return self.to_string_function(obj)

    def __init__(self, rel, search_fields, to_string_function, attrs=None):
        self.rel = rel
        self.search_fields = search_fields
        if to_string_function: self.to_string_function = to_string_function
        super(ForeignKeySearchInput, self).__init__(attrs)

    def render(self, name, value, attrs=None):
        if attrs is None:
            attrs = {}
        rendered = super(ForeignKeySearchInput, self).render(name, value, attrs)
        if value:
            label = self.label_for_value(value)
        else:
            label = u''
        return rendered + mark_safe(u'''
            <style type="text/css" media="screen">
                #lookup_%(name)s {
                    padding-right:16px;
                    background: url(
                        %(static_url)sadmin/img/admin/selector-search.gif
                    ) no-repeat right;
{'fraction_non_alphanumeric': 0.08649789029535865, 'fraction_numerical': 0.005450070323488045, 'mean_word_length': 2.623566878980892, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '2119579', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1571, 'n_words': 435}