<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.contrib.auth.models import Message
from django.http import HttpResponse, HttpResponseNotFound
from django.utils.encoding import smart_str
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.utils.text import truncate_words

import operator

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

    to_string_function = lambda s: truncate_words(s, 14)

    class Media:
        css = {
            'all': ('css/jquery.autocomplete.css',)
        }
        js = (
            'js/jquery.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(
                        %(admin_media_prefix)simg/admin/selector-search.gif
                    ) no-repeat right;<commit_msg>Do not reload jquery.js in widgets. It will drop all the plugins loaded in templates.<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.contrib.auth.models import Message
from django.http import HttpResponse, HttpResponseNotFound
from django.utils.encoding import smart_str
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.utils.text import truncate_words

import operator

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

    to_string_function = lambda s: truncate_words(s, 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(
                        %(admin_media_prefix)simg/admin/selector-search.gif
                    ) no-repeat right;
{'fraction_non_alphanumeric': 0.09098394400988061, 'fraction_numerical': 0.004116920543433511, 'mean_word_length': 2.572794117647059, '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': '911835', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1342, 'n_words': 363}