Skip to content

Commit

Permalink
Fix bluk import
Browse files Browse the repository at this point in the history
  • Loading branch information
haneslinger committed Jan 5, 2024
1 parent 2f24661 commit b79a82d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
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
11 changes: 8 additions & 3 deletions seed/views/v3/meter_readings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
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.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 +101,10 @@ 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:
serializer.save(meter_id=self.meter_pk)
except ProgrammingError:
raise serializers.ValidationError('No meter_pk provided in URL to create the meter reading')

0 comments on commit b79a82d

Please sign in to comment.