Skip to content

Commit

Permalink
Use vehicle changed date end time in permit ending
Browse files Browse the repository at this point in the history
Refs #PV-709
  • Loading branch information
mhieta committed Nov 21, 2023
1 parent 6519524 commit 8ec09ce
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
27 changes: 18 additions & 9 deletions parking_permits/models/parking_permit.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@

from ..constants import ParkingPermitEndType
from ..exceptions import ParkkihubiPermitError, PermitCanNotBeEnded
from ..utils import diff_months_ceil, flatten_dict, get_end_time, get_permit_prices
from ..utils import (
diff_months_ceil,
end_date_to_datetime,
flatten_dict,
get_end_time,
get_permit_prices,
)
from .mixins import TimestampedModelMixin, UserStampedModelMixin
from .parking_zone import ParkingZone
from .temporary_vehicle import TemporaryVehicle
Expand Down Expand Up @@ -511,14 +517,17 @@ def get_price_change_list(self, new_zone, is_low_emission):

def end_permit(self, end_type, force_end=False):
if end_type == ParkingPermitEndType.PREVIOUS_DAY_END:
previous_day = timezone.now() - timezone.timedelta(days=1)
end_time = previous_day.replace(
hour=23,
minute=59,
second=59,
microsecond=999999,
tzinfo=timezone.utc,
)
vehicle_changed_date = self.vehicle_changed_date
if vehicle_changed_date:
end_time = end_date_to_datetime(vehicle_changed_date)
else:
previous_day = timezone.localtime() - timezone.timedelta(days=1)
end_time = previous_day.replace(
hour=23,
minute=59,
second=59,
microsecond=999999,
)
elif end_type == ParkingPermitEndType.AFTER_CURRENT_PERIOD:
end_time = self.current_period_end_time
else:
Expand Down
38 changes: 19 additions & 19 deletions parking_permits/tests/test_cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,31 @@ class CronTestCase(TestCase):
def setUp(self):
self.customer = CustomerFactory(first_name="Stephen", last_name="Strange")

@freeze_time(datetime(2023, 11, 30, 0, 22))
@freeze_time(tz.make_aware(datetime(2023, 11, 30, 0, 22)))
def test_automatic_expiration_permits(self):
ParkingPermitFactory(
customer=self.customer,
end_time=datetime(2023, 12, 1, tzinfo=dt_tz.utc),
end_time=tz.make_aware(datetime(2023, 12, 1)),
status=ParkingPermitStatus.VALID,
)
ParkingPermitFactory(
customer=self.customer,
end_time=datetime(2023, 11, 29, tzinfo=dt_tz.utc),
end_time=tz.make_aware(datetime(2023, 11, 29)),
status=ParkingPermitStatus.DRAFT,
)
ParkingPermitFactory(
customer=self.customer,
end_time=datetime(2023, 12, 1, tzinfo=dt_tz.utc),
end_time=tz.make_aware(datetime(2023, 12, 1)),
status=ParkingPermitStatus.DRAFT,
)
ParkingPermitFactory(
customer=self.customer,
end_time=datetime(2023, 11, 29, tzinfo=dt_tz.utc),
end_time=tz.make_aware(datetime(2023, 11, 29)),
status=ParkingPermitStatus.VALID,
)
ParkingPermitFactory(
customer=self.customer,
end_time=datetime(2023, 11, 29, tzinfo=dt_tz.utc),
end_time=tz.make_aware(datetime(2023, 11, 29)),
status=ParkingPermitStatus.CLOSED,
)
valid_permits = ParkingPermit.objects.filter(status=ParkingPermitStatus.VALID)
Expand All @@ -58,30 +58,30 @@ def test_automatic_expiration_permits(self):
self.assertEqual(draft_permits.count(), 0)
self.assertEqual(closed_permits.count(), 2)

@freeze_time(datetime(2023, 11, 30, 0, 22))
@freeze_time(tz.make_aware(datetime(2023, 11, 30, 0, 22)))
def test_automatic_expiration_permits_with_primary_permit_change(self):
ParkingPermitFactory(
id=80000010,
customer=self.customer,
end_time=datetime(2023, 11, 29, 23, 59, tzinfo=dt_tz.utc),
end_time=tz.make_aware(datetime(2023, 11, 29, 23, 59)),
status=ParkingPermitStatus.VALID,
primary_vehicle=True,
)
ParkingPermitFactory(
id=80000020,
customer=self.customer,
end_time=datetime(2023, 12, 1, 23, 59, tzinfo=dt_tz.utc),
end_time=tz.make_aware(datetime(2023, 12, 1, 23, 59)),
status=ParkingPermitStatus.VALID,
primary_vehicle=False,
)
ParkingPermitFactory(
customer=self.customer,
end_time=datetime(2023, 11, 29, tzinfo=dt_tz.utc),
end_time=tz.make_aware(datetime(2023, 11, 29)),
status=ParkingPermitStatus.DRAFT,
)
ParkingPermitFactory(
customer=self.customer,
end_time=datetime(2023, 12, 1, tzinfo=dt_tz.utc),
end_time=tz.make_aware(datetime(2023, 12, 1)),
status=ParkingPermitStatus.DRAFT,
)
valid_permits = ParkingPermit.objects.filter(status=ParkingPermitStatus.VALID)
Expand All @@ -102,35 +102,35 @@ def test_automatic_expiration_permits_with_primary_permit_change(self):
self.assertEqual(closed_permit.id, 80000010)
self.assertEqual(
closed_permit.end_time,
datetime(2023, 11, 29, 23, 59, 59, 999999, tzinfo=dt_tz.utc),
tz.make_aware(datetime(2023, 11, 29, 23, 59, 59, 999999)),
)

@freeze_time(datetime(2023, 11, 30, 0, 22))
@freeze_time(tz.make_aware(datetime(2023, 11, 30, 0, 22)))
def test_automatic_expiration_permits_with_permit_vehicle_changed(self):
ParkingPermitFactory(
id=80000010,
customer=self.customer,
end_time=datetime(2023, 12, 29, 23, 59, tzinfo=dt_tz.utc),
end_time=tz.make_aware(datetime(2023, 12, 29, 23, 59)),
vehicle_changed=True,
vehicle_changed_date=datetime(2023, 11, 29, tzinfo=dt_tz.utc).date(),
vehicle_changed_date=tz.make_aware(datetime(2023, 11, 29)).date(),
status=ParkingPermitStatus.VALID,
primary_vehicle=True,
)
ParkingPermitFactory(
id=80000020,
customer=self.customer,
end_time=datetime(2023, 12, 29, tzinfo=dt_tz.utc),
end_time=tz.make_aware(datetime(2023, 12, 29)),
status=ParkingPermitStatus.VALID,
primary_vehicle=False,
)
ParkingPermitFactory(
customer=self.customer,
end_time=datetime(2023, 11, 29, tzinfo=dt_tz.utc),
end_time=tz.make_aware(datetime(2023, 11, 29)),
status=ParkingPermitStatus.DRAFT,
)
ParkingPermitFactory(
customer=self.customer,
end_time=datetime(2023, 12, 1, tzinfo=dt_tz.utc),
end_time=tz.make_aware(datetime(2023, 12, 1)),
status=ParkingPermitStatus.DRAFT,
)
valid_permits = ParkingPermit.objects.filter(status=ParkingPermitStatus.VALID)
Expand All @@ -152,7 +152,7 @@ def test_automatic_expiration_permits_with_permit_vehicle_changed(self):
self.assertEqual(closed_permit.id, 80000010)
self.assertEqual(
closed_permit.end_time,
datetime(2023, 11, 29, 23, 59, 59, 999999, tzinfo=dt_tz.utc),
tz.make_aware(datetime(2023, 11, 29, 23, 59, 59, 999999)),
)


Expand Down

0 comments on commit 8ec09ce

Please sign in to comment.