<s> from collections import namedtuple <EOL> from django . contrib . auth . models import User <EOL> from django . utils . safestring import mark_safe <EOL> from selectable . base import LookupBase <EOL> from selectable . base import ModelLookup <EOL> from selectable . registry import registry <EOL> from timepiece . crm . models import Project , Business <EOL> class ProjectLookup ( ModelLookup ) : <EOL> model = Project <EOL> search_fields = ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def get_item_label ( self , project ) : <EOL> return mark_safe ( u'<STR_LIT>' % self . get_item_value ( project ) ) <EOL> def get_item_value ( self , project ) : <EOL> return project . name if project else '<STR_LIT>' <EOL> class BusinessLookup ( ModelLookup ) : <EOL> model = Business <EOL> search_fields = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def get_item_label ( self , business ) : <EOL> return mark_safe ( u'<STR_LIT>' % self . get_item_value ( business ) ) <EOL> def get_item_value ( self , business ) : <EOL> return business . name if business else '<STR_LIT>' <EOL> class UserLookup ( ModelLookup ) : <EOL> model = User <EOL> search_fields = ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) <EOL> def get_query ( self , request , q ) : <EOL> return super ( UserLookup , self ) . get_query ( request , q ) . order_by ( '<STR_LIT>' ) <EOL> def get_item_label ( self , user ) : <EOL> return mark_safe ( u'<STR_LIT>' % self . get_item_value ( user ) ) <EOL> def get_item_value ( self , user ) : <EOL> return user . get_name_or_username ( ) if user else '<STR_LIT>' <EOL> class QuickLookup ( LookupBase ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . lookups = { <EOL> '<STR_LIT:user>' : UserLookup ( ) , <EOL> '<STR_LIT>' : ProjectLookup ( ) , <EOL> '<STR_LIT>' : BusinessLookup ( ) , <EOL> } <EOL> super ( QuickLookup , self ) . __init__ ( * args , ** kwargs ) <EOL> def get_query ( self , request , q ) : <EOL> results = [ ] <EOL> SearchResult = namedtuple ( '<STR_LIT>' , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:label>' , '<STR_LIT:value>' ] ) <EOL> for result_type , lookup in self . lookups . items ( ) : <EOL> for item in lookup . get_query ( request , q ) [ : <NUM_LIT:10> ] : <EOL> label = lookup . get_item_label ( item ) <EOL> value = lookup . get_item_value ( item ) <EOL> results . append ( SearchResult ( result_type , item , label , value ) ) <EOL> results . sort ( key = lambda a : a . value ) <EOL> return results <EOL> def get_item_label ( self , item ) : <EOL> return item . label <EOL> def get_item_id ( self , item ) : <EOL> return '<STR_LIT>' . format ( item . result_type , item . item . pk ) <EOL> def get_item ( self , value ) : <EOL> try : <EOL> result_type , pk = value . split ( '<STR_LIT:->' , <NUM_LIT:1> ) <EOL> return self . lookups [ result_type ] . get_item ( pk ) <EOL> except ( ValueError , KeyError ) : <EOL> return None <EOL> def get_item_value ( self , item ) : <EOL> return item . value if item else '<STR_LIT>' <EOL> registry . register ( BusinessLookup ) <EOL> registry . register ( ProjectLookup ) <EOL> registry . register ( UserLookup ) <EOL> registry . register ( QuickLookup ) </s>
