Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize newlines from form #397

Merged
merged 3 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions constance/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from django.utils.encoding import smart_bytes
from django.utils.formats import localize
from django.utils.module_loading import import_string
from django.utils.text import normalize_newlines
from django.utils.translation import ugettext_lazy as _

from . import LazyConfig, settings
Expand Down Expand Up @@ -146,6 +147,9 @@ def save(self):
current = getattr(config, name)
new = self.cleaned_data[name]

if isinstance(new, str):
new = normalize_newlines(new)

if conf.settings.USE_TZ and isinstance(current, datetime) and not timezone.is_aware(current):
current = timezone.make_aware(current)

Expand Down
1 change: 1 addition & 0 deletions example/cheeseshop/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
'MUSICIANS': (4, 'number of musicians inside the shop'),
'DATE_ESTABLISHED': (date(1972, 11, 30), "the shop's first opening"),
'MY_SELECT_KEY': ('yes', 'select yes or no', 'yes_no_null_select'),
'MULTILINE': ('Line one\nLine two', 'multiline string'),
}

CONSTANCE_BACKEND = 'constance.backends.database.DatabaseBackend'
Expand Down
21 changes: 21 additions & 0 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from constance import settings
from constance.admin import Config
from constance.admin import get_values


class TestAdmin(TestCase):
Expand Down Expand Up @@ -117,6 +118,26 @@ def test_submit(self):
response = self.options.changelist_view(request, {})
self.assertIsInstance(response, HttpResponseRedirect)

@mock.patch('constance.settings.CONFIG_FIELDSETS', {
'FieldSetOne': ('MULTILINE',)
})
@mock.patch('constance.settings.CONFIG', {
'MULTILINE': ('Hello\nWorld', 'multiline value'),
})
@mock.patch('constance.settings.IGNORE_ADMIN_VERSION_CHECK', True)
def test_newlines_normalization(self):
self.client.login(username='admin', password='nimda')
request = self.rf.post('/admin/constance/config/', data={
"MULTILINE": "Hello\r\nWorld",
"version": "123",
})
request.user = self.superuser
request._dont_enforce_csrf_checks = True
with mock.patch("django.contrib.messages.add_message"):
response = self.options.changelist_view(request, {})
self.assertIsInstance(response, HttpResponseRedirect)
self.assertEqual(get_values()['MULTILINE'], 'Hello\nWorld')

@mock.patch('constance.settings.CONFIG', {
'DATETIME_VALUE': (datetime(2019, 8, 7, 18, 40, 0), 'some naive datetime'),
})
Expand Down