Skip to content
Snippets Groups Projects

Resolve "Unfinished appointments"

Merged Piotr Gawron requested to merge 174-unfinished-appointments into master
17 files
+ 649
171
Compare changes
  • Side-by-side
  • Inline
Files
17
@@ -6,31 +6,55 @@ from django.http import JsonResponse
from django.urls import reverse
from django.utils import timezone
from web.models import Appointment
from web.views import e500_error
from web.views.appointment import APPOINTMENT_LIST_GENERIC, APPOINTMENT_LIST_UNFINISHED, APPOINTMENT_LIST_APPROACHING
from web.views.notifications import get_filter_locations, \
get_today_midnight_date, \
get_unfinished_appointments
from web.api_views.serialization_utils import serialize_datetime, location_to_str, flying_team_to_str, add_column, \
bool_to_yes_no, get_filters_for_data_table_request
from web.models import Appointment, Study, SubjectColumns, AppointmentColumns, AppointmentList
from web.models.appointment_list import APPOINTMENT_LIST_GENERIC, APPOINTMENT_LIST_UNFINISHED, \
APPOINTMENT_LIST_APPROACHING
from web.models.constants import GLOBAL_STUDY_ID
from web.views.notifications import get_filter_locations, get_today_midnight_date, get_unfinished_appointments
logger = logging.getLogger(__name__)
@login_required
def get_appointments(request, type, min_date, max_date):
if type == APPOINTMENT_LIST_GENERIC:
result = Appointment.objects.filter(location__in=get_filter_locations(request.user),
)
elif type == APPOINTMENT_LIST_UNFINISHED:
def get_appointment_columns(request, appointment_list_type):
study = Study.objects.filter(id=GLOBAL_STUDY_ID)[0]
appointment_lists = AppointmentList.objects.filter(study=study, type=appointment_list_type)
if len(appointment_lists) > 0:
appointment_list = appointment_lists[0]
subject_columns = appointment_list.visible_subject_columns
appointment_columns = appointment_list.visible_appointment_columns
else:
subject_columns = SubjectColumns()
appointment_columns = AppointmentColumns()
result = []
add_column(result, "First name", "first_name", subject_columns, "string_filter")
add_column(result, "Last name", "last_name", subject_columns, "string_filter")
add_column(result, "Info sent", "post_mail_sent", appointment_columns, "yes_no_filter")
add_column(result, "Date", "datetime_when", appointment_columns, None)
add_column(result, "Appointment types", "appointment_types", appointment_columns, "appointment_type_filter",
sortable=False)
add_column(result, "Edit", "edit", None, None, sortable=False)
return JsonResponse({"columns": result})
@login_required
def get_appointments(request, appointment_type, min_date, max_date):
if appointment_type == APPOINTMENT_LIST_GENERIC:
result = Appointment.objects.filter(location__in=get_filter_locations(request.user))
elif appointment_type == APPOINTMENT_LIST_UNFINISHED:
result = get_unfinished_appointments(request.user)
elif type == APPOINTMENT_LIST_APPROACHING:
elif appointment_type == APPOINTMENT_LIST_APPROACHING:
result = Appointment.objects.filter(
datetime_when__gt=get_today_midnight_date(),
location__in=get_filter_locations(request.user),
status=Appointment.APPOINTMENT_STATUS_SCHEDULED
).order_by("datetime_when")
else:
raise TypeError("Unknown query type: " + type)
raise TypeError("Unknown query type: " + appointment_type)
if min_date is not None:
min_date = datetime.strptime(min_date, "%Y-%m-%d").replace(tzinfo=timezone.now().tzinfo)
@@ -42,74 +66,126 @@ def get_appointments(request, type, min_date, max_date):
return result.order_by("datetime_when")
def get_appointments_order(appointments_to_be_ordered, order_column, order_direction):
result = appointments_to_be_ordered
if order_direction == "asc":
order_direction = ""
else:
order_direction = "-"
if order_column == "first_name":
result = appointments_to_be_ordered.order_by(order_direction + 'visit__subject__subject__first_name')
elif order_column == "last_name":
result = appointments_to_be_ordered.order_by(order_direction + 'visit__subject__subject__last_name')
elif order_column == "location":
result = appointments_to_be_ordered.order_by(order_direction + 'location')
elif order_column == "flying_team":
result = appointments_to_be_ordered.order_by(order_direction + 'flying_team')
elif order_column == "post_mail_sent":
result = appointments_to_be_ordered.order_by(order_direction + 'post_mail_sent')
elif order_column == "datetime_when":
result = appointments_to_be_ordered.order_by(order_direction + 'datetime_when')
else:
logger.warn("Unknown sort column: " + str(order_column))
return result
def get_appointments_filtered(appointments_to_be_filtered, filters):
result = appointments_to_be_filtered
for row in filters:
column = row[0]
value = row[1]
if column == "first_name":
result = result.filter(visit__subject__subject__first_name__icontains=value)
elif column == "last_name":
result = result.filter(visit__subject__subject__last_name__icontains=value)
elif column == "location":
result = result.filter(location=value)
elif column == "flying_team":
result = result.filter(flying_team=value)
elif column == "appointment_types":
result = result.filter(appointment_types=value)
else:
message = "UNKNOWN filter: "
if column is None:
message += "[None]"
else:
message += str(column)
logger.warn(message)
return result
@login_required
def appointments(request, type):
try:
# id of the query from dataTable: https://datatables.net/manual/server-side
draw = int(request.GET.get("draw", "-1"))
def appointments(request, appointment_type):
# 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"))
start = int(request.GET.get("start", "0"))
length = int(request.GET.get("length", "10"))
min_date = request.GET.get("start_date", None)
max_date = request.GET.get("end_date", None)
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")
if min_date is not None:
length = 1000000000
min_date = request.GET.get("start_date", None)
max_date = request.GET.get("end_date", None)
all_appointments = get_appointments(request, type, min_date, max_date)
filters = get_filters_for_data_table_request(request)
count = all_appointments.count()
if min_date is not None:
length = 1000000000
all_appointments = get_appointments(request, appointment_type, min_date, max_date)
count = all_appointments.count()
sliced_subjects = all_appointments[start:(start + length)]
sorted_appointments = get_appointments_order(all_appointments, order_column, order_dir)
filtered_appointments = get_appointments_filtered(sorted_appointments, filters)
sliced_appointments = filtered_appointments[start:(start + length)]
result_appointments = sliced_subjects
result_appointments = sliced_appointments
count_filtered = all_appointments.count()
count_filtered = all_appointments.count()
data = []
for appointment in result_appointments:
data.append(serialize_appointment(appointment))
data = []
for appointment in result_appointments:
data.append(serialize_appointment(appointment))
return JsonResponse({
"draw": draw,
"recordsTotal": count,
"recordsFiltered": count_filtered,
"data": data,
})
except:
logger.exception("Problem with getting appointments")
return e500_error(request)
return JsonResponse({
"draw": draw,
"recordsTotal": count,
"recordsFiltered": count_filtered,
"data": data,
})
def serialize_appointment(appointment):
subject_string = ""
nd_number = screening_number = phone_numbers = appointment_types = None
first_name = ""
last_name = ""
nd_number = screening_number = phone_numbers = appointment_type_names = None
if appointment.visit is not None:
title = "Visit " + str(appointment.visit.visit_number)
study_subject = appointment.visit.subject
subject_string = study_subject.subject.last_name + " " + study_subject.subject.first_name
first_name = study_subject.subject.first_name
last_name = study_subject.subject.last_name
nd_number = study_subject.nd_number
screening_number = study_subject.screening_number
phone_numbers = ", ".join(filter(None,
[study_subject.subject.phone_number, study_subject.subject.phone_number_2,
study_subject.subject.phone_number_3]))
appointment_types = ", ".join([unicode(type) for type in appointment.appointment_types.all()])
appointment_type_names = ", ".join(
[unicode(appointment_type_codes) for appointment_type_codes in appointment.appointment_types.all()])
else:
title = appointment.comment
type = ", ".join([type.code for type in appointment.appointment_types.all()])
time = ""
if appointment.datetime_when is not None:
time = appointment.datetime_when.strftime('%Y-%m-%d %H:%M')
until = ""
if appointment.datetime_when is not None:
until = appointment.datetime_until().strftime('%Y-%m-%d %H:%M')
appointment_type_codes = ", ".join(
[appointment_type_codes.code for appointment_type_codes in appointment.appointment_types.all()])
until = serialize_datetime(appointment.datetime_until())
if appointment.flying_team is None:
location = unicode(appointment.location)
else:
location = unicode(appointment.location) + " (" + unicode(appointment.flying_team) + ")"
location = location_to_str(appointment.location)
flying_team = flying_team_to_str(appointment.flying_team)
result = {
"subject": subject_string,
@@ -117,14 +193,19 @@ def serialize_appointment(appointment):
"nd_number": nd_number,
"screening_number": screening_number,
"phone_number": phone_numbers,
"appointment_types": appointment_types,
"type": type,
"datetime_when": time,
"appointment_type_names": appointment_type_names,
"datetime_until": until,
"comment": appointment.comment,
"color": appointment.color(),
"id": appointment.id,
"first_name": first_name,
"last_name": last_name,
"location": location,
"flying_team": flying_team,
"post_mail_sent": bool_to_yes_no(appointment.post_mail_sent),
"datetime_when": serialize_datetime(appointment.datetime_when),
"appointment_types": appointment_type_codes,
"url": reverse('web.views.appointment_edit', kwargs={'id': str(appointment.id)})
}
return result
Loading