# 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)
# 
# Path: timepiece/entries/models.py
# class Activity(models.Model):
#     """
#     Represents different types of activity: debugging, developing,
#     brainstorming, QA, etc...
#     """
#     code = models.CharField(
#         max_length=5, unique=True,
#         help_text='Enter a short code to describe the type of activity that took place.')
#     name = models.CharField(
#         max_length=50,
#         help_text='Now enter a more meaningful name for the activity.')
#     billable = models.BooleanField(default=True)
# 
#     def __str__(self):
#         return self.name
# 
#     class Meta:
#         db_table = 'timepiece_activity'  # Using legacy table name
#         ordering = ('name',)
#         verbose_name_plural = 'activities'


from selectable.base import ModelLookup
from selectable.registry import registry
from timepiece.crm.models import Project
from timepiece.entries.models import Activity



class ActivityLookup(ModelLookup):
    model = Activity
    search_fields = ('name__icontains', )

    def get_query(self, request, term):
        results = super(ActivityLookup, self).get_query(request, term)
        project_pk = request.GET.get('project', None)
        if project_pk not in [None, '']:
            project = Project.objects.get(pk=project_pk)
            if project and project.activity_group:
                return project.activity_group.activities.all().filter(name__icontains=term)

# 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)
# 
# Path: timepiece/entries/models.py
# class Activity(models.Model):
#     """
#     Represents different types of activity: debugging, developing,
#     brainstorming, QA, etc...
#     """
#     code = models.CharField(
#         max_length=5, unique=True,
#         help_text='Enter a short code to describe the type of activity that took place.')
#     name = models.CharField(
#         max_length=50,
#         help_text='Now enter a more meaningful name for the activity.')
#     billable = models.BooleanField(default=True)
# 
#     def __str__(self):
#         return self.name
# 
#     class Meta:
#         db_table = 'timepiece_activity'  # Using legacy table name
#         ordering = ('name',)
#         verbose_name_plural = 'activities'


# Path: timepiece/entries/lookups.py
from selectable.base import ModelLookup
from selectable.registry import registry
from timepiece.crm.models import Project
from timepiece.entries.models import Activity




class ActivityLookup(ModelLookup):
    model = Activity
    search_fields = ('name__icontains', )

    def get_query(self, request, term):
        results = super(ActivityLookup, self).get_query(request, term)
        project_pk = request.GET.get('project', None)
        if project_pk not in [None, '']:
            project = Project.objects.get(pk=project_pk)
            if project and project.activity_group:
                return project.activity_group.activities.all().filter(name__icontains=term)

        return results