Project

General

Profile

Download (1.51 KB) Statistics
| Branch: | Tag: | Revision:

calebasse / calebasse / ressources / lookups.py @ 3db8d984

1
import itertools
2

    
3
from ajax_select import LookupChannel
4
from calebasse.personnes.models import Worker
5
from calebasse.ressources.models import Service
6

    
7
class FakeGroup:
8
    pk = None
9
    label = None
10

    
11
    def __init__(self, pk, label):
12
        self.pk = pk
13
        self.label = label
14

    
15
    def __unicode__(self):
16
        return u'Groupe: %s' % self.label
17

    
18

    
19
class WorkerOrGroupLookup(LookupChannel):
20
    model = Worker
21
    search_field = 'display_name'
22

    
23
    def get_query(self, q, request):
24
        service = None
25
        if q.startswith('group:'):
26
            service = Service.objects.get(pk=int(q[6:]))
27
        else:
28
            try:
29
                service = Service.objects.get(name__iexact=q)
30
            except Service.DoesNotExist:
31
                pass
32

    
33
        if service:
34
            kwargs = { "%s__icontains" % self.search_field : q }
35
            group = FakeGroup('group:%s' % service.id, service.name)
36
            return itertools.chain([group], self.model.objects.for_service(service.id).order_by('last_name'))
37

    
38
        kwargs = { "%s__icontains" % self.search_field : q }
39
        return self.model.objects.filter(enabled=True).filter(**kwargs).order_by('last_name')
40

    
41
    def get_result(self, obj):
42
        return self.format_item_display(obj)
43

    
44
    def format_match(self, obj):
45
        return self.format_item_display(obj)
46

    
47
    def format_item_display(self,obj):
48
        if isinstance(obj, FakeGroup):
49
            text = obj.label
50
        else:
51
            text = obj.last_name.upper() + ' ' + obj.first_name
52
        return unicode(text)
53

    
(3-3/7)