Skip to content

Commit f78d4cc

Browse files
committed
Merge branch 'release/6.0.0'
2 parents 52c196f + a42aba2 commit f78d4cc

33 files changed

+881
-268
lines changed

.coveragerc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[run]
2+
branch = true
3+
omit =
4+
*/tests/*
5+
*/migrations/*
6+
*/urls.py
7+
*/settings/*
8+
*/wsgi.py
9+
example_django_react_templatetags/*
10+
venv/*
11+
manage.py
12+
runtests.py
13+
setup.py
14+
source = .
15+
16+
[report]
17+
show_missing = true

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,6 @@ example_django_react_templatetags/django_react_templatetags
5050

5151
# DB
5252
*.sqlite3
53+
54+
# Pyenv
55+
.python-version

.travis.yml

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
language: python
22

33
env:
4-
- DJANGO_VERSION=1.11.8
5-
- DJANGO_VERSION=2.0
4+
- DJANGO_VERSION=2.2.11
5+
- DJANGO_VERSION=3.0.4
66

77
python:
8-
- "2.7"
98
- "3.5"
109
- "3.6"
11-
- "3.7-dev"
12-
- "pypy"
10+
- "3.7"
11+
- "3.8"
1312

1413
matrix:
1514
exclude:
16-
- python: "2.7"
17-
env: DJANGO_VERSION=2.0
18-
- python: "pypy"
19-
env: DJANGO_VERSION=2.0
20-
- python: "3.7-dev"
21-
env: DJANGO_VERSION=1.11.8
15+
- python: "3.5"
16+
env: DJANGO_VERSION=3.0.4
2217

2318
install:
24-
- pip install -q Django==$DJANGO_VERSION requests responses mock
19+
- pip install -q Django==$DJANGO_VERSION requests mock hypernova
2520

2621
script:
2722
- python runtests.py

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This django library allows you to add React (16+) components into your django te
1313
- Include react components using django templatetags
1414
- Unlimited amount of components in one view
1515
- Support custom models (that is from the beginning not json-serializable)
16-
- Server side rendering
16+
- Server side rendering with [Hypernova](https://github.com/airbnb/hypernova) or [Hastur](https://github.com/frojd/Hastur)
1717

1818

1919
## Installation
@@ -29,9 +29,10 @@ $ pip install django_react_templatetags
2929

3030
You should first read [Getting started](https://github.com/Frojd/django-react-templatetags/blob/develop/docs/getting-started.md), then go through these topics:
3131
- [Settings](https://github.com/Frojd/django-react-templatetags/blob/develop/docs/settings.md)
32-
- [Adding a single component](https://github.com/Frojd/django-react-templatetags/blob/develop/<Paste>docs/example-single-component.md)
32+
- [How to use the templatetags included in this library](https://github.com/Frojd/django-react-templatetags/blob/develop/docs/templatetags-params.md)
33+
- [Adding a single component](https://github.com/Frojd/django-react-templatetags/blob/develop/docs/example-single-component.md)
3334
- [Adding multiple components](https://github.com/Frojd/django-react-templatetags/blob/develop/docs/example-multiple-components.md)
34-
- [Example application](https://github.com/Frojd/django-react-templatetags/blob/develop/docs/example-application.md)
35+
- [Examples](https://github.com/Frojd/django-react-templatetags/blob/develop/docs/examples.md)
3536
- [Working with models](https://github.com/Frojd/django-react-templatetags/blob/develop/docs/working-with-models.md)
3637
- [Server side rendering](https://github.com/Frojd/django-react-templatetags/blob/develop/docs/server-side-rendering.md)
3738
- [FAQ](https://github.com/Frojd/django-react-templatetags/blob/develop/docs/faq.md)
@@ -44,6 +45,11 @@ This library include tests, just run `python runtests.py`
4445
You can also run separate test cases: `python runtests.py tests.test_filters.ReactIncludeComponentTest`
4546

4647

48+
## Coverage
49+
50+
Make sure you have Coverage.py installed, then run `coverage run runtests.py` to measure coverage. We are currently at 95%.
51+
52+
4753
## Contributing
4854

4955
Want to contribute? Awesome. Just send a pull request.

django_react_templatetags/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"""
99

1010
__title__ = "django_react_templatetags"
11-
__version__ = "5.4.0"
12-
__build__ = 540
11+
__version__ = "6.0.0"
12+
__build__ = 541
1313
__author__ = "Martin Sandström"
1414
__license__ = "MIT"
15-
__copyright__ = "Copyright 2015-2019 Fröjd Interactive"
15+
__copyright__ = "Copyright 2015-2020 Fröjd Interactive"

django_react_templatetags/context_processors.py

100644100755
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4+
import warnings
5+
46

57
def react_context_processor(request):
68
"""Expose a global list of react components to be processed"""
9+
warnings.warn(
10+
"react_context_processor is no longer required.", DeprecationWarning
11+
)
712

813
return {
914
'REACT_COMPONENTS': [],

django_react_templatetags/encoders.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ class ReactRepresentationJSONEncoder(DjangoJSONEncoder):
2020

2121
def default(self, o):
2222
if isinstance(o, RepresentationMixin):
23-
# Allow backwards compability with react_representation prop
24-
if not hasattr(o, 'to_react_representation'):
25-
return o.react_representation
26-
2723
args = [self.context if hasattr(self, 'context') else None]
2824
args = [x for x in args if x is not None]
2925

django_react_templatetags/ssr/__init__.py

Whitespace-only changes.

django_react_templatetags/ssr.py renamed to django_react_templatetags/ssr/default.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ def load_or_empty(self, component, headers={}, ssr_context=None):
3333
)
3434
logger.exception(msg)
3535

36-
return inner_html
36+
return {
37+
"html": inner_html,
38+
"params": {},
39+
}
3740

3841

3942
def load(self, request_json, headers):
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import logging
2+
import json
3+
import re
4+
5+
from django.conf import settings
6+
import hypernova
7+
8+
9+
logger = logging.getLogger(__name__)
10+
hypernova_id_re = re.compile(r"data-hypernova-id=\"([\w\-]*)\"")
11+
hypernova_key_re = re.compile(r"data-hypernova-key=\"([\w\-]*)\"")
12+
13+
14+
class HypernovaService:
15+
def load_or_empty(self, component, headers={}, ssr_context=None):
16+
# from hypernova.plugins.dev_mode import DevModePlugin
17+
18+
renderer = hypernova.Renderer(
19+
settings.REACT_RENDER_HOST,
20+
# [DevModePlugin(logger)] if settings.DEBUG else [],
21+
[],
22+
timeout=get_request_timeout(),
23+
headers=headers,
24+
)
25+
26+
inner_html = ""
27+
try:
28+
props = json.loads(component["json"])
29+
if ssr_context:
30+
props["context"] = ssr_context
31+
inner_html = renderer.render({component["name"]: props})
32+
except Exception as e:
33+
msg = "SSR request to '{}' failed: {}".format(
34+
settings.REACT_RENDER_HOST, e.__class__.__name__
35+
)
36+
logger.exception(msg)
37+
38+
if not inner_html:
39+
return {"html": "", "params": {}}
40+
41+
match = re.search(hypernova_id_re, inner_html)
42+
hypernova_id = match.group(1) if match else None
43+
44+
match = re.search(hypernova_key_re, inner_html)
45+
hypernova_key = match.group(1) if match else None
46+
47+
return {
48+
"html": inner_html,
49+
"params": {"hypernova_id": hypernova_id, "hypernova_key": hypernova_key,},
50+
}
51+
52+
53+
def get_request_timeout():
54+
if not hasattr(settings, "REACT_RENDER_TIMEOUT"):
55+
return 20
56+
57+
return settings.REACT_RENDER_TIMEOUT

0 commit comments

Comments
 (0)