Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bulk import #4466

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions seed/tests/test_meter_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,38 @@ def test_error_with_time_aware(self):
self.assertEqual(response.json()['status'], 'error')
self.assertEqual(response.json()['message'], 'end_time must be non-time zone aware')

def test_bulk_import_bad_time(self):
# create property
property_view = self.property_view_factory.get_property_view()
url = reverse('api:v3:property-meters-list', kwargs={'property_pk': property_view.id})

payload = {
'type': 'Electric',
'source': 'Manual Entry',
'source_id': '1234567890',
}

response = self.client.post(url, data=json.dumps(payload), content_type='application/json')
meter_pk = response.json()['id']

url = reverse('api:v3:property-meter-readings-list', kwargs={'property_pk': property_view.id, 'meter_pk': meter_pk})

# prepare the data in bulk format
payload = []
for values in [("2022-01-05 05:00:00", "2022-01-05 06:00:00", 22.2),
("2022-01-05 05:00:00", "2022-01-05 06:00:00", 88.8), ]:
payload.append({
"start_time": values[0],
"end_time": values[1],
"reading": values[2],
"source_unit": "Wh (Watt-hours)",
# conversion factor is required and is the conversion from the source unit to kBTU (1 Wh = 0.00341 kBtu)
"conversion_factor": 0.00341,
})

response = self.client.post(url, data=json.dumps(payload), content_type='application/json')
self.assertEqual(response.status_code, 400)

def test_bulk_import(self):
# create property
property_view = self.property_view_factory.get_property_view()
Expand Down
13 changes: 10 additions & 3 deletions seed/views/v3/meter_readings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
SEED Platform (TM), Copyright (c) Alliance for Sustainable Energy, LLC, and other contributors.
See also https://github.com/seed-platform/seed/main/LICENSE.md
"""
from django.db import transaction
from django.db.utils import ProgrammingError
from django.utils.decorators import method_decorator
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework import serializers
from rest_framework.parsers import FormParser, JSONParser
from rest_framework.renderers import JSONRenderer

Expand Down Expand Up @@ -99,7 +102,11 @@ def perform_create(self, serializer):
"""On create, make sure to add in the property id which comes from the URL kwargs."""

# check permissions?
if self.meter_pk:
serializer.save(meter_id=self.meter_pk)
else:
if self.meter_pk is None:
raise Exception('No meter_pk provided in URL to create the meter reading')

try:
with transaction.atomic():
serializer.save(meter_id=self.meter_pk)
except ProgrammingError:
raise serializers.ValidationError('No meter_pk provided in URL to create the meter reading')