From d78c43e309d2620e272fec2dba8e25003213c9d7 Mon Sep 17 00:00:00 2001 From: r-xyz <100710244+r-xyz@users.noreply.github.com> Date: Sun, 8 Sep 2024 19:20:52 +0200 Subject: [PATCH] Add reservation end date in kiosk. Fixes #253. --- .../templates/kiosk/tool_reservation.html | 63 ++++++++++++------- NEMO/apps/kiosk/views.py | 11 ++-- 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/NEMO/apps/kiosk/templates/kiosk/tool_reservation.html b/NEMO/apps/kiosk/templates/kiosk/tool_reservation.html index 1bc8d3f2..52629631 100644 --- a/NEMO/apps/kiosk/templates/kiosk/tool_reservation.html +++ b/NEMO/apps/kiosk/templates/kiosk/tool_reservation.html @@ -24,11 +24,11 @@

Create a new reservation

When would you like to reserve the {{ tool }}?

+ placeholder="Choose a start date">
When would you like to reserve the {{ tool }}? class="form-control" placeholder="Choose a start time">
+
+ +
When would you like to reserve the {{ tool }}? {% for item in tool_reservation_times %} unavailable_times.push([{{ item.start|date:"U" }},{{ item.end|date:"U" }}]); {% endfor %} - let date_picker = $('#date').pickadate({format: "{{ pickadate_date_format }}", formatSubmit: "yyyy-mm-dd", firstDay: 1, hiddenName: true, onSet: refresh_times}); - let start_time_picker = $('#start').pickatime({interval: 15, format: "{{ pickadate_time_format }}", formatSubmit: "H:i", hiddenName: true, formatLabel: format_label}); - let end_time_picker = $('#end').pickatime({interval: 15, format: "{{ pickadate_time_format }}", formatSubmit: "H:i", hiddenName: true, formatLabel: format_label}); + let start_date_picker = $('#start_date').pickadate({format: "{{ pickadate_date_format }}", formatSubmit: "yyyy-mm-dd", firstDay: 1, hiddenName: true, onSet: function(event) { + var date = start_date_picker.pickadate('picker').get('select', '{{ pickadate_date_format }}'); + end_date_picker.pickadate('picker').set('select', date); + refresh_times; + } + }); + let end_date_picker = $('#end_date').pickadate({format: "{{ pickadate_date_format }}", formatSubmit: "yyyy-mm-dd", firstDay: 1, hiddenName: true, onSet: refresh_times}); + let start_time_picker = $('#start').pickatime({interval: 15, format: "{{ pickadate_time_format }}", formatSubmit: "H:i", hiddenName: true, formatLabel: format_labels(true)}); + let end_time_picker = $('#end').pickatime({interval: 15, format: "{{ pickadate_time_format }}", formatSubmit: "H:i", hiddenName: true, formatLabel: format_labels(false)}); // set initial date - if ('{{ date|default_if_none:'' }}') { - date_picker.pickadate('picker').set('select', '{{ date }}', {format: '{{ pickadate_date_format }}'}) + if ('{{ start_date|default_if_none:'' }}') { + start_date_picker.pickadate('picker').set('select', '{{ start_date }}', {format: '{{ pickadate_date_format }}'}) } function refresh_times() { start_time_picker.pickatime('picker').render(); end_time_picker.pickatime('picker').render(); } - function format_label(time) { - if (date_picker.pickadate('picker').get('select') && unavailable_times.length > 0) { - let date_selected = date_picker.pickadate('picker').get('select').pick; // selected date in milliseconds - let time_selected = time.pick * 60 * 1000; // time in milliseconds - let date_time_selected = (date_selected + time_selected)/1000; // back to seconds to compare with python timestamp - for (let i=0 ; i < unavailable_times.length; i++) { - let times = unavailable_times[i]; - let start = times[0]; - let end = times[1]; - if (date_time_selected >= start && date_time_selected < end) { - return '{{ pickadate_time_format }} !alre!ad!y re!serve!d'; + function format_labels(is_start){ + return function format_label(time) { + let date_picker = is_start ? start_date_picker : end_date_picker; + if (date_picker.pickadate('picker').get('select') && unavailable_times.length > 0) { + let date_selected = date_picker.pickadate('picker').get('select').pick; // selected date in milliseconds + let time_selected = time.pick * 60 * 1000; // time in milliseconds + let date_time_selected = (date_selected + time_selected)/1000; // back to seconds to compare with python timestamp + for (let i=0 ; i < unavailable_times.length; i++) { + let times = unavailable_times[i]; + let start = times[0]; + let end = times[1]; + if (date_time_selected >= start && date_time_selected < end) { + return '{{ pickadate_time_format }} !alre!ad!y re!serve!d'; + } } } - } - return '{{ pickadate_time_format }}'; - } + return '{{ pickadate_time_format }}'; + } + } revert(120) diff --git a/NEMO/apps/kiosk/views.py b/NEMO/apps/kiosk/views.py index 507b3458..1bc7d86a 100644 --- a/NEMO/apps/kiosk/views.py +++ b/NEMO/apps/kiosk/views.py @@ -255,11 +255,14 @@ def reserve_tool(request): """ Create a reservation for a user. """ try: - date = parse_date(request.POST["date"]) - start = localize(datetime.combine(date, parse_time(request.POST["start"]))) - end = localize(datetime.combine(date, parse_time(request.POST["end"]))) + start_date = parse_date(request.POST["start_date"]) + end_date = parse_date(request.POST["end_date"]) + start = localize(datetime.combine(start_date, parse_time(request.POST["start"]))) + end = localize(datetime.combine(end_date, parse_time(request.POST["end"]))) except: - dictionary["message"] = "Please enter a valid date, start time, and end time for the reservation." + dictionary["message"] = ( + "Please enter a valid start date, start time, end date and end time for the reservation." + ) return render(request, "kiosk/error.html", dictionary) # Create the new reservation: reservation = Reservation()