Form field handling improvements - fixing field list handler for empty lines

pull/512/head
dgtlmoon 3 years ago
parent 501183e66b
commit 015353eccc

@ -37,27 +37,31 @@ valid_method = {
default_method = 'GET' default_method = 'GET'
class StringListField(StringField): class StringListField(StringField):
widget = widgets.TextArea() widget = widgets.TextArea()
def _value(self): def _value(self):
if self.data: 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: else:
return u'' return u''
# incoming # incoming
def process_formdata(self, valuelist): def process_formdata(self, valuelist):
if valuelist: if valuelist and len(valuelist[0].strip()):
# Remove empty strings # Remove empty strings, stripping and splitting \r\n, only \n etc.
cleaned = list(filter(None, valuelist[0].split("\n"))) self.data = valuelist[0].splitlines()
self.data = [x.strip() for x in cleaned] # Remove empty lines from the final data
p = 1 self.data = list(filter(lambda x: len(x.strip()), self.data))
else: else:
self.data = [] self.data = []
class SaltyPasswordField(StringField): class SaltyPasswordField(StringField):
widget = widgets.PasswordInput() widget = widgets.PasswordInput()
encrypted_password = "" encrypted_password = ""

@ -20,7 +20,7 @@ def test_check_watch_field_storage(client, live_server):
res = client.post( res = client.post(
url_for("edit_page", uuid="first"), 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, "minutes_between_check": 126,
"css_filter" : ".fooclass", "css_filter" : ".fooclass",
"title" : "My title", "title" : "My title",
@ -38,8 +38,14 @@ def test_check_watch_field_storage(client, live_server):
url_for("edit_page", uuid="first"), url_for("edit_page", uuid="first"),
follow_redirects=True 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"126" in res.data
assert b".fooclass" in res.data assert b".fooclass" in res.data
assert b"My title" in res.data assert b"My title" in res.data

Loading…
Cancel
Save