Project

General

Profile

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

calebasse / calebasse / dossiers / lookups.py @ 0ae9ecd6

1
# -*- coding: utf-8 -*-
2

    
3
from calebasse.lookups import CalebasseLookup
4
from calebasse.dossiers.models import PatientRecord, PatientAddress
5
from itertools import chain
6

    
7
class PatientRecordLookup(CalebasseLookup):
8
    model = PatientRecord
9
    search_field = 'last_name'
10
    homonym = False
11

    
12
    def get_query(self,q,request):
13
        kwargs = { "%s__istartswith" % self.search_field : q }
14
        not_closed_filter_field = 'last_state__status__name'
15

    
16
        qs = self.model.objects.filter(**kwargs).order_by(self.search_field)
17

    
18
        if request.COOKIES.has_key('home-service'):
19
            service = request.COOKIES['home-service'].upper().replace('-', ' ')
20
            qs = qs.filter(service__name=service)
21
        #nb = qs.count()
22
        #nb_distinct = qs.distinct('display_name').count()
23
        #if nb != nb_distinct:
24
        #    self.homonym = True
25
        qs.prefetch_related('last_state__status')
26
        qs.query.order_by = [not_closed_filter_field, ]
27

    
28
        # filtering all closed records to put them at the end
29
        separation_criteria = {'last_state__status__name': 'Clos'}
30
        closed = qs.filter(**separation_criteria)
31
        qs = qs.exclude(**separation_criteria)
32

    
33
        qs = qs.order_by('-%s' % not_closed_filter_field)
34

    
35
        return chain(qs, closed)
36

    
37
    def get_result(self, obj):
38
        return self.format_item_display(obj)
39

    
40
    def format_match(self, obj):
41
        return self.format_item_display(obj)
42

    
43
    def format_item_display(self,obj):
44
        text = obj.last_name.upper() + ' ' + obj.first_name
45
        if obj.paper_id or obj.last_state:
46
            text += u' ('
47
        if obj.paper_id:
48
            text += obj.paper_id
49
            if obj.last_state:
50
                text += u' - '
51
        if obj.last_state:
52
            text += obj.last_state.status.name
53
        if obj.paper_id or obj.last_state:
54
            text += u')'
55
        return unicode(text)
56

    
57
class PatientAddressLookup(CalebasseLookup):
58
    model = PatientAddress
59
    search_field = 'display_name'
60

    
61
    def get_query(self, q, request):
62
        qs = super(PatientAddressLookup, self).get_query(q, request)
63
        if request.session.has_key('patientrecord_id'):
64
            qs = qs.filter(patientcontact__id=request.session['patientrecord_id'])
65
        return qs
66

    
(4-4/12)