forked from reactive-python/reactpy-django
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
198 lines (152 loc) · 5.15 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import inspect
from itertools import cycle
from channels.db import database_sync_to_async
from django.http import HttpRequest
from django.shortcuts import render
from django.views.generic import TemplateView, View
from .types import TestObject
def base_template(request):
return render(
request,
"base.html",
{"my_object": TestObject(1), "my_html_object": "<div> Hello World </div>"},
)
def errors_template(request):
return render(request, "errors.html", {})
def host_port_template(request: HttpRequest, port: int):
host = request.get_host().replace(str(request.get_port()), str(port))
return render(request, "host_port.html", {"new_host": host})
def host_port_roundrobin_template(
request: HttpRequest, port1: int, port2: int, count: int = 1
):
from reactpy_django import config
# Override ReactPy config to use round-robin hosts
original = config.REACTPY_DEFAULT_HOSTS
config.REACTPY_DEFAULT_HOSTS = cycle(
[
f"{request.get_host().split(':')[0]}:{port1}",
f"{request.get_host().split(':')[0]}:{port2}",
]
)
html = render(
request,
"host_port_roundrobin.html",
{"count": range(max(count, 1))},
)
# Reset ReactPy config
config.REACTPY_DEFAULT_HOSTS = original
return html
def view_to_component_sync_func(request):
return render(
request,
"view_to_component.html",
{"test_name": inspect.currentframe().f_code.co_name}, # type:ignore
)
async def view_to_component_async_func(request):
return render(
request,
"view_to_component.html",
{"test_name": inspect.currentframe().f_code.co_name}, # type:ignore
)
class ViewToComponentSyncClass(View):
def get(self, request, *args, **kwargs):
return render(
request,
"view_to_component.html",
{"test_name": self.__class__.__name__},
)
class ViewToComponentAsyncClass(View):
async def get(self, request, *args, **kwargs):
return await database_sync_to_async(render)(
request,
"view_to_component.html",
{"test_name": self.__class__.__name__},
)
class ViewToComponentTemplateViewClass(TemplateView):
template_name = "view_to_component.html"
def get_context_data(self, **kwargs):
return {"test_name": self.__class__.__name__}
def view_to_component_sync_func_compatibility(request):
return render(
request,
"view_to_component.html",
{"test_name": inspect.currentframe().f_code.co_name}, # type:ignore
)
async def view_to_component_async_func_compatibility(request):
return await database_sync_to_async(render)(
request,
"view_to_component.html",
{"test_name": inspect.currentframe().f_code.co_name}, # type:ignore
)
class ViewToComponentSyncClassCompatibility(View):
def get(self, request, *args, **kwargs):
return render(
request,
"view_to_component.html",
{"test_name": self.__class__.__name__},
)
class ViewToComponentAsyncClassCompatibility(View):
async def get(self, request, *args, **kwargs):
return await database_sync_to_async(render)(
request,
"view_to_component.html",
{"test_name": self.__class__.__name__},
)
class ViewToComponentTemplateViewClassCompatibility(TemplateView):
template_name = "view_to_component.html"
def get_context_data(self, **kwargs):
return {"test_name": self.__class__.__name__}
def view_to_iframe_args(request, arg1, arg2, kwarg1=None, kwarg2=None):
success = (
arg1 == "Arg1" and arg2 == "Arg2" and kwarg1 == "Kwarg1" and kwarg2 == "Kwarg2"
)
return render(
request,
"view_to_component.html",
{
"test_name": inspect.currentframe().f_code.co_name, # type:ignore
"status": "Success" if success else "false",
},
)
def view_to_component_script(request):
return render(
request,
"view_to_component_script.html",
{
"test_name": inspect.currentframe().f_code.co_name, # type:ignore
"status": "false",
},
)
def view_to_component_request(request):
if request.method == "POST":
return render(
request,
"view_to_component.html",
{"test_name": inspect.currentframe().f_code.co_name}, # type:ignore
)
return render(
request,
"view_to_component.html",
{
"test_name": inspect.currentframe().f_code.co_name, # type:ignore
"status": "false",
},
)
def view_to_component_args(request, success):
return render(
request,
"view_to_component.html",
{
"test_name": inspect.currentframe().f_code.co_name, # type:ignore
"status": success,
},
)
def view_to_component_kwargs(request, success="false"):
return render(
request,
"view_to_component.html",
{
"test_name": inspect.currentframe().f_code.co_name, # type:ignore
"status": success,
},
)