<s> from django . conf import settings <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from completion . backends . base import BaseBackend <EOL> from completion . models import AutocompleteObject <EOL> from completion . utils import clean_phrase , create_key , partial_complete <EOL> class DatabaseAutocomplete ( BaseBackend ) : <EOL> def flush ( self ) : <EOL> AutocompleteObject . objects . all ( ) . delete ( ) <EOL> def store_object ( self , obj , data ) : <EOL> """<STR_LIT>""" <EOL> self . remove_object ( obj , data ) <EOL> title = data [ '<STR_LIT:title>' ] <EOL> for partial_title in partial_complete ( title ) : <EOL> key = create_key ( partial_title ) <EOL> autocomplete_obj = AutocompleteObject ( <EOL> title = key , <EOL> object_id = obj . pk , <EOL> content_type = ContentType . objects . get_for_model ( obj ) , <EOL> pub_date = data [ '<STR_LIT>' ] , <EOL> data = data [ '<STR_LIT:data>' ] <EOL> ) <EOL> autocomplete_obj . save ( ) <EOL> autocomplete_obj . sites = data [ '<STR_LIT>' ] <EOL> def remove_object ( self , obj , data ) : <EOL> AutocompleteObject . objects . for_object ( obj ) . delete ( ) <EOL> def suggest ( self , phrase , limit , models ) : <EOL> phrase = create_key ( phrase ) <EOL> if not phrase : <EOL> return [ ] <EOL> query = dict ( <EOL> title__startswith = phrase , <EOL> sites__pk__exact = settings . SITE_ID , <EOL> ) <EOL> if models is not None : <EOL> query . update ( content_type__in = [ <EOL> ContentType . objects . get_for_model ( model_class ) for model_class in models <EOL> ] ) <EOL> qs = AutocompleteObject . objects . filter ( <EOL> ** query <EOL> ) . values_list ( '<STR_LIT:data>' , flat = True ) . distinct ( ) <EOL> if limit is not None : <EOL> qs = qs [ : limit ] <EOL> return qs </s>
