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

Merge branch '143-calendar-view' into 'master'

list of appointments contains information about availabilities

Closes #143

See merge request NCER-PD/scheduling-system!94
parents b74e186e 99a5539e
No related branches found
Tags v1.0.0-beta.3
1 merge request!94list of appointments contains information about availabilities
Pipeline #
...@@ -43,6 +43,7 @@ urlpatterns = [ ...@@ -43,6 +43,7 @@ urlpatterns = [
url(r'^specializations$', worker.specializations, name='web.api.specializations'), url(r'^specializations$', worker.specializations, name='web.api.specializations'),
url(r'^units$', worker.units, name='web.api.units'), url(r'^units$', worker.units, name='web.api.units'),
url(r'^workers$', worker.workers_for_daily_planning, name='web.api.workers'), url(r'^workers$', worker.workers_for_daily_planning, name='web.api.workers'),
url(r'^workers/availabilities$', worker.availabilities, name='web.api.workers.availabilities$'),
# daily planning events # daily planning events
url(r'^events/(?P<date>\d{4}-\d{2}-\d{2})/$', daily_planning.events, name='web.api.events'), url(r'^events/(?P<date>\d{4}-\d{2}-\d{2})/$', daily_planning.events, name='web.api.events'),
......
import datetime
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.utils import timezone
from web.api_views.daily_planning import get_workers_for_daily_planning from web.api_views.daily_planning import get_workers_for_daily_planning, get_availabilities
from ..models import Worker from ..models import Worker
...@@ -33,3 +36,31 @@ def workers_for_daily_planning(request): ...@@ -33,3 +36,31 @@ def workers_for_daily_planning(request):
} }
workers_list_for_json.append(worker_dict_for_json) workers_list_for_json.append(worker_dict_for_json)
return JsonResponse(workers_list_for_json, safe=False) return JsonResponse(workers_list_for_json, safe=False)
@login_required
def availabilities(request):
result = []
min_date = request.GET.get("start_date")
max_date = request.GET.get("end_date")
workers = get_workers_for_daily_planning(request)
min_date = datetime.datetime.strptime(min_date, "%Y-%m-%d").replace(tzinfo=timezone.now().tzinfo)
max_date = datetime.datetime.strptime(max_date, "%Y-%m-%d").replace(tzinfo=timezone.now().tzinfo)
while min_date <= max_date:
str_date = min_date.strftime('%Y-%m-%d')
date_result = {"date": str_date, "workers": []}
for worker in workers:
if len(get_availabilities(worker, str_date)) > 0:
date_result["workers"].append({
"worker_id": worker.id,
"initials": worker.initials(),
})
result.append(date_result)
min_date += datetime.timedelta(days=1)
return JsonResponse({
"availabilities": result,
})
...@@ -111,3 +111,11 @@ class Worker(models.Model): ...@@ -111,3 +111,11 @@ class Worker(models.Model):
def __unicode__(self): def __unicode__(self):
return "%s %s" % (self.first_name, self.last_name) return "%s %s" % (self.first_name, self.last_name)
def initials(self):
result = ""
if len(self.first_name) > 0:
result += self.first_name[0]
if len(self.last_name) > 0:
result += self.last_name[0]
return result
...@@ -202,11 +202,35 @@ function appointment_date_change_behaviour(datetime_picker, worker_select, minut ...@@ -202,11 +202,35 @@ function appointment_date_change_behaviour(datetime_picker, worker_select, minut
} }
function get_calendar_events_function(source, allow_url_redirection) { function get_calendar_events_function(source, allow_url_redirection, day_headers, workerAvailabilitiesUrl) {
if (allow_url_redirection === undefined) { if (allow_url_redirection === undefined) {
allow_url_redirection = false; allow_url_redirection = false;
} }
return function (start, end, timezone, callback) { return function (start, end, timezone, callback) {
if (day_headers !== undefined) {
console.log("XXXX");
$.ajax({
data: {
start_date: start.format(),
end_date: end.format()
},
url: workerAvailabilitiesUrl,
success: function (doc) {
for (var i = 0; i < doc.availabilities.length; i++) {
const entry = doc.availabilities[i];
var initials = '';
for (var j = 0; j < entry.workers.length; j++) {
initials += entry.workers[j].initials + ", ";
}
var element = day_headers[entry.date];
if (element !== undefined) {
element.innerHTML = initials;
}
}
}
});
}
$.ajax({ $.ajax({
data: { data: {
// our hypothetical feed requires UNIX timestamps // our hypothetical feed requires UNIX timestamps
...@@ -220,7 +244,7 @@ function get_calendar_events_function(source, allow_url_redirection) { ...@@ -220,7 +244,7 @@ function get_calendar_events_function(source, allow_url_redirection) {
const entry = doc.data[i]; const entry = doc.data[i];
var title = entry.subject; var title = entry.subject;
if (title != "") { if (title !== "") {
title += " (" + entry.nd_number + "); type: " + entry.type; title += " (" + entry.nd_number + "); type: " + entry.type;
} else { } else {
title = entry.title title = entry.title
......
...@@ -75,6 +75,7 @@ ...@@ -75,6 +75,7 @@
<script> <script>
$(function () { $(function () {
var dayHeaders = {};
var approachingTable = $('#approaching_table'); var approachingTable = $('#approaching_table');
var table = approachingTable.DataTable({ var table = approachingTable.DataTable({
serverSide: true, serverSide: true,
...@@ -115,6 +116,12 @@ ...@@ -115,6 +116,12 @@
weekNumbers: true, weekNumbers: true,
startParam: "start_date", startParam: "start_date",
endParam: "end_date", endParam: "end_date",
dayRender: function (date, cell) {
var element = document.createElement("div");
cell[0].appendChild(element);
dayHeaders[$(cell[0]).attr("data-date")] = element;
},
eventRender: function (event, element) { eventRender: function (event, element) {
var content = "<ul>"; var content = "<ul>";
if (event.phone_number) { if (event.phone_number) {
...@@ -143,7 +150,10 @@ ...@@ -143,7 +150,10 @@
}); });
console.log(event); console.log(event);
}, },
events: get_calendar_events_function("{% url 'web.api.appointments' full_list %}", true), events: get_calendar_events_function(
"{% url 'web.api.appointments' full_list %}",
true, dayHeaders,
"{% url 'web.api.workers.availabilities$' %}")
}); });
}); });
</script> </script>
......
# coding=utf-8 # coding=utf-8
import json
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.test import Client from django.test import Client
......
# coding=utf-8 # coding=utf-8
import json import json
from django.test import RequestFactory
from django.urls import reverse from django.urls import reverse
from web.models import Availability
from web.models.constants import TUESDAY_AS_DAY_OF_WEEK
from web.api_views.worker import availabilities
from web.tests import LoggedInWithWorkerTestCase from web.tests import LoggedInWithWorkerTestCase
...@@ -47,3 +51,29 @@ class TestApi(LoggedInWithWorkerTestCase): ...@@ -47,3 +51,29 @@ class TestApi(LoggedInWithWorkerTestCase):
response = self.client.get(reverse('web.api.workers')) response = self.client.get(reverse('web.api.workers'))
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
self.assertTrue(self.worker.first_name in response.content) self.assertTrue(self.worker.first_name in response.content)
def test_empty_availabilities(self):
factory = RequestFactory()
request = factory.get('/api/workers?start_date=2017-10-20&end_date=2017-11-20')
request.user = self.user
result = availabilities(request)
entries = json.loads(result.content)['availabilities']
for entry in entries:
self.assertEqual(0, len(entry["workers"]))
def test_non_empty_availabilities(self):
factory = RequestFactory()
request = factory.get('/api/workers?start_date=2017-10-20&end_date=2017-11-20')
request.user = self.user
availability = Availability.objects.create(person=self.worker, day_number=TUESDAY_AS_DAY_OF_WEEK,
available_from="8:00", available_till="16:00")
availability.save()
result = availabilities(request)
entries = json.loads(result.content)['availabilities']
count = 0;
for entry in entries:
count += len(entry["workers"])
self.assertTrue(count > 0)
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