Skip to content

Commit b669cb7

Browse files
committed
Reintroduce the ability to easily use the render() return value in a string context
1 parent fe147ec commit b669cb7

15 files changed

Lines changed: 108 additions & 61 deletions

File tree

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ repos:
1212
- id: file-contents-sorter
1313
files: .gitignore
1414
- repo: https://github.com/asottile/pyupgrade
15-
rev: v2.32.0
15+
rev: v2.32.1
1616
hooks:
1717
- id: pyupgrade
1818
args: [--py38-plus]
1919
- repo: https://github.com/adamchainz/django-upgrade
20-
rev: 1.5.0
20+
rev: 1.7.0
2121
hooks:
2222
- id: django-upgrade
2323
args: [--target-version, "3.2"]
@@ -36,13 +36,13 @@ repos:
3636
- id: flake8
3737
args: ["--ignore=E203,E501,W503"]
3838
- repo: https://github.com/pre-commit/mirrors-eslint
39-
rev: v8.13.0
39+
rev: v8.15.0
4040
hooks:
4141
- id: eslint
4242
args: [--fix]
4343
verbose: true
4444
additional_dependencies:
45-
- eslint@8.13.0
45+
- eslint@8.15.0
4646
- eslint-config-prettier@8.5.0
4747
- "@babel/core"
4848
- "@babel/eslint-parser"

CHANGELOG.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ Change log
88

99
.. _Next version: https://github.com/feincms/feincms/compare/v22.2.0...main
1010

11+
- The ``render()`` methods of bundled content types have been changed to return
12+
a tuple instead of a HTML fragment in FeinCMS v22.0.0. This was backwards
13+
incompatible in some scenarios. Those methods have been changed to return a
14+
tuple subclass which automatically renders a HTML fragment if evaluated in a
15+
string context.
16+
1117

1218
`v22.2.0`_ (2022-05-06)
1319
~~~~~~~~~~~~~~~~~~~~~~~
@@ -38,8 +44,9 @@ Change log
3844

3945
.. _v22.0.0: https://github.com/feincms/feincms/compare/v1.20.0...v22.0.0
4046

41-
- Changed all bundled content types' ``render()`` methods to return the
42-
``(template_name, context)`` tuple instead of rendering content themselves.
47+
- **Possibly backwards incompatible** Changed all bundled content types'
48+
``render()`` methods to return the ``(template_name, context)`` tuple instead
49+
of rendering content themselves.
4350
- Dropped compatibility guarantees with Python < 3.6, Django < 3.2.
4451
- Added pre-commit.
4552
- The default view was changed to accept the path as a ``path`` keyword

feincms/content/file/models.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from django.utils.translation import gettext_lazy as _
1111

1212
from feincms import settings
13+
from feincms.utils.tuple import AutoRenderTuple
1314

1415

1516
class FileContent(models.Model):
@@ -29,7 +30,9 @@ class Meta:
2930
verbose_name_plural = _("files")
3031

3132
def render(self, **kwargs):
32-
return (
33-
["content/file/%s.html" % self.region, "content/file/default.html"],
34-
{"content": self},
33+
return AutoRenderTuple(
34+
(
35+
["content/file/%s.html" % self.region, "content/file/default.html"],
36+
{"content": self},
37+
)
3538
)

feincms/content/filer/models.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from django.utils.translation import gettext_lazy as _
55

66
from feincms.admin.item_editor import FeinCMSInline
7+
from feincms.utils.tuple import AutoRenderTuple
78

89

910
try:
@@ -35,14 +36,16 @@ class Meta:
3536
abstract = True
3637

3738
def render(self, **kwargs):
38-
return (
39-
[
40-
f"content/filer/{self.file_type}_{self.type}.html",
41-
"content/filer/%s.html" % self.type,
42-
"content/filer/%s.html" % self.file_type,
43-
"content/filer/default.html",
44-
],
45-
{"content": self},
39+
return AutoRenderTuple(
40+
(
41+
[
42+
f"content/filer/{self.file_type}_{self.type}.html",
43+
"content/filer/%s.html" % self.type,
44+
"content/filer/%s.html" % self.file_type,
45+
"content/filer/default.html",
46+
],
47+
{"content": self},
48+
)
4649
)
4750

4851
class FilerFileContent(ContentWithFilerFile):

feincms/content/image/models.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from feincms import settings
1313
from feincms.templatetags import feincms_thumbnail
14+
from feincms.utils.tuple import AutoRenderTuple
1415

1516

1617
class ImageContent(models.Model):
@@ -63,10 +64,7 @@ def render(self, **kwargs):
6364
if hasattr(self, "position"):
6465
templates.insert(0, "content/image/%s.html" % self.position)
6566

66-
return (
67-
templates,
68-
{"content": self},
69-
)
67+
return AutoRenderTuple((templates, {"content": self}))
7068

7169
def get_image(self):
7270
type, separator, size = getattr(self, "format", "").partition(":")

feincms/content/richtext/models.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from feincms import settings
55
from feincms.contrib.richtext import RichTextField
6+
from feincms.utils.tuple import AutoRenderTuple
67

78

89
class RichTextContent(models.Model):
@@ -29,10 +30,7 @@ class Meta:
2930
verbose_name_plural = _("rich texts")
3031

3132
def render(self, **kwargs):
32-
return (
33-
"content/richtext/default.html",
34-
{"content": self},
35-
)
33+
return AutoRenderTuple(("content/richtext/default.html", {"content": self}))
3634

3735
@classmethod
3836
def initialize_type(cls, cleanse=None):

feincms/content/section/models.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from feincms.contrib.richtext import RichTextField
1010
from feincms.module.medialibrary.fields import MediaFileForeignKey
1111
from feincms.module.medialibrary.models import MediaFile
12+
from feincms.utils.tuple import AutoRenderTuple
1213

1314

1415
class SectionContentInline(FeinCMSInline):
@@ -80,14 +81,16 @@ def render(self, **kwargs):
8081
else:
8182
mediafile_type = "nomedia"
8283

83-
return (
84-
[
85-
f"content/section/{mediafile_type}_{self.type}.html",
86-
"content/section/%s.html" % mediafile_type,
87-
"content/section/%s.html" % self.type,
88-
"content/section/default.html",
89-
],
90-
{"content": self},
84+
return AutoRenderTuple(
85+
(
86+
[
87+
f"content/section/{mediafile_type}_{self.type}.html",
88+
"content/section/%s.html" % mediafile_type,
89+
"content/section/%s.html" % self.type,
90+
"content/section/default.html",
91+
],
92+
{"content": self},
93+
)
9194
)
9295

9396
def save(self, *args, **kwargs):

feincms/content/template/models.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from feincms.content.raw.models import RawContent # noqa
55
from feincms.content.richtext.models import RichTextContent # noqa
6+
from feincms.utils.tuple import AutoRenderTuple
67

78

89
class TemplateContent(models.Model):
@@ -30,7 +31,4 @@ def initialize_type(cls, TEMPLATES):
3031
)
3132

3233
def render(self, **kwargs):
33-
return (
34-
self.template,
35-
{"content": self},
36-
)
34+
return AutoRenderTuple((self.template, {"content": self}))

feincms/content/video/models.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from django.db import models
44
from django.utils.translation import gettext_lazy as _
55

6+
from feincms.utils.tuple import AutoRenderTuple
7+
68

79
class VideoContent(models.Model):
810
"""
@@ -69,7 +71,4 @@ def ctx_for_video(self, vurl):
6971

7072
def render(self, **kwargs):
7173
ctx = self.ctx_for_video(self.video)
72-
return (
73-
self.get_templates(ctx["portal"]),
74-
ctx,
75-
)
74+
return AutoRenderTuple((self.get_templates(ctx["portal"]), ctx))

feincms/extensions/translations.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from django.conf import settings as django_settings
2323
from django.db import models
24-
from django.http import HttpResponseRedirect, HttpRequest
24+
from django.http import HttpRequest, HttpResponseRedirect
2525
from django.utils import translation
2626
from django.utils.safestring import mark_safe
2727
from django.utils.translation import gettext_lazy as _
@@ -43,6 +43,7 @@
4343

4444
PRIMARY_LANGUAGE: str = django_settings.LANGUAGES[0][0]
4545

46+
4647
# ------------------------------------------------------------------------
4748
def user_has_language_set(request: HttpRequest) -> bool:
4849
"""
@@ -72,7 +73,9 @@ def translation_allowed_language(select_language: str) -> str:
7273

7374

7475
# ------------------------------------------------------------------------
75-
def translation_set_language(request: HttpRequest, select_language: str) -> Optional[HttpResponseRedirect]:
76+
def translation_set_language(
77+
request: HttpRequest, select_language: str
78+
) -> Optional[HttpResponseRedirect]:
7679
"""
7780
Set and activate a language, if that language is available.
7881
"""
@@ -95,7 +98,9 @@ def translation_set_language(request: HttpRequest, select_language: str) -> Opti
9598

9699
if hasattr(request, "session"):
97100
# User has a session, then set this language there
98-
current_session_language = request.session.get(LANGUAGE_SESSION_KEY, PRIMARY_LANGUAGE)
101+
current_session_language = request.session.get(
102+
LANGUAGE_SESSION_KEY, PRIMARY_LANGUAGE
103+
)
99104

100105
if select_language != current_session_language:
101106
request.session[LANGUAGE_SESSION_KEY] = select_language

0 commit comments

Comments
 (0)