Newer
Older
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
from web.models.constants import SUBJECT_TYPE_CHOICES
from web.views.notifications import get_subjects_with_no_visit, get_subjects_with_reminder, get_today_midnight_date
from web.views.subject import SUBJECT_LIST_GENERIC, SUBJECT_LIST_NO_VISIT, SUBJECT_LIST_REQUIRE_CONTACT
result_subjects = Subject.objects.filter(city__isnull=False).values_list('city').distinct()
result_subjects = Subject.objects.filter(country__isnull=False).values_list('country').distinct()
result_subjects = Subject.objects.filter(referral__isnull=False).values_list('referral').distinct()
@login_required
def get_subjects(request, type):
if type == SUBJECT_LIST_GENERIC:
return Subject.objects.all()
elif type == SUBJECT_LIST_NO_VISIT:
return get_subjects_with_no_visit(request.user)
elif type == SUBJECT_LIST_REQUIRE_CONTACT:
return get_subjects_with_reminder(request.user)
else:
raise TypeError("Unknown query type: " + type)
def get_subjects_order(subjects_to_be_ordered, order_column, order_direction):
result = subjects_to_be_ordered
if order_direction == "asc":
order_direction = ""
else:
order_direction = "-"
if order_column == "first_name":
result = subjects_to_be_ordered.order_by(order_direction + 'first_name')
elif order_column == "last_name":
result = subjects_to_be_ordered.order_by(order_direction + 'last_name')
elif order_column == "nd_number":
result = subjects_to_be_ordered.order_by(order_direction + 'nd_number')
elif order_column == "screening_number":
result = subjects_to_be_ordered.order_by(order_direction + 'screening_number')
elif order_column == "default_location":
result = subjects_to_be_ordered.order_by(order_direction + 'default_location')
elif order_column == "dead":
result = subjects_to_be_ordered.order_by(order_direction + 'dead')
elif order_column == "resigned":
result = subjects_to_be_ordered.order_by(order_direction + 'resigned')
elif order_column == "information_sent":
result = subjects_to_be_ordered.order_by(order_direction + 'information_sent')
elif order_column == "postponed":
result = subjects_to_be_ordered.order_by(order_direction + 'postponed')
elif order_column == "type":
result = subjects_to_be_ordered.order_by(order_direction + 'type')
elif order_column == "visit_0":
result = subjects_to_be_ordered.annotate(visit_0=Min('visit__datetime_begin')).order_by(
order_direction + 'visit_0')
elif order_column == "visit_1":
result = subjects_to_be_ordered.annotate(visit_0=Min('visit__datetime_begin'))
result = result.filter(visit__datetime_begin__gt=Min('visit__datetime_begin'))
result = result.annotate(visit_1=Min('visit__datetime_begin')).order_by(order_direction + 'visit_1')
logger.info(result.query)
return result
def get_subjects_filtered(subjects_to_be_filtered, filters):
result = subjects_to_be_filtered
for row in filters:
column = row[0]
value = row[1]
if column == "first_name":
result = result.filter(first_name__icontains=value)
elif column == "last_name":
result = result.filter(last_name__icontains=value)
elif column == "nd_number":
result = result.filter(nd_number__icontains=value)
elif column == "screening_number":
result = result.filter(screening_number__icontains=value)
result = result.filter(resigned=(value == "true"))
result = result.filter(postponed=(value == "true"))
elif column == "information_sent":
result = result.filter(information_sent=(value == "true"))
elif column == "default_location":
result = result.filter(default_location=value)
elif column == "type":
result = result.filter(type=value)
elif column == "":
pass
message = "UNKNOWN filter: "
if column is None:
message += "[None]"
else:
message += str(column)
logger.warn(message)
return result
@login_required
def subjects(request, type):
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
try:
# id of the query from dataTable: https://datatables.net/manual/server-side
draw = int(request.GET.get("draw", "-1"))
start = int(request.GET.get("start", "0"))
length = int(request.GET.get("length", "10"))
order = int(request.GET.get("order[0][column]", "0"))
order_dir = request.GET.get("order[0][dir]", "asc")
order_column = request.GET.get("columns[" + str(order) + "][data]", "last_name")
filters = []
column_id = 0
while request.GET.get("columns[" + str(column_id) + "][search][value]", "unknown") != "unknown":
val = request.GET.get("columns[" + str(column_id) + "][search][value]", "unknown")
if val != "":
filters.append([request.GET.get("columns[" + str(column_id) + "][data]"), val])
column_id += 1
all_subjects = get_subjects(request, type)
count = all_subjects.count()
ordered_subjects = get_subjects_order(all_subjects, order_column, order_dir)
filtered_subjects = get_subjects_filtered(ordered_subjects, filters)
sliced_subjects = filtered_subjects[start:(start + length)]
count_filtered = filtered_subjects.count()
data = []
data.append(serialize_subject(subject))
return JsonResponse({
"draw": draw,
"recordsTotal": count,
"recordsFiltered": count_filtered,
"data": data,
})
except:
return e500_error(request)
data = [{"id": subject_type_id, "name": subject_type_name} for subject_type_id, subject_type_name in
SUBJECT_TYPE_CHOICES.items()]
return JsonResponse({
"types": data
})
def get_yes_no(val):
if val:
return "YES"
else:
return "NO"
def serialize_subject_visit(visit):
status = "---"
appointments = visit.appointment_set.filter()
pass
def serialize_subject(subject):
location = ""
if subject.default_location is not None:
location = subject.default_location.name
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
visits = Visit.objects.filter(subject=subject)
serialized_visits = []
for visit in visits:
if visit.datetime_begin < get_today_midnight_date():
if visit.is_finished:
finished_appointments_count = visit.appointment_set.filter(
status=Appointment.APPOINTMENT_STATUS_FINISHED).count()
if finished_appointments_count > 0:
status = "DONE"
else:
status = "MISSED"
elif visit.datetime_end < get_today_midnight_date():
status = "EXCEEDED"
else:
scheduled_appointments_count = visit.appointment_set.filter(
status=Appointment.APPOINTMENT_STATUS_SCHEDULED).count()
if scheduled_appointments_count > 0:
status = "IN_PROGRESS"
else:
status = "SHOULD_BE_IN_PROGRESS"
else:
status = "UPCOMING"
serialized_visits.append({
"status": status,
"datetime_start": visit.datetime_begin.strftime('%Y-%m-%d'),
"datetime_end": visit.datetime_end.strftime('%Y-%m-%d'),
})
result = {
"first_name": subject.first_name,
"last_name": subject.last_name,
"nd_number": subject.nd_number,
"screening_number": subject.screening_number,
"default_location": location,
"dead": get_yes_no(subject.dead),
"resigned": get_yes_no(subject.resigned),
"postponed": get_yes_no(subject.postponed),
"information_sent": get_yes_no(subject.information_sent),
"type": subject.get_type_display(),
}
return result