forked from kezabelle/django-intercoolerjs-helpers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_urls.py
117 lines (88 loc) · 3.27 KB
/
test_urls.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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals, absolute_import
from uuid import uuid4
from django.conf.urls import url
from django.forms import Form, CharField, IntegerField
from django.http import HttpResponse, Http404
from django.shortcuts import redirect
from django.template.defaultfilters import pluralize
from django.template.response import TemplateResponse
try:
from django.urls import reverse
except ImportError: # Django <1.10
from django.core.urlresolvers import reverse
def _page_data():
return tuple(str(uuid4()) for x in range(0, 10))
clicktrack = 0
def click(request):
global clicktrack
clicktrack += 1
do_reset = (request.is_intercooler() and
request.intercooler_data.element.id == 'intro-btn2' and
request.intercooler_data.current_url.match is not None)
if do_reset:
clicktrack = 0
time = pluralize(clicktrack)
text = "<span>You clicked me {} time{}...</span>".format(clicktrack, time)
if do_reset:
text = "<span>You reset the counter!, via {}</span>".format(request.intercooler_data.trigger.id)
if not request.is_intercooler():
raise Http404("Not allowed to come here outside of an Intercooler.js request!")
resp = HttpResponse(text)
return resp
def redirector(request):
return redirect(reverse('redirected'))
def redirected(request):
return TemplateResponse(request, template="redirected.html", context={})
class TestForm(Form):
field = CharField()
number = IntegerField(max_value=10, min_value=5)
def form(request):
template = "form.html"
_form = TestForm(request.POST or None)
if _form.is_valid():
return redirect(reverse('redirected'))
context = {'form': _form}
return TemplateResponse(request, template=template, context=context)
def polling_stop(request):
resp = HttpResponse("Cancelled")
resp['X-IC-CancelPolling'] = "true"
return resp
def polling_start(request):
resp = HttpResponse("")
resp['X-IC-ResumePolling'] = "true"
return resp
def polling(request):
template = "polling.html"
if request.is_intercooler():
template = "polling_response.html"
context = {
'item': str(uuid4()),
}
return TemplateResponse(request, template=template, context=context)
def infinite_scrolling(request):
template = "infinite_scrolling.html"
if request.is_intercooler():
template = "infinite_scrolling_include.html"
context = {
'rows': _page_data(),
}
return TemplateResponse(request, template=template, context=context)
def root(request):
template = "demo_project.html"
context = {
'rows': _page_data(),
'form': TestForm()
}
return TemplateResponse(request, template=template, context=context)
urlpatterns = [
url('^form/$', form, name='form'),
url('^redirector/redirected/$', redirected, name='redirected'),
url('^redirector/$', redirector, name='redirector'),
url('^click/$', click, name='click'),
url('^polling/stop/$', polling_stop, name='polling_stop'),
url('^polling/start/$', polling_start, name='polling_start'),
url('^polling/$', polling, name='polling'),
url('^infinite/scrolling/$', infinite_scrolling, name='infinite_scrolling'),
url('^$', root, name='root'),
]