1
|
# -*- coding: utf-8 -*-
|
2
|
|
3
|
from calebasse.cbv import HOME_SERVICE_COOKIE
|
4
|
from calebasse.lookups import CalebasseLookup
|
5
|
from calebasse.dossiers.models import PatientRecord, PatientAddress
|
6
|
from itertools import chain
|
7
|
|
8
|
class PatientRecordLookup(CalebasseLookup):
|
9
|
model = PatientRecord
|
10
|
search_field = 'last_name'
|
11
|
homonym = False
|
12
|
|
13
|
def get_query(self,q,request):
|
14
|
kwargs = { "%s__istartswith" % self.search_field : q }
|
15
|
not_closed_filter_field = 'last_state__status__name'
|
16
|
|
17
|
qs = self.model.objects.filter(**kwargs).order_by(self.search_field)
|
18
|
|
19
|
if request.COOKIES.has_key(HOME_SERVICE_COOKIE):
|
20
|
service = request.COOKIES[HOME_SERVICE_COOKIE]
|
21
|
qs = qs.filter(service__slug=service)
|
22
|
#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
|
qs.query.order_by = [not_closed_filter_field, ]
|
28
|
|
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
|
qs = qs.order_by('-%s' % not_closed_filter_field)
|
35
|
|
36
|
return chain(qs, closed)
|
37
|
|
38
|
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
|
if obj.paper_id or obj.last_state:
|
47
|
text += u' ('
|
48
|
if obj.paper_id:
|
49
|
text += obj.paper_id
|
50
|
if obj.last_state:
|
51
|
text += u' - '
|
52
|
if obj.last_state:
|
53
|
text += obj.last_state.status.name
|
54
|
if obj.paper_id or obj.last_state:
|
55
|
text += u')'
|
56
|
return unicode(text)
|
57
|
|
58
|
class PatientAddressLookup(CalebasseLookup):
|
59
|
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
|
67
|
|