# Path: timepiece/crm/models.py
# class Project(models.Model):
#     name = models.CharField(max_length=255)
#     tracker_url = models.CharField(
#         max_length=255, blank=True, null=False, default="")
#     business = models.ForeignKey(
#         Business, related_name='new_business_projects')
#     point_person = models.ForeignKey(User, limit_choices_to={'is_staff': True})
#     users = models.ManyToManyField(
#         User, related_name='user_projects', through='ProjectRelationship')
#     activity_group = models.ForeignKey(
#         'entries.ActivityGroup', related_name='activity_group', null=True,
#         blank=True, verbose_name='restrict activities to')
#     type = models.ForeignKey(
#         Attribute, limit_choices_to={'type': 'project-type'},
#         related_name='projects_with_type')
#     status = models.ForeignKey(
#         Attribute, limit_choices_to={'type': 'project-status'},
#         related_name='projects_with_status')
#     description = models.TextField()
# 
#     objects = models.Manager()
#     trackable = TrackableProjectManager()
# 
#     class Meta:
#         db_table = 'timepiece_project'  # Using legacy table name.
#         ordering = ('name', 'status', 'type',)
#         permissions = (
#             ('view_project', 'Can view project'),
#             ('email_project_report', 'Can email project report'),
#             ('view_project_time_sheet', 'Can view project time sheet'),
#             ('export_project_time_sheet', 'Can export project time sheet'),
#             ('generate_project_invoice', 'Can generate project invoice'),
#         )
# 
#     def __str__(self):
#         return '{0} ({1})'.format(self.name, self.business.get_display_name())
# 
#     @property
#     def billable(self):
#         return self.type.billable
# 
#     def get_absolute_url(self):
#         return reverse('view_project', args=(self.pk,))
# 
#     def get_active_contracts(self):
#         """Returns all associated contracts which are not marked complete."""
#         ProjectContract = apps.get_model('contracts', 'ProjectContract')
#         return self.contracts.exclude(status=ProjectContract.STATUS_COMPLETE)
# 
# class Business(models.Model):
#     name = models.CharField(max_length=255)
#     short_name = models.CharField(max_length=255, blank=True)
#     email = models.EmailField(blank=True)
#     description = models.TextField(blank=True)
#     notes = models.TextField(blank=True)
#     external_id = models.CharField(max_length=32, blank=True)
# 
#     class Meta:
#         db_table = 'timepiece_business'  # Using legacy table name.
#         ordering = ('name',)
#         verbose_name_plural = 'Businesses'
#         permissions = (
#             ('view_business', 'Can view businesses'),
#         )
# 
#     def __str__(self):
#         return self.get_display_name()
# 
#     def get_absolute_url(self):
#         return reverse('view_business', args=(self.pk,))
# 
#     def get_display_name(self):
#         return self.short_name or self.name


from collections import namedtuple
from django.contrib.auth.models import User
from django.utils.safestring import mark_safe
from selectable.base import LookupBase
from selectable.base import ModelLookup
from selectable.registry import registry
from timepiece.crm.models import Project, Business
    def get_item_label(self, business):
        return mark_safe(u'<span class="business">%s</span>' % self.get_item_value(business))

    def get_item_value(self, business):
        return business.name if business else ''


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

    def get_query(self, request, q):
        return super(UserLookup, self).get_query(request, q).order_by('last_name')

    def get_item_label(self, user):
        return mark_safe(u'<span class="user">%s</span>' % self.get_item_value(user))

    def get_item_value(self, user):
        return user.get_name_or_username() if user else ''


class QuickLookup(LookupBase):

    def __init__(self, *args, **kwargs):
        self.lookups = {
            'user': UserLookup(),
            'project': ProjectLookup(),
            'business': BusinessLookup(),
        }

# Path: timepiece/crm/models.py
# class Project(models.Model):
#     name = models.CharField(max_length=255)
#     tracker_url = models.CharField(
#         max_length=255, blank=True, null=False, default="")
#     business = models.ForeignKey(
#         Business, related_name='new_business_projects')
#     point_person = models.ForeignKey(User, limit_choices_to={'is_staff': True})
#     users = models.ManyToManyField(
#         User, related_name='user_projects', through='ProjectRelationship')
#     activity_group = models.ForeignKey(
#         'entries.ActivityGroup', related_name='activity_group', null=True,
#         blank=True, verbose_name='restrict activities to')
#     type = models.ForeignKey(
#         Attribute, limit_choices_to={'type': 'project-type'},
#         related_name='projects_with_type')
#     status = models.ForeignKey(
#         Attribute, limit_choices_to={'type': 'project-status'},
#         related_name='projects_with_status')
#     description = models.TextField()
# 
#     objects = models.Manager()
#     trackable = TrackableProjectManager()
# 
#     class Meta:
#         db_table = 'timepiece_project'  # Using legacy table name.
#         ordering = ('name', 'status', 'type',)
#         permissions = (
#             ('view_project', 'Can view project'),
#             ('email_project_report', 'Can email project report'),
#             ('view_project_time_sheet', 'Can view project time sheet'),
#             ('export_project_time_sheet', 'Can export project time sheet'),
#             ('generate_project_invoice', 'Can generate project invoice'),
#         )
# 
#     def __str__(self):
#         return '{0} ({1})'.format(self.name, self.business.get_display_name())
# 
#     @property
#     def billable(self):
#         return self.type.billable
# 
#     def get_absolute_url(self):
#         return reverse('view_project', args=(self.pk,))
# 
#     def get_active_contracts(self):
#         """Returns all associated contracts which are not marked complete."""
#         ProjectContract = apps.get_model('contracts', 'ProjectContract')
#         return self.contracts.exclude(status=ProjectContract.STATUS_COMPLETE)
# 
# class Business(models.Model):
#     name = models.CharField(max_length=255)
#     short_name = models.CharField(max_length=255, blank=True)
#     email = models.EmailField(blank=True)
#     description = models.TextField(blank=True)
#     notes = models.TextField(blank=True)
#     external_id = models.CharField(max_length=32, blank=True)
# 
#     class Meta:
#         db_table = 'timepiece_business'  # Using legacy table name.
#         ordering = ('name',)
#         verbose_name_plural = 'Businesses'
#         permissions = (
#             ('view_business', 'Can view businesses'),
#         )
# 
#     def __str__(self):
#         return self.get_display_name()
# 
#     def get_absolute_url(self):
#         return reverse('view_business', args=(self.pk,))
# 
#     def get_display_name(self):
#         return self.short_name or self.name


# Path: timepiece/crm/lookups.py
from collections import namedtuple
from django.contrib.auth.models import User
from django.utils.safestring import mark_safe
from selectable.base import LookupBase
from selectable.base import ModelLookup
from selectable.registry import registry
from timepiece.crm.models import Project, Business

    def get_item_label(self, business):
        return mark_safe(u'<span class="business">%s</span>' % self.get_item_value(business))

    def get_item_value(self, business):
        return business.name if business else ''


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

    def get_query(self, request, q):
        return super(UserLookup, self).get_query(request, q).order_by('last_name')

    def get_item_label(self, user):
        return mark_safe(u'<span class="user">%s</span>' % self.get_item_value(user))

    def get_item_value(self, user):
        return user.get_name_or_username() if user else ''


class QuickLookup(LookupBase):

    def __init__(self, *args, **kwargs):
        self.lookups = {
            'user': UserLookup(),
            'project': ProjectLookup(),
            'business': BusinessLookup(),
        }

        super(QuickLookup, self).__init__(*args, **kwargs)