-
Notifications
You must be signed in to change notification settings - Fork 32
/
views.py
235 lines (190 loc) · 7 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import os
from django.http import Http404
from django.shortcuts import render, render_to_response, redirect
from django.template import RequestContext
from django.views.generic import TemplateView, DetailView
from webapp.apps.register.forms import SubscribeForm
from django.template.context_processors import csrf
from django.conf import settings
from django.views.decorators.clickjacking import xframe_options_exempt
import requests
import btax
import taxcalc
BTAX_VERSION = btax.__version__
TAXCALC_VERSION = taxcalc.__version__
BLOG_URL = os.environ.get('BLOG_URL', 'www.ospc.org')
EMAIL_DEFAULT = '1'
def settings_context_processor(request):
return {'BLOG_URL': settings.BLOG_URL}
def subscribeform(request):
if request.method == 'POST':
subscribeform = SubscribeForm(request.POST)
if subscribeform.is_valid():
subscriber = subscribeform.save()
subscriber.send_subscribe_confirm_email()
else:
subscribeform = SubscribeForm()
return subscribeform
def subscribed(request):
return render(request, 'pages/thanks-for-subscribing.html')
def check_email(request):
return render(request, 'register/please-check-email.html', {})
def homepage(request):
form = subscribeform(request)
csrf_token = csrf(request)
if request.method == 'POST' and form.is_valid():
return check_email(request)
test = render(request, 'pages/home_content.html', {
'csrv_token': csrf(request)['csrf_token'],
'email_form': form,
'section': {
'active_nav': 'home',
'title': 'Welcome to the Open Source Policy Center',
},
'username': request.user
})
return test
def aboutpage(request):
form = subscribeform(request)
if request.method == 'POST' and form.is_valid():
return check_email(request)
test_1 = render(request, 'pages/about.html', {
'csrv_token': csrf(request)['csrf_token'],
'email_form': form,
'section': {
'active_nav': 'about',
'title': 'About',
}
})
return test_1
def gallerypage(request):
return render(request, 'pages/gallery.html', {
'manifest_url': os.environ.get('TAXPLOT_MANIFEST_URL'),
'section': {
'active_nav': 'gallery',
'title': 'Open Source Policy Center Gallery',
},
})
def hellopage(request):
return render(request, 'pages/hello.html', {
'manifest_url': os.environ.get('TAXPLOT_MANIFEST_URL'),
'section': {
'active_nav': 'hello',
'title': 'Hello',
},
})
def newspage(request):
return redirect(BLOG_URL)
def newsdetailpage(request):
return redirect(BLOG_URL)
def docspage(request):
return render(request, 'pages/docs.html', {
'section': {
'active_nav': 'docs',
'title': 'Open Source Policy Center Documentation',
},
})
def gettingstartedpage(request):
return render(request, 'pages/gettingstarted.html', {
'section': {
'active_nav': 'getting-started',
'title': 'Getting Started',
},
})
def _discover_widgets():
'''stubbed out data I wish to recieve from some widget discovery mechanism'''
manifest_url = os.environ.get('TAXPLOT_MANIFEST_URL')
if not manifest_url:
raise ValueError('TAXPLOT_MANIFEST_URL environment variable not set')
resp = requests.get(manifest_url)
resp.raise_for_status()
widgets = resp.json()
return { w['plot_id'] : w for w in widgets }
def widgetpage(request, widget_id):
widgets = _discover_widgets()
if widget_id not in list(widgets.keys()):
raise ValueError('Invalid Widget Id {0}'.format(widget_id))
widget = widgets[widget_id]
form = subscribeform(request)
if request.method == 'POST' and form.is_valid():
return check_email(request)
request.get_host()
embed_url = os.path.join('http://',request.get_host(), 'gallery', 'embed', widget_id)
if request.method == 'GET':
include_email = request.GET.get('includeEmail', EMAIL_DEFAULT) == '1'
else:
include_email = False
return render(request, 'pages/widget.html', {
'csrv_token': csrf(request)['csrf_token'],
'email_form': form,
'embed_url': embed_url,
'include_email': include_email,
'best_width': widget.get('best_width'),
'best_height': widget.get('best_height'),
'best_width_portrait': widget.get('best_width_portrait'),
'best_height_portrait': widget.get('best_height_portrait'),
'widget_title': widget['plot_name'],
'widget_url': widget['plot_url'],
'long_description': widget['long_description'],
'Concept_credit': widget['Concept_credit'],
'Development_credit': widget['Development_credit'],
'OSS_credit': widget['OSS_credit'],
'section': {
'title': 'Widget',
}
})
def border_adjustment_plot(request):
return render(request, 'pages/border_adjustment.html', {
'section': {
'title': 'Widget',
}
})
@xframe_options_exempt
def embedpage(request, widget_id, layout='landscape'):
form = subscribeform(request)
widgets = _discover_widgets()
if widget_id not in list(widgets.keys()):
raise ValueError('Invalid Widget Id {0}'.format(widget_id))
widget = widgets[widget_id]
form = subscribeform(request)
if request.method == 'POST' and form.is_valid():
return check_email(request)
if request.method == 'GET':
include_email = request.GET.get('includeEmail', '') == '1'
else:
include_email = False
gallery_link = os.path.join('http://',request.get_host(), 'gallery', widget_id)
if layout == 'portrait':
response_obj = {
'best_width': widget.get('best_width_portrait'),
'best_height': widget.get('best_height_portrait'),
'widget_title': widget['plot_name'],
'widget_url': widget['plot_url'].replace('landscape','portrait'),
'email_form': form,
'include_email': include_email,
'long_description': widget['long_description'],
'Concept_credit': widget['Concept_credit'],
'Development_credit': widget['Development_credit'],
'OSS_credit': widget['OSS_credit'],
'gallery_link': gallery_link,
}
else:
response_obj = {
'best_width': widget.get('best_width'),
'best_height': widget.get('best_height'),
'widget_title': widget['plot_name'],
'widget_url': widget['plot_url'],
'email_form': form,
'include_email': include_email,
'long_description': widget['long_description'],
'Concept_credit': widget['Concept_credit'],
'Development_credit': widget['Development_credit'],
'OSS_credit': widget['OSS_credit'],
'gallery_link': gallery_link,
}
return render(request, 'pages/embed.html', response_obj)
def apps_landing_page(request):
context = {'btax_version': BTAX_VERSION,
'taxcalc_version': TAXCALC_VERSION,
}
return render(request, 'pages/apps.html', context)