Project

General

Profile

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

calebasse / calebasse / dossiers / lookups.py @ 1164bc89

1 61861b9b Jérôme Schneider
# -*- coding: utf-8 -*-
2 4376fbf4 Jérôme Schneider
3 fb700370 Jérôme Schneider
from calebasse.cbv import HOME_SERVICE_COOKIE
4 9f39f12f Jérôme Schneider
from calebasse.lookups import CalebasseLookup
5 677c2c8d Jérôme Schneider
from calebasse.dossiers.models import PatientRecord, PatientAddress
6 6a664659 Serghei MIHAI
from itertools import chain
7 4376fbf4 Jérôme Schneider
8 9f39f12f Jérôme Schneider
class PatientRecordLookup(CalebasseLookup):
9 4376fbf4 Jérôme Schneider
    model = PatientRecord
10 cd8a83dc Jérôme Schneider
    search_field = 'last_name'
11 61861b9b Jérôme Schneider
    homonym = False
12 4376fbf4 Jérôme Schneider
13
    def get_query(self,q,request):
14 cd8a83dc Jérôme Schneider
        kwargs = { "%s__istartswith" % self.search_field : q }
15 fb771100 Serghei MIHAI
        not_closed_filter_field = 'last_state__status__name'
16
17 cd8a83dc Jérôme Schneider
        qs = self.model.objects.filter(**kwargs).order_by(self.search_field)
18 fb771100 Serghei MIHAI
19 fb700370 Jérôme Schneider
        if request.COOKIES.has_key(HOME_SERVICE_COOKIE):
20
            service = request.COOKIES[HOME_SERVICE_COOKIE]
21
            qs = qs.filter(service__slug=service)
22 61861b9b Jérôme Schneider
        #nb = qs.count()
23
        #nb_distinct = qs.distinct('display_name').count()
24
        #if nb != nb_distinct:
25
        #    self.homonym = True
26
        qs.prefetch_related('last_state__status')
27 fb771100 Serghei MIHAI
        qs.query.order_by = [not_closed_filter_field, ]
28 6a664659 Serghei MIHAI
29
        # filtering all closed records to put them at the end
30
        separation_criteria = {'last_state__status__name': 'Clos'}
31
        closed = qs.filter(**separation_criteria)
32
        qs = qs.exclude(**separation_criteria)
33
34 fb771100 Serghei MIHAI
        qs = qs.order_by('-%s' % not_closed_filter_field)
35
36 6a664659 Serghei MIHAI
        return chain(qs, closed)
37 4376fbf4 Jérôme Schneider
38 4e4c4c50 Jérôme Schneider
    def get_result(self, obj):
39
        return self.format_item_display(obj)
40
41
    def format_match(self, obj):
42
        return self.format_item_display(obj)
43
44
    def format_item_display(self,obj):
45
        text = obj.last_name.upper() + ' ' + obj.first_name
46 a6638759 Jérôme Schneider
        if obj.paper_id or obj.last_state:
47
            text += u' ('
48 cffbd8a2 Jérôme Schneider
        if obj.paper_id:
49 a6638759 Jérôme Schneider
            text += obj.paper_id
50
            if obj.last_state:
51
                text += u' - '
52 61861b9b Jérôme Schneider
        if obj.last_state:
53 a6638759 Jérôme Schneider
            text += obj.last_state.status.name
54
        if obj.paper_id or obj.last_state:
55
            text += u')'
56 4e4c4c50 Jérôme Schneider
        return unicode(text)
57 cffbd8a2 Jérôme Schneider
58 9f39f12f Jérôme Schneider
class PatientAddressLookup(CalebasseLookup):
59 677c2c8d Jérôme Schneider
    model = PatientAddress
60
    search_field = 'display_name'
61
62
    def get_query(self, q, request):
63
        qs = super(PatientAddressLookup, self).get_query(q, request)
64
        if request.session.has_key('patientrecord_id'):
65
            qs = qs.filter(patientcontact__id=request.session['patientrecord_id'])
66
        return qs