From 5ee40d02ffec9409c871adf2423bf30b30875992 Mon Sep 17 00:00:00 2001 From: Tuomas Suutari Date: Thu, 7 Dec 2017 15:46:59 +0200 Subject: [PATCH 1/2] enforcement/valid_parking: Tune the delay of invalid visibility The valid_parking endpoint returns the last parking even if it is no longer valid, when there is no valid parkings, but the latest parking is not too old. The limit used to be 1 day, but that was too much. Change that limit to 15 minutes as requested. --- parkings/api/enforcement/valid_parking.py | 4 ++-- parkings/tests/api/enforcement/test_valid_parking.py | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/parkings/api/enforcement/valid_parking.py b/parkings/api/enforcement/valid_parking.py index a67257fe..22cd0a4e 100644 --- a/parkings/api/enforcement/valid_parking.py +++ b/parkings/api/enforcement/valid_parking.py @@ -76,8 +76,8 @@ def filter_time(self, queryset, name, value): valid_parkings = queryset.valid_at(time) if valid_parkings: return valid_parkings - day_ago = time - datetime.timedelta(days=1) - valid_within_limit = queryset.starts_before(time).ends_after(day_ago) + limit = time - datetime.timedelta(minutes=15) + valid_within_limit = queryset.starts_before(time).ends_after(limit) return valid_within_limit.order_by('-time_end')[:1] diff --git a/parkings/tests/api/enforcement/test_valid_parking.py b/parkings/tests/api/enforcement/test_valid_parking.py index 7a761313..b3d2ce55 100644 --- a/parkings/tests/api/enforcement/test_valid_parking.py +++ b/parkings/tests/api/enforcement/test_valid_parking.py @@ -132,6 +132,8 @@ def test_registration_number_filter(operator, staff_api_client, parking_factory) 'after_start_of_2nd', 'before_end_of_2nd', 'at_end_of_2nd', + 'less_than_15min_after_2nd', + 'more_than_15min_after_2nd', 'less_than_day_after_2nd', 'more_than_day_after_2nd', 'now', @@ -158,7 +160,9 @@ def test_time_filtering(operator, staff_api_client, parking_factory, name): 'after_start_of_2nd': ('2014-01-01T12:00:01Z', [p2]), 'before_end_of_2nd': ('2016-01-01T11:59:59Z', [p2]), 'at_end_of_2nd': ('2016-01-01T12:00:00Z', [p2]), - 'less_than_day_after_2nd': ('2016-01-01T22:00:00Z', [p2]), + 'less_than_15min_after_2nd': ('2016-01-01T12:14:59Z', [p2]), + 'more_than_15min_after_2nd': ('2016-01-02T12:15:01Z', []), + 'less_than_day_after_2nd': ('2016-01-01T22:00:00Z', []), 'more_than_day_after_2nd': ('2016-01-02T13:00:00Z', []), 'now': ('', [p3]), }[name] From f8103c73868996d5f9f7fbc965d7500d01f657b4 Mon Sep 17 00:00:00 2001 From: Tuomas Suutari Date: Mon, 11 Dec 2017 13:19:29 +0200 Subject: [PATCH 2/2] Make the time old parking are visible configurable --- parkings/api/enforcement/valid_parking.py | 9 ++++++++- parkkihubi/settings.py | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/parkings/api/enforcement/valid_parking.py b/parkings/api/enforcement/valid_parking.py index 22cd0a4e..2d33e64f 100644 --- a/parkings/api/enforcement/valid_parking.py +++ b/parkings/api/enforcement/valid_parking.py @@ -1,6 +1,7 @@ import datetime import django_filters +from django.conf import settings from django.utils import timezone from django.utils.translation import gettext_lazy as _ from rest_framework import permissions, serializers, viewsets @@ -76,11 +77,17 @@ def filter_time(self, queryset, name, value): valid_parkings = queryset.valid_at(time) if valid_parkings: return valid_parkings - limit = time - datetime.timedelta(minutes=15) + limit = time - get_time_old_parkings_visible() valid_within_limit = queryset.starts_before(time).ends_after(limit) return valid_within_limit.order_by('-time_end')[:1] +def get_time_old_parkings_visible(default=datetime.timedelta(minutes=15)): + value = getattr(settings, 'PARKKIHUBI_TIME_OLD_PARKINGS_VISIBLE', None) + assert value is None or isinstance(value, datetime.timedelta) + return value if value is not None else default + + class ValidParkingViewSet(viewsets.ReadOnlyModelViewSet): queryset = Parking.objects.order_by('-time_end') serializer_class = ValidParkingSerializer diff --git a/parkkihubi/settings.py b/parkkihubi/settings.py index a29ad4ed..23957e1a 100644 --- a/parkkihubi/settings.py +++ b/parkkihubi/settings.py @@ -167,6 +167,7 @@ # Parkkihubi # ############## PARKKIHUBI_TIME_PARKINGS_EDITABLE = timedelta(minutes=2) +PARKKIHUBI_TIME_OLD_PARKINGS_VISIBLE = timedelta(minutes=15) PARKKIHUBI_PUBLIC_API_ENABLED = env.bool('PARKKIHUBI_PUBLIC_API_ENABLED', True) PARKKIHUBI_OPERATOR_API_ENABLED = env.bool('PARKKIHUBI_OPERATOR_API_ENABLED', True) PARKKIHUBI_INTERNAL_API_ENABLED = env.bool('PARKKIHUBI_INTERNAL_API_ENABLED', False)