Skip to content

Commit

Permalink
Merge pull request #397 from Mogost/issue-396
Browse files Browse the repository at this point in the history
Normalize newlines from form
  • Loading branch information
Mogost committed Jun 4, 2020
2 parents 731514c + 73fa362 commit 9e6dc03
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
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

0 comments on commit 9e6dc03

Please sign in to comment.