Skip to content

Commit

Permalink
Fix data_timezone when saving addupi telemetry
Browse files Browse the repository at this point in the history
When saving addupi telemetry, time zone was set to the empty string
instead of "UTC", resulting in error when attempting to fetch the data.
  • Loading branch information
aptiko committed Dec 30, 2023
1 parent 64ab6c5 commit 16620ee
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 3 deletions.
4 changes: 3 additions & 1 deletion enhydris/telemetry/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ def __init__(self, *args, **kwargs):
if self.driver.hide_device_locator:
self.fields["device_locator"].widget = forms.HiddenInput()
if self.driver.hide_data_timezone:
self.fields["data_timezone"].widget = forms.HiddenInput()
self.fields["data_timezone"].widget = forms.HiddenInput(
attrs={"value": "UTC"}
)
self.fields["data_timezone"].required = False

def clean(self):
Expand Down
10 changes: 9 additions & 1 deletion enhydris/telemetry/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from traceback import print_tb

from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.db import IntegrityError, models
from django.utils.translation import ugettext_lazy as _

import iso8601
Expand Down Expand Up @@ -117,6 +117,14 @@ def _cleanup_measurements(self, measurements):
result.seek(0)
return result

def _check_data_timezone(self):
if self.data_timezone not in [choice[0] for choice in timezone_choices]:
raise IntegrityError(f"'{self.data_timezone}' is not a valid time zone")

def save(self, *args, **kwargs):
self._check_data_timezone()
super().save(*args, **kwargs)


class Sensor(models.Model):
telemetry = models.ForeignKey(Telemetry, on_delete=models.CASCADE)
Expand Down
5 changes: 5 additions & 0 deletions enhydris/telemetry/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ def test_data_timezone_hidden(self):
widget_class_name = form.fields["data_timezone"].widget.__class__.__name__
self.assertEqual(widget_class_name, "HiddenInput")

def test_data_timezone_hidden_value(self):
TestTelemetryAPIClient.hide_data_timezone = True
form = ConnectionDataForm(*self.form_args, **self.form_kwargs)
self.assertEqual(form.fields["data_timezone"].widget.attrs["value"], "UTC")

def test_data_timezone_not_hidden(self):
TestTelemetryAPIClient.hide_data_timezone = False
form = ConnectionDataForm(*self.form_args, **self.form_kwargs)
Expand Down
14 changes: 14 additions & 0 deletions enhydris/telemetry/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from unittest.mock import MagicMock, patch

from django.core.exceptions import ValidationError
from django.db import IntegrityError
from django.test import TestCase

from freezegun import freeze_time
Expand All @@ -17,6 +18,19 @@
from enhydris.telemetry.models import Telemetry, TelemetryLogMessage, fix_zone_name


class TelemetryTestCase(TestCase):
def test_cannot_save_wrong_data_timezone(self):
with self.assertRaisesRegex(IntegrityError, "'' is not a valid time zone"):
Telemetry.objects.create(
station=mommy.make(Station),
type="meteoview2",
fetch_interval_minutes=10,
fetch_offset_minutes=2,
additional_config="{}",
data_timezone="",
)


class TelemetryFetchValidatorsTestCase(TestCase):
@classmethod
def setUpClass(cls):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ def _post_step_2(cls):
"data_timezone": "Europe/Athens",
"username": "[email protected]",
"password": "topsecret",
"data_timezone": "Europe/Athens",
},
)

Expand Down

0 comments on commit 16620ee

Please sign in to comment.