Project

General

Profile

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

calebasse / calebasse / dossiers / lookups.py @ 6188a681

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 format_match(self,obj):
38
        return self.format_item_display(texte)
39

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

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

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

    
60
class PatientAddressLookup(CalebasseLookup):
61
    model = PatientAddress
62
    search_field = 'display_name'
63

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

    
(4-4/11)