Add fallback for legacy sms values

This commit is contained in:
Pēteris Caune 2021-05-21 13:05:37 +03:00
parent 855d188981
commit e91441d814
No known key found for this signature in database
GPG Key ID: E28D7679E9A9EDE2
2 changed files with 18 additions and 2 deletions

View File

@ -742,14 +742,20 @@ class Channel(models.Model):
@property
def sms_notify_up(self):
assert self.kind == "sms"
if not self.value.startswith("{"):
return False
doc = json.loads(self.value)
return doc["up"]
return doc.get("up", False)
@property
def sms_notify_down(self):
assert self.kind == "sms"
if not self.value.startswith("{"):
return True
doc = json.loads(self.value)
return doc["down"]
return doc.get("down", True)
@property
def opsgenie_key(self):

View File

@ -50,3 +50,13 @@ class ChannelModelTestCase(BaseTestCase):
c.value = json.dumps({"key": "abc", "region": "eu"})
self.assertEqual(c.opsgenie_key, "abc")
self.assertEqual(c.opsgenie_region, "eu")
def test_it_handles_legacy_sms_value(self):
c = Channel(kind="sms", value="+123123123")
self.assertTrue(c.sms_notify_down)
self.assertFalse(c.sms_notify_up)
def test_it_handles_legacy_sms_json_value(self):
c = Channel(kind="sms", value=json.dumps({"value": "+123123123"}))
self.assertTrue(c.sms_notify_down)
self.assertFalse(c.sms_notify_up)