Project

General

Profile

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

calebasse / calebasse / ressources / lookups.py @ dd986559

1
import itertools
2

    
3
from calebasse.lookups import CalebasseLookup
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(CalebasseLookup):
20
    model = Worker
21
    search_field = 'last_name'
22
    enabled = True
23

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

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

    
39
        kwargs = { "%s__istartswith" % self.search_field : q }
40
        if self.enabled:
41
            return self.model.objects.filter(enabled=True).filter(**kwargs).order_by('last_name')
42
        return self.model.objects.filter(**kwargs).order_by('last_name')
43

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

    
47
    def format_match(self, obj):
48
        return self.format_item_display(obj)
49

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

    
57
class AllWorkerOrGroupLookup(WorkerOrGroupLookup):
58
    enabled = False
(3-3/7)