Skip to content

Commit

Permalink
Permits: Normalize timestamps on bulk-create
Browse files Browse the repository at this point in the history
Do the full_clean calls before passing the data to bulk_create method so
that the timestamps are normalized in the saved objects too.
  • Loading branch information
suutari-ai committed Sep 16, 2019
1 parent 8ff9012 commit 470c2af
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
14 changes: 8 additions & 6 deletions parkings/models/permit.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from itertools import chain

from django.conf import settings
from django.contrib.gis.db import models as gis_models
from django.db import models, router, transaction
Expand Down Expand Up @@ -66,14 +68,14 @@ def by_area(self, area_identifier):
return self.filter(lookup_items__in=lookup_items).distinct()

def bulk_create(self, permits, *args, **kwargs):
for permit in permits:
assert isinstance(permit, Permit)
permit.full_clean()

with transaction.atomic(using=self.db, savepoint=False):
created_permits = super().bulk_create(permits, *args, **kwargs)
lookup_items = []
for permit in created_permits:
assert isinstance(permit, Permit)
permit.full_clean()
lookup_items.extend(permit._make_lookup_items())
PermitLookupItem.objects.using(self.db).bulk_create(lookup_items)
PermitLookupItem.objects.using(self.db).bulk_create(
chain(*(x._make_lookup_items() for x in created_permits)))
return created_permits


Expand Down
52 changes: 50 additions & 2 deletions parkings/tests/api/enforcement/test_permit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from ....factories.permit import (
generate_areas, generate_external_ids, generate_subjects)
from ....models import PermitLookupItem
from ....models import Permit, PermitLookupItem

list_url = reverse('enforcement:v1:permit-list')

Expand Down Expand Up @@ -204,7 +204,8 @@ def test_permit_data_matches_permit_object(staff_api_client, permit):
check_permit_areas_keys(response.data['areas'][0])


def test_permit_bulk_create_creates_lookup_items(staff_api_client, permit_series):
def test_permit_bulk_create_creates_lookup_items(
staff_api_client, permit_series):
permit_data = [
{
"series": permit_series.id,
Expand All @@ -225,3 +226,50 @@ def test_permit_bulk_create_creates_lookup_items(staff_api_client, permit_series

assert response.status_code == HTTP_201_CREATED
assert PermitLookupItem.objects.count() == 2


def test_permit_bulk_create_normalizes_timestamps(
staff_api_client, permit_series):
permit_data = [
{
"series": permit_series.id,
"external_id": "E1",
'subjects': [{
'start_time': '1970-01-01T01:23:00+01:23',
'end_time': '2030-06-30T12:00+03:00',
'registration_number': 'REG-1',
}],
'areas': [{
'start_time': '1970-01-01T00:00:00Z',
'end_time': '2030-06-30T11:00+02:00',
'area': 'AREA 51',
}],
},
{
"series": permit_series.id,
"external_id": "E2",
'subjects': [{
'start_time': '1969-12-31T22:00:00-02:00',
'end_time': '2030-06-30T10:00+01:00',
'registration_number': 'REG-2',
}],
'areas': [{
'start_time': '1970-01-01T00:00:00+00:00',
'end_time': '2030-06-30T09:00Z',
'area': 'AREA 52',
}],
},
]

response = staff_api_client.post(list_url, data=permit_data)

assert response.status_code == HTTP_201_CREATED
(permit1, permit2) = list(Permit.objects.order_by('external_id'))
assert permit1.subjects[0]['start_time'] == '1970-01-01T00:00:00+00:00'
assert permit1.subjects[0]['end_time'] == '2030-06-30T09:00:00+00:00'
assert permit1.areas[0]['start_time'] == '1970-01-01T00:00:00+00:00'
assert permit1.areas[0]['end_time'] == '2030-06-30T09:00:00+00:00'
assert permit2.subjects[0]['start_time'] == '1970-01-01T00:00:00+00:00'
assert permit2.subjects[0]['end_time'] == '2030-06-30T09:00:00+00:00'
assert permit2.areas[0]['start_time'] == '1970-01-01T00:00:00+00:00'
assert permit2.areas[0]['end_time'] == '2030-06-30T09:00:00+00:00'

0 comments on commit 470c2af

Please sign in to comment.