Skip to content

Commit

Permalink
Deciding whether to return HTML or not once is enough
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed Feb 1, 2019
1 parent c4a5640 commit a2a0eed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
17 changes: 9 additions & 8 deletions form_designer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from django.db import models
from django.db.models.fields import BLANK_CHOICE_DASH
from django.utils.encoding import python_2_unicode_compatible
from django.utils.html import format_html, mark_safe
from django.utils.html import format_html, format_html_join
from django.utils.module_loading import import_string
from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _
Expand Down Expand Up @@ -262,13 +262,14 @@ def sorted_data(self, include=()):
return data

def formatted_data(self, html=False):
formatted = ""
for key, value in self.sorted_data().items():
if html:
formatted += format_html("<dt>{}</dt><dd>{}</dd>\n", key, value)
else:
formatted += "%s: %s\n" % (key, value)
return mark_safe("<dl>%s</dl>" % formatted) if html else formatted
if html:
return format_html(
"<dl>{}</dl>",
format_html_join(
"", "<dt>{}</dt><dd>{}</dd>", self.sorted_data().items()
),
)
return "".join("%s: %s\n" % item for item in self.sorted_data().items())

def formatted_data_html(self):
return self.formatted_data(html=True)
5 changes: 5 additions & 0 deletions tests/testapp/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ def test_forms(self):
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
)

response = self.client.get(
"/admin/form_designer/formsubmission/{}/change/".format(submission.id)
)
self.assertContains(response, "<dt>body</dt>")

def test_admin(self):
User.objects.create_superuser("admin", "admin@example.com", "password")
self.client.login(username="admin", password="password")
Expand Down

0 comments on commit a2a0eed

Please sign in to comment.