from django.contrib.auth.models import User

from selectable.base import ModelLookup
from selectable.registry import registry


class UserLookup(ModelLookup):
    model = User
    search_fields = (
        'first_name__icontains',
        'last_name__icontains',
        'email__icontains',
    )

    def get_item_value(self, item):
        return item.email

    def get_item_label(self, item):
        return u"%s (%s)" % (item.get_full_name(), item.email)

    def create_item(self, value):
        """We aren't actually creating a new user, we just need to supply the
           email to the form processor
        """
        return value

registry.register(UserLookup)

@@ -1,12 +1,29 @@
+import operator%0A%0A
 from django.
@@ -53,16 +53,47 @@
 ort User
+%0Afrom django.db.models import Q
 %0A%0Afrom s
@@ -336,15 +336,592 @@
 s',%0A
-
     )%0A%0A
+    def get_query(self, request, term):%0A        qs = self.get_queryset()%0A        if term:%0A            search_filters = %5B%5D%0A            if len(term.split(' ')) == 1:%0A                if self.search_fields:%0A                    for field in self.search_fields:%0A                        search_filters.append(Q(**%7Bfield: term%7D))%0A                qs = qs.filter(reduce(operator.or_, search_filters))%0A            else:%0A                # Accounts for 'John Doe' term; will compare against get_full_name%0A                qs = %5Bx for x in qs if term in x.get_full_name()%5D%0A        return qs%0A%0A
     

Customize lookup get_query to account for looking up a portion of User.get_full_name