Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f5321a8

Browse filesBrowse files
committed
Major refactor to simplify django-react's functionality.
- decoupled django-react from django-webpack - removed most of the API and front-end helpers - django-react's intended purpose is now primarily rendering
1 parent a5f213e commit f5321a8
Copy full SHA for f5321a8

36 files changed

+330-651Lines changed: 330 additions & 651 deletions
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎django_react/__init__.py‎

Copy file name to clipboard
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
from .components import ReactComponent
2-
from .bundles import ReactBundle
1+
from .component import ReactComponent
32
from .render import render_component
Collapse file

‎django_react/bundles.py‎

Copy file name to clipboardExpand all lines: django_react/bundles.py
-12Lines changed: 0 additions & 12 deletions
This file was deleted.
Collapse file

‎django_react/component.py‎

Copy file name to clipboard
+77Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import sys
2+
import json
3+
from django.core.serializers.json import DjangoJSONEncoder
4+
from django.utils import six
5+
from django.utils.safestring import mark_safe
6+
from .exceptions import PropSerializationError, RenderingError
7+
from .render import render_component
8+
9+
10+
class ReactComponent(object):
11+
path_to_source = None
12+
props = None
13+
serialized_props = None
14+
json_encoder_class = DjangoJSONEncoder
15+
16+
def __init__(self, path_to_source, **kwargs):
17+
self.path_to_source = path_to_source
18+
self.props = kwargs
19+
20+
def render_to_string(self):
21+
"""
22+
Render a component to its initial HTML. You can use this method to generate HTML
23+
on the server and send the markup down on the initial request for faster page loads
24+
and to allow search engines to crawl you pages for SEO purposes.
25+
"""
26+
27+
# While rendering templates Django will silently ignore some types of exceptions,
28+
# so we need to intercept them and raise our own class of exception
29+
try:
30+
return mark_safe(
31+
render_component(
32+
path_to_source=self.path_to_source,
33+
serialized_props=self.get_serialized_props(),
34+
)
35+
)
36+
except (TypeError, AttributeError) as e:
37+
six.reraise(RenderingError, RenderingError(*e.args), sys.exc_info()[2])
38+
39+
def render_to_static_markup(self):
40+
"""
41+
Similar to `ReactComponent.render_to_string`, except this doesn't create
42+
extra DOM attributes such as `data-react-id`, that React uses internally.
43+
This is useful if you want to use React as a simple static page generator,
44+
as stripping away the extra attributes can save lots of bytes.
45+
"""
46+
47+
# While rendering templates Django will silently ignore some types of exceptions,
48+
# so we need to intercept them and raise our own class of exception
49+
try:
50+
return mark_safe(
51+
render_component(
52+
path_to_source=self.path_to_source,
53+
serialized_props=self.get_serialized_props(),
54+
to_static_markup=True,
55+
)
56+
)
57+
except (TypeError, AttributeError) as e:
58+
six.reraise(RenderingError, RenderingError(*e.args), sys.exc_info()[2])
59+
60+
def get_serialized_props(self):
61+
"""
62+
Returns the ReactComponent's props as a JSON string
63+
"""
64+
65+
if self.serialized_props:
66+
return self.serialized_props
67+
68+
# While rendering templates Django will silently ignore some types of exceptions,
69+
# so we need to intercept them and raise our own class of exception
70+
try:
71+
self.serialized_props = mark_safe(
72+
json.dumps(self.props, cls=self.json_encoder_class)
73+
)
74+
except (TypeError, AttributeError) as e:
75+
six.reraise(PropSerializationError, PropSerializationError(*e.args), sys.exc_info()[2])
76+
77+
return self.serialized_props
Collapse file

‎django_react/components.py‎

Copy file name to clipboardExpand all lines: django_react/components.py
-258Lines changed: 0 additions & 258 deletions
This file was deleted.
Collapse file

‎django_react/exceptions.py‎

Copy file name to clipboard
-12Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
class ReactComponentCalledDirectly(Exception):
2-
pass
3-
4-
5-
class ReactComponentMissingSource(Exception):
6-
pass
7-
8-
91
class SourceFileNotFound(Exception):
102
pass
113

@@ -15,8 +7,4 @@ class PropSerializationError(Exception):
157

168

179
class RenderingError(Exception):
18-
pass
19-
20-
21-
class ComponentBundlingError(Exception):
2210
pass
Collapse file

‎django_react/package.json‎

Copy file name to clipboard
+1-4Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
{
22
"private": true,
33
"dependencies": {
4-
"express": "^4.11.1",
54
"react": "^0.12.2",
6-
"yargs": "^1.3.3",
75
"babel": "~4.1.1",
8-
"babel-core": "~4.1.1",
9-
"babel-loader": "~4.0.0"
6+
"babel-core": "~4.1.1"
107
}
118
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.