Skip to content

Commit

Permalink
Merge pull request City-of-Helsinki#45 from suutari-ai/pasi-15min-limit
Browse files Browse the repository at this point in the history
enforcement/valid_parking: Tune the delay of invalid visibility
  • Loading branch information
suutari-ai authored Dec 11, 2017
2 parents ea7fb66 + f8103c7 commit 9cd615d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
11 changes: 9 additions & 2 deletions parkings/api/enforcement/valid_parking.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -76,11 +77,17 @@ 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 - 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
Expand Down
6 changes: 5 additions & 1 deletion parkings/tests/api/enforcement/test_valid_parking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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]
Expand Down
1 change: 1 addition & 0 deletions parkkihubi/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_ENFORCEMENT_API_ENABLED = (
Expand Down

0 comments on commit 9cd615d

Please sign in to comment.