Skip to content
Snippets Groups Projects
Commit 5c8c5992 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

list of appointments (ith calendar) uses dynamic loading of data

parent f5413b19
No related branches found
No related tags found
1 merge request!35performance on appointment list
import traceback import traceback
import pytz
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.http import JsonResponse from django.http import JsonResponse
from django.urls import reverse
from web.models import Appointment from web.models import Appointment
from web.views import e500_error from web.views import e500_error
...@@ -13,13 +13,14 @@ from web.views.notifications import get_filter_locations, \ ...@@ -13,13 +13,14 @@ from web.views.notifications import get_filter_locations, \
@login_required @login_required
def get_appointments(request, type): def get_appointments(request, type, min_date, max_date):
if type == APPOINTMENT_LIST_GENERIC: if type == APPOINTMENT_LIST_GENERIC:
return Appointment.objects.filter(location__in=get_filter_locations(request.user)).order_by("datetime_when") result = Appointment.objects.filter(location__in=get_filter_locations(request.user),
)
elif type == APPOINTMENT_LIST_UNFINISHED: elif type == APPOINTMENT_LIST_UNFINISHED:
return get_unfinished_appointments(request.user).order_by("datetime_when") result = get_unfinished_appointments(request.user)
elif type == APPOINTMENT_LIST_APPROACHING: elif type == APPOINTMENT_LIST_APPROACHING:
return Appointment.objects.filter( result = Appointment.objects.filter(
datetime_when__gt=get_today_midnight_date(), datetime_when__gt=get_today_midnight_date(),
location__in=get_filter_locations(request.user), location__in=get_filter_locations(request.user),
status=Appointment.APPOINTMENT_STATUS_SCHEDULED status=Appointment.APPOINTMENT_STATUS_SCHEDULED
...@@ -27,6 +28,13 @@ def get_appointments(request, type): ...@@ -27,6 +28,13 @@ def get_appointments(request, type):
else: else:
raise TypeError("Unknown query type: " + type) raise TypeError("Unknown query type: " + type)
if min_date is not None:
result = result.filter(datetime_when__gt=min_date)
if max_date is not None:
result = result.filter(datetime_when__lt=max_date)
return result.order_by("datetime_when")
@login_required @login_required
def appointments(request, type): def appointments(request, type):
...@@ -36,7 +44,12 @@ def appointments(request, type): ...@@ -36,7 +44,12 @@ def appointments(request, type):
start = int(request.GET.get("start", "0")) start = int(request.GET.get("start", "0"))
length = int(request.GET.get("length", "10")) length = int(request.GET.get("length", "10"))
order = int(request.GET.get("order[0][column]", "0"))
min_date = request.GET.get("start_date", None)
max_date = request.GET.get("end_date", None)
if min_date is not None:
length = 1000000000
filters = [] filters = []
column_id = 0 column_id = 0
...@@ -46,7 +59,7 @@ def appointments(request, type): ...@@ -46,7 +59,7 @@ def appointments(request, type):
filters.append([request.GET.get("columns[" + str(column_id) + "][data]"), val]) filters.append([request.GET.get("columns[" + str(column_id) + "][data]"), val])
column_id += 1 column_id += 1
all_appointments = get_appointments(request, type) all_appointments = get_appointments(request, type, min_date, max_date)
count = all_appointments.count() count = all_appointments.count()
...@@ -54,7 +67,7 @@ def appointments(request, type): ...@@ -54,7 +67,7 @@ def appointments(request, type):
appointments = sliced_subjects appointments = sliced_subjects
count_filtered = all_appointments.count() count_filtered = sliced_subjects.count()
data = [] data = []
for appointment in appointments: for appointment in appointments:
...@@ -72,8 +85,6 @@ def appointments(request, type): ...@@ -72,8 +85,6 @@ def appointments(request, type):
def serialize_appointment(appointment): def serialize_appointment(appointment):
local_tz = pytz.timezone('UTC')
subject = "" subject = ""
if appointment.visit is not None: if appointment.visit is not None:
title = appointment.visit.follow_up_title() title = appointment.visit.follow_up_title()
...@@ -88,13 +99,19 @@ def serialize_appointment(appointment): ...@@ -88,13 +99,19 @@ def serialize_appointment(appointment):
if appointment.datetime_when is not None: if appointment.datetime_when is not None:
time = appointment.datetime_when.strftime('%Y-%m-%d %H:%M') time = appointment.datetime_when.strftime('%Y-%m-%d %H:%M')
# time = appointment.datetime_when.strftime('%Y-%m-%d %H:%M:%S.%f %Z%z') # time = appointment.datetime_when.strftime('%Y-%m-%d %H:%M:%S.%f %Z%z')
until = ""
if appointment.datetime_when is not None:
until = appointment.datetime_until().strftime('%Y-%m-%d %H:%M')
result = { result = {
"subject": subject, "subject": subject,
"title": title, "title": title,
"type": type, "type": type,
"datetime_when": time, "datetime_when": time,
"datetime_until": until,
"comment": appointment.comment, "comment": appointment.comment,
"color": appointment.color(),
"id": appointment.id, "id": appointment.id,
"url": reverse('web.views.appointment_edit', kwargs={'id': str(appointment.id)})
} }
return result return result
...@@ -24,53 +24,20 @@ ...@@ -24,53 +24,20 @@
{% block maincontent %} {% block maincontent %}
<div class="row"> <div class="row">
<div class="col-md-5"> <div class="col-md-5">
{% if approaching_list %} <table id="approaching_table" class="table table-bordered table-striped">
<table id="approaching_table" class="table table-bordered table-striped"> <thead>
<thead> <tr>
<tr> <th>Subject</th>
<th>Subject</th> <th>Visit</th>
<th>Visit</th> <th>Type</th>
<th>Type</th> <th>Date</th>
<th>Date</th> <th>Details</th>
<th>Details</th> </tr>
</tr> </thead>
</thead> <tbody>
<tbody> </tbody>
</table>
{% for approach in approaching_list %}
<tr>
{% if approach.visit == None %}
<td>
</td>
<td>
</td>
{% else %}
<td>
{{ approach.visit.subject.first_name }} {{ approach.visit.subject.last_name }}
({{ approach.visit.subject.nd_number }})
</td>
<td>
{{ approach.visit.follow_up_title }}
</td>
{% endif %}
<td>
{% for type in approach.appointment_types.all %}
{{ type.code }},
{% endfor %}
</td>
<td>{{ approach.datetime_when | date:"Y-m-d H:i" }}</td>
<td>
<a href="{% url 'web.views.appointment_edit' approach.id %}" type="button"
class="btn btn-block btn-default">Details</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<p>No visits approaching in close future.</p>
{% endif %}
</div> </div>
<div class="col-md-7"> <div class="col-md-7">
...@@ -101,16 +68,32 @@ ...@@ -101,16 +68,32 @@
<script> <script>
$(function () { $(function () {
$('#planning_table, #approaching_table').DataTable({ var table = $('#approaching_table').DataTable({
"paging": true, serverSide: true,
"lengthChange": false, processing: true,
"searching": true, ordering: false,
"ordering": true, ajax: "{% url 'web.api.appointments' approaching_list %}",
"order": [[3, "asc"]], columns: [
"info": true, {"data": "subject"},
"autoWidth": false {"data": "title"},
{"data": "type"},
{"data": "datetime_when"},
{"data": null},
],
columnDefs: [{
"targets": 4,
"data": "id",
"defaultContent": '<a href="#" type="button" class="btn btn-block btn-default">Edit</a>'
}]
})
$('#approaching_table tbody').on('click', 'a', function () {
var data = table.row($(this).parents('tr')).data();
var url = "{% url 'web.views.appointment_edit' 12345 %}".replace(/12345/, data.id.toString());
window.location.href = url;
}); });
$('#approaching_table_filter').css("display", "none");
$('#calendar').fullCalendar({ $('#calendar').fullCalendar({
header: { header: {
left: 'prev,next today', left: 'prev,next today',
...@@ -119,19 +102,38 @@ ...@@ -119,19 +102,38 @@
}, },
editable: false, editable: false,
weekNumbers: true, weekNumbers: true,
events: [ startParam: "start_date",
{% for appointment in full_list %} endParam: "end_date",
{ events: function (start, end, timezone, callback) {
title: '{{ appointment.title }}', $.ajax({
start: '{{ appointment.datetime_when | date:"c" }}', data: {
end: '{{ appointment.datetime_until | date:"c" }}', // our hypothetical feed requires UNIX timestamps
color: '{{ appointment.color }}', start_date: start.format(),
subject_id: '{{ appointment.visit.subject.id }}', end_date: end.format()
id: '{{ appointment.id }}',
url: '{% url 'web.views.appointment_edit' appointment.id %}',
}, },
{% endfor %} url: "{% url 'web.api.appointments' full_list %}",
], success: function (doc) {
var events = [];
for (var i = 0; i < doc.data.length; i++) {
var title = doc.data[i].subject;
if (title != "") {
title += "; type: " + doc.data[i].type;
} else {
title = doc.data[i].title
}
events.push({
title: title,
start: doc.data[i].datetime_when,
end: doc.data[i].datetime_until,
id: doc.data[i].id,
color: doc.data[i].color,
url: doc.data[i].url,
})
}
callback(events);
}
});
}
}); });
}); });
</script> </script>
......
...@@ -13,17 +13,9 @@ APPOINTMENT_LIST_APPROACHING = "APPROACHING" ...@@ -13,17 +13,9 @@ APPOINTMENT_LIST_APPROACHING = "APPROACHING"
def appointments(request): def appointments(request):
approaching_list = 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')
full_list = get_calendar_full_appointments(request.user)
context = { context = {
'approaching_list': approaching_list, 'approaching_list': APPOINTMENT_LIST_APPROACHING,
'full_list': full_list 'full_list': APPOINTMENT_LIST_GENERIC
} }
return wrap_response(request, "appointments/index.html", context) return wrap_response(request, "appointments/index.html", context)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment