From 015353eccc79470b08af6243cbc04b46c2ba557b Mon Sep 17 00:00:00 2001 From: dgtlmoon Date: Sun, 24 Apr 2022 13:12:50 +0200 Subject: [PATCH] Form field handling improvements - fixing field list handler for empty lines --- changedetectionio/forms.py | 18 +++++++++++------- .../tests/test_watch_fields_storage.py | 10 ++++++++-- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/changedetectionio/forms.py b/changedetectionio/forms.py index b0dd561c..45c1457a 100644 --- a/changedetectionio/forms.py +++ b/changedetectionio/forms.py @@ -37,27 +37,31 @@ valid_method = { default_method = 'GET' + class StringListField(StringField): widget = widgets.TextArea() def _value(self): if self.data: - return "\r\n".join(self.data) + # ignore empty lines in the storage + data = list(filter(lambda x: len(x.strip()), self.data)) + # Apply strip to each line + data = list(map(lambda x: x.strip(), data)) + return "\r\n".join(data) else: return u'' # incoming def process_formdata(self, valuelist): - if valuelist: - # Remove empty strings - cleaned = list(filter(None, valuelist[0].split("\n"))) - self.data = [x.strip() for x in cleaned] - p = 1 + if valuelist and len(valuelist[0].strip()): + # Remove empty strings, stripping and splitting \r\n, only \n etc. + self.data = valuelist[0].splitlines() + # Remove empty lines from the final data + self.data = list(filter(lambda x: len(x.strip()), self.data)) else: self.data = [] - class SaltyPasswordField(StringField): widget = widgets.PasswordInput() encrypted_password = "" diff --git a/changedetectionio/tests/test_watch_fields_storage.py b/changedetectionio/tests/test_watch_fields_storage.py index b57273d7..513c474e 100644 --- a/changedetectionio/tests/test_watch_fields_storage.py +++ b/changedetectionio/tests/test_watch_fields_storage.py @@ -20,7 +20,7 @@ def test_check_watch_field_storage(client, live_server): res = client.post( url_for("edit_page", uuid="first"), - data={ "notification_urls": "json://myapi.com", + data={ "notification_urls": "json://127.0.0.1:30000\r\njson://128.0.0.1\r\n", "minutes_between_check": 126, "css_filter" : ".fooclass", "title" : "My title", @@ -38,8 +38,14 @@ def test_check_watch_field_storage(client, live_server): url_for("edit_page", uuid="first"), follow_redirects=True ) + # checks that we dont get an error when using blank lines in the field value + assert not b"json://127.0.0.1\n\njson" in res.data + assert not b"json://127.0.0.1\r\n\njson" in res.data + assert not b"json://127.0.0.1\r\n\rjson" in res.data + + assert b"json://127.0.0.1" in res.data + assert b"json://128.0.0.1" in res.data - assert b"json://myapi.com" in res.data assert b"126" in res.data assert b".fooclass" in res.data assert b"My title" in res.data