<s> import operator <EOL> from django . http import HttpResponse , HttpResponseNotFound <EOL> from django . db import models <EOL> from django . db . models . query import QuerySet <EOL> from django . utils . encoding import smart_str <EOL> from django . utils . translation import ugettext as _ <EOL> from django . utils . text import get_text_list <EOL> try : <EOL> from functools import update_wrapper <EOL> except ImportError : <EOL> from django . utils . functional import update_wrapper <EOL> from django_extensions . admin . widgets import ForeignKeySearchInput <EOL> from django . conf import settings <EOL> if '<STR_LIT>' in settings . INSTALLED_APPS : <EOL> from reversion . admin import VersionAdmin as ModelAdmin <EOL> else : <EOL> from django . contrib . admin import ModelAdmin <EOL> class ForeignKeyAutocompleteAdmin ( ModelAdmin ) : <EOL> """<STR_LIT>""" <EOL> related_search_fields = { } <EOL> related_string_functions = { } <EOL> def get_urls ( self ) : <EOL> from django . conf . urls . defaults import patterns , url <EOL> def wrap ( view ) : <EOL> def wrapper ( * args , ** kwargs ) : <EOL> return self . admin_site . admin_view ( view ) ( * args , ** kwargs ) <EOL> return update_wrapper ( wrapper , view ) <EOL> info = self . model . _meta . app_label , self . model . _meta . module_name <EOL> urlpatterns = patterns ( '<STR_LIT>' , <EOL> url ( r'<STR_LIT>' , <EOL> wrap ( self . foreignkey_autocomplete ) , <EOL> name = '<STR_LIT>' % info ) , <EOL> ) + super ( ForeignKeyAutocompleteAdmin , self ) . get_urls ( ) <EOL> return urlpatterns <EOL> def foreignkey_autocomplete ( self , request ) : <EOL> """<STR_LIT>""" <EOL> query = request . GET . get ( '<STR_LIT:q>' , None ) <EOL> app_label = request . GET . get ( '<STR_LIT>' , None ) <EOL> model_name = request . GET . get ( '<STR_LIT>' , None ) <EOL> search_fields = request . GET . get ( '<STR_LIT>' , None ) <EOL> object_pk = request . GET . get ( '<STR_LIT>' , None ) <EOL> try : <EOL> to_string_function = self . related_string_functions [ model_name ] <EOL> except KeyError : <EOL> to_string_function = lambda x : x . __unicode__ ( ) <EOL> if search_fields and app_label and model_name and ( query or object_pk ) : <EOL> def construct_search ( field_name ) : <EOL> if field_name . startswith ( '<STR_LIT>' ) : <EOL> return "<STR_LIT>" % field_name [ <NUM_LIT:1> : ] <EOL> elif field_name . startswith ( '<STR_LIT:=>' ) : <EOL> return "<STR_LIT>" % field_name [ <NUM_LIT:1> : ] <EOL> elif field_name . startswith ( '<STR_LIT:@>' ) : <EOL> return "<STR_LIT>" % field_name [ <NUM_LIT:1> : ] <EOL> else : <EOL> return "<STR_LIT>" % field_name <EOL> model = models . get_model ( app_label , model_name ) <EOL> queryset = model . _default_manager . all ( ) <EOL> data = '<STR_LIT>' <EOL> if query : <EOL> for bit in query . split ( ) : <EOL> or_queries = [ models . Q ( ** { construct_search ( <EOL> smart_str ( field_name ) ) : smart_str ( bit ) } ) <EOL> for field_name in search_fields . split ( '<STR_LIT:U+002C>' ) ] <EOL> other_qs = QuerySet ( model ) <EOL> other_qs . dup_select_related ( queryset ) <EOL> other_qs = other_qs . filter ( reduce ( operator . or_ , or_queries ) ) <EOL> queryset = queryset & other_qs <EOL> data = '<STR_LIT>' . join ( [ u'<STR_LIT>' % ( <EOL> to_string_function ( f ) , f . pk ) for f in queryset ] ) <EOL> elif object_pk : <EOL> try : <EOL> obj = queryset . get ( pk = object_pk ) <EOL> except : <EOL> pass <EOL> else : <EOL> data = to_string_function ( obj ) <EOL> return HttpResponse ( data ) <EOL> return HttpResponseNotFound ( ) <EOL> def get_help_text ( self , field_name , model_name ) : <EOL> searchable_fields = self . related_search_fields . get ( field_name , None ) <EOL> if searchable_fields : <EOL> help_kwargs = { <EOL> '<STR_LIT>' : model_name , <EOL> '<STR_LIT>' : get_text_list ( searchable_fields , _ ( '<STR_LIT>' ) ) , <EOL> } <EOL> return _ ( '<STR_LIT>' ) % help_kwargs <EOL> return '<STR_LIT>' <EOL> def formfield_for_dbfield ( self , db_field , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if ( isinstance ( db_field , models . ForeignKey ) and <EOL> db_field . name in self . related_search_fields ) : <EOL> model_name = db_field . rel . to . _meta . object_name <EOL> help_text = self . get_help_text ( db_field . name , model_name ) <EOL> if kwargs . get ( '<STR_LIT>' ) : <EOL> help_text = u'<STR_LIT>' % ( kwargs [ '<STR_LIT>' ] , help_text ) <EOL> kwargs [ '<STR_LIT>' ] = ForeignKeySearchInput ( db_field . rel , <EOL> self . related_search_fields [ db_field . name ] ) <EOL> kwargs [ '<STR_LIT>' ] = help_text <EOL> return super ( ForeignKeyAutocompleteAdmin , <EOL> self ) . formfield_for_dbfield ( db_field , ** kwargs ) </s>
