This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
views.py
274 lines (236 loc) · 8.61 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from django.core.context_processors import csrf
from django.conf.urls.defaults import patterns
from django.template import Context, loader
import simplejson
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from phantomweb.phantom_web_exceptions import PhantomWebException, PhantomRedirectException
from phantomweb.util import PhantomWebDecorator, get_user_object, LogEntryDecorator
from phantomweb.workload import terminate_iaas_instance, phantom_lc_load, phantom_sites_add, phantom_sites_delete, phantom_sites_load, phantom_lc_delete, phantom_lc_save, phantom_domain_load, phantom_domain_terminate, phantom_domain_resize, phantom_domain_start, phantom_domain_details, phantom_instance_terminate, phantom_sensors_load
from django.contrib import admin
@LogEntryDecorator
@login_required
def django_terminate_iaas_instance(request):
user_obj = get_user_object(request.user.username)
try:
response_dict = terminate_iaas_instance(request.GET, user_obj)
h = HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
finally:
user_obj.close()
return h
@LogEntryDecorator
@login_required
def django_domain_html(request):
try:
# no need to talk to the workload app here
response_dict = {}
response_dict.update(csrf(request))
response_dict['user'] = request.user
t = loader.get_template('phantom_domain.html')
c = Context(response_dict)
except PhantomRedirectException, ex:
return HttpResponseRedirect(ex.redir)
return HttpResponse(t.render(c))
@LogEntryDecorator
@login_required
def django_sensors_load(request):
user_obj = get_user_object(request.user.username)
try:
response_dict = phantom_sensors_load(request.GET, user_obj)
h = HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
finally:
user_obj.close()
return h
@LogEntryDecorator
@login_required
def django_domain_load(request):
user_obj = get_user_object(request.user.username)
try:
response_dict = phantom_domain_load(request.GET, user_obj)
h = HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
finally:
user_obj.close()
return h
@LogEntryDecorator
@login_required
def django_domain_start(request):
user_obj = get_user_object(request.user.username)
try:
response_dict = phantom_domain_start(request.POST, user_obj)
h = HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
finally:
user_obj.close()
return h
@LogEntryDecorator
@login_required
def django_domain_resize(request):
user_obj = get_user_object(request.user.username)
try:
response_dict = phantom_domain_resize(request.POST, user_obj)
h = HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
finally:
user_obj.close()
return h
@LogEntryDecorator
@login_required
def django_domain_details(request):
user_obj = get_user_object(request.user.username)
try:
response_dict = phantom_domain_details(request.POST, user_obj)
h = HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
finally:
user_obj.close()
return h
@LogEntryDecorator
@login_required
def django_domain_terminate(request):
user_obj = get_user_object(request.user.username)
try:
response_dict = phantom_domain_terminate(request.POST, user_obj)
h = HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
finally:
user_obj.close()
return h
@LogEntryDecorator
@login_required
def django_instance_terminate(request):
user_obj = get_user_object(request.user.username)
try:
response_dict = phantom_instance_terminate(request.POST, user_obj)
h = HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
finally:
user_obj.close()
return h
@LogEntryDecorator
@login_required
def django_phantom_html(request):
try:
# no need to talk to the workload app here
response_dict = {}
response_dict.update(csrf(request))
response_dict['user'] = request.user
print response_dict
t = loader.get_template('../templates/phantom.html')
c = Context(response_dict)
except PhantomRedirectException, ex:
return HttpResponseRedirect(ex.redir)
return HttpResponse(t.render(c))
#
# launch configuration options
#
@LogEntryDecorator
@login_required
def django_lc_html(request):
try:
# no need to talk to the workload app here
response_dict = {}
response_dict.update(csrf(request))
t = loader.get_template('../templates/launchconfig.html')
c = Context(response_dict)
except PhantomRedirectException, ex:
return HttpResponseRedirect(ex.redir)
return HttpResponse(t.render(c))
@LogEntryDecorator
@login_required
def django_lc_load(request):
user_obj = get_user_object(request.user.username)
try:
response_dict = phantom_lc_load(request.GET, user_obj)
h = HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
finally:
user_obj.close()
return h
@LogEntryDecorator
@login_required
def django_lc_delete(request):
user_obj = get_user_object(request.user.username)
try:
response_dict = phantom_lc_delete(request.POST, user_obj)
h = HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
finally:
user_obj.close()
return h
@LogEntryDecorator
@login_required
def django_lc_save(request):
user_obj = get_user_object(request.user.username)
try:
response_dict = phantom_lc_save(request.POST, user_obj)
h = HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
finally:
user_obj.close()
return h
#
# manage cloud functions
#
@LogEntryDecorator
@login_required
def django_profile_html(request):
response_dict = {}
response_dict.update(csrf(request))
response_dict['user'] = request.user
t = loader.get_template('../templates/profile.html')
c = Context(response_dict)
return HttpResponse(t.render(c))
@LogEntryDecorator
@login_required
def django_sites_load(request):
user_obj = get_user_object(request.user.username)
try:
response_dict = phantom_sites_load(request.GET, user_obj)
h = HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
finally:
user_obj.close()
return h
@LogEntryDecorator
@login_required
def django_sites_delete(request):
user_obj = get_user_object(request.user.username)
try:
response_dict = phantom_sites_delete(request.GET, user_obj)
h = HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
finally:
user_obj.close()
return h
@LogEntryDecorator
@login_required
def django_sites_add(request):
user_obj = get_user_object(request.user.username)
try:
response_dict = phantom_sites_add(request.REQUEST, user_obj)
h = HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')
finally:
user_obj.close()
return h
@LogEntryDecorator
@login_required
def django_change_password(request):
if request.is_ajax():
try:
user = User.objects.get(username=request.user.username)
except User.DoesNotExist:
return HttpResponse("USER_NOT_FOUND", status=500)
old_password = request.POST.get('old_password')
if not user.check_password(old_password):
return HttpResponse("BAD_OLD_PASSWORD", status=500)
new_password = request.POST.get('new_password')
new_password_confirmation = request.POST.get('new_password_confirmation')
if new_password != new_password_confirmation:
return HttpResponse("PASSWORDS_DO_NOT_MATCH", status=500)
if not new_password:
return HttpResponse("NEW_PASSWORD_IS_BLANK", status=500)
if not new_password_confirmation:
return HttpResponse("NEW_PASSWORD_CONFIRMATION_IS_BLANK", status=500)
user.set_password(new_password)
user.save()
return HttpResponse("{}", status=200)
else:
return HttpResponse(status=400)
class MyModelAdmin(admin.ModelAdmin):
def get_urls(self):
urls = super(MyModelAdmin, self).get_urls()
my_urls = patterns('',
(r'^my_view/$', self.admin_site.admin_view(self.my_view))
)
return my_urls + urls