-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
345 lines (299 loc) · 11.4 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# Create your views here.
from django.shortcuts import render
from discoverify.models import *
from django.views.decorators.csrf import csrf_protect
from django.http import HttpResponse
from django.http import HttpResponseRedirect
import string
from random import randint
from django.template import RequestContext
from django.shortcuts import render_to_response
import datetime
from django.contrib.auth.models import User
from django.contrib.admin.views.decorators import staff_member_required
from django.utils import simplejson
from django.contrib import auth
from django.contrib.auth import authenticate, login
from django.contrib.auth import logout
def logout_view(request):
logout(request)
return HttpResponseRedirect('http://bits-oasis.org/2014/discoverify/')
def home_view(request):
context = RequestContext(request)
context_dict = {}
return render_to_response('index.html', context_dict, context)
def create_dummy_view(request):
context = RequestContext(request)
context_dict = {}
return render_to_response('createPathWizard.html', context_dict, context)
def path_details_view(request,path_id):
selected_path = Path.objects.get(id=int(path_id))
all_steps = selected_path.steps.all()
no_of_steps = len(all_steps)
context = RequestContext(request)
context_dict = {'selected_path':selected_path,'no_of_steps':no_of_steps,'all_steps':all_steps}
return render_to_response('learnPage.html', context_dict, context)
def edit_path_view(request,path_id):
if not request.user.is_authenticated():
return HttpResponse('Forbidden')
selected_path = Path.objects.get(id=int(path_id))
if request.user != selected_path.author:
return HttpResponse("You don't own this learning path")
tags = ''.join([x.name for x in selected_path.tag.all()])
all_steps = selected_path.steps.all()
context = RequestContext(request)
context_dict = {'selected_path':selected_path,'tags':tags,'all_steps':all_steps}
return render_to_response('editPage.html', context_dict, context)
def step_details_view(request,step_id=0):
if request.POST:
step_id=request.POST['step_id']
selected_step = Step.objects.get(id=int(step_id))
context = RequestContext(request)
context_dict = {'selected_step':selected_step}
return render_to_response('stepage.html', context_dict, context)
def add_step_end(request):
if request.POST:
if not request.user.is_authenticated():
return HttpResponse('Forbidden')
path_id=request.POST['path_id']
selected_path = Path.objects.get(id=int(path_id))
if request.user != selected_path.author:
return HttpResponse("You don't own this learning path")
new_step_number = len(selected_path.steps.all())+1
####### Not adding exception handling now
short_desc = request.POST['short_description']
long_desc = request.POST['long_description']
resources = 'more placeholder stuff'
#add forums stuff
new_step = Step(number=new_step_number,short_description=short_desc,long_description=long_desc,resources=resources)
new_step.save()
for x in range(1,5):
if not request.POST['url_link'+str(x)]:
continue
l = Link(name='placeholder',url_link=request.POST['url_link'+str(x)],linl_type=request.POST['category'+str(x)])
l.save()
new_step.links.add(l)
new_step.save()
selected_path.steps.add(new_step)
return HttpResponse('{"message":"sucess","status":"1","step_no":%s,"step_id":%s,"step_desc":"%s"}' %(new_step.number,new_step.id,new_step.short_description))
def create_path(request):
if request.POST:
if not request.user.is_authenticated():
return HttpResponse('Forbidden')
try:
name = request.POST['name']
short_description = request.POST['short_description']
img = request.FILES['img']
except:
return HttpResponse('Incomplete Data Sent')
author = request.user
online = False
no_registrations = 0
upvotes = 0
#create forum stuff
time = "0"
new_path = Path(name=name,short_description=short_description,author=author,online=online,img=img,no_registrations=no_registrations,upvotes=upvotes,time=time)
new_path.save()
path_id = new_path.id
return HttpResponseRedirect('/2014/discoverify/editpath/%s' % path_id)
# Create your views here.
def update_path(request):
if request.POST:
if not request.user.is_authenticated():
return HttpResponse('Forbidden')
path_id=request.POST['path_id']
selected_path = Path.objects.get(id=int(path_id))
if request.user != selected_path.author:
return HttpResponse("You don't own this learning path")
if 'long_description' in request.POST:
selected_path.long_description = request.POST['long_description']
selected_path.save()
elif 'prereq' in request.POST:
selected_path.prereq = request.POST['prereq']
selected_path.save()
elif 'outcomes' in request.POST:
selected_path.outcomes = request.POST['outcomes']
selected_path.save()
elif 'short_description' in request.POST:
selected_path.short_description = request.POST['short_description']
selected_path.save()
elif 'faq' in request.POST:
selected_path.faq = request.POST['faq']
selected_path.save()
# elif img in request.POST:
# selected_path.img = request.POST['img']
elif 'tags' in request.POST:
tags_cs = request.POST['tags']
tags = tags_cs.split(',')
all_old_tags = [x.name for x in Tag.objects.all()]
for x in tags:
if x in all_old_tags:
old_matching_tag = Tag.objects.get(name=x)
selected_path.tag.add(old_matching_tag)
else:
t = Tag(name=x)
t.save()
all_old_tags.append(x)
selected_path.tag.add(t)
selected_path.save()
return HttpResponse('{"message":"sucess","status":"1"}')
def update_step(request,step_id):
selected_step = Step.objects.get(id=int(step_id))
if request.POST:
if not request.user.is_authenticated():
return HttpResponse('Forbidden')
path_id=request.POST['path_id']
selected_path = Path.objects.get(id=int(path_id))
if request.user != selected_path.author:
return HttpResponse("You don't own this learning path")
####### Not adding exception handling now
short_desc = request.POST['short_description']
long_desc = request.POST['long_description']
resources = 'more placeholder stuff'
selected_step.short_description = short_desc
selected_step.long_description = long_desc
selected_step.save()
all_links = [x for x in selected_step.links.all()]
for link in all_links:
selected_step.links.remove(link)
selected_step.save()
for x in range(1,5):
if not request.POST['url_link'+str(x)]:
continue
l = Link(name='placeholder',url_link=request.POST['url_link'+str(x)],linl_type=request.POST['category'+str(x)])
l.save()
selected_step.links.add(l)
selected_step.save()
selected_step.save()
return HttpResponse('{"message":"sucess","status":"1"}')
else:
resp = {}
resp['num'] = str(selected_step.number)
resp['short_description'] = str(selected_step.short_description)
resp['long_description'] = str(selected_step.long_description)
resp['links'] = []
for link in selected_step.links.all():
t = {}
t['id'] = str(link.id)
t['name'] = str(link.name)
t['linl_type'] = str(link.linl_type)
t['url_link'] = str(link.url_link)
resp['links'].append(t)
json = simplejson.dumps(resp)
return HttpResponse(json, mimetype='application/json')
def remove_step(request):
if request.POST:
if not request.user.is_authenticated():
return HttpResponse('Forbidden')
step_id=request.POST['step_id']
path_id= request.POST['path_id']
selected_step = Step.objects.get(id=int(step_id))
selected_path = Path.objects.get(id=int(path_id))
if request.user != selected_path.author:
return HttpResponse("You don't own this learning path")
#Now check if the step to be remmoved is the last tep
#Dirty hack again :/
if selected_step.number == max([x.number for x in selected_path.steps.all()]):
selected_path.steps.remove(selected_step)
return HttpResponse('{"status":"1","message":"sucess"}')
else:
all_steps_with_higer_number = [x for x in selected_path.steps.all() if x.number>selected_step.number]
selected_path.steps.remove(selected_step)
for x in all_steps_with_higer_number:
x.number -= 1
x.save()
return HttpResponse('{"status":"1","message":"sucess"}')
def get_steps(request):
if request.POST:
path_id=request.POST['path_id']
selected_path = Path.objects.get(id=int(path_id))
resp={}
resp['Steps']=[]
all_steps = selected_path.steps.all()
for step in all_steps:
temp = {}
temp['id'] = step.id
temp['number'] = step.number
temp['short_description'] = step.short_description
temp['long_description'] = step.long_description
temp['resources'] = step.resources
# temp['links'] = []
# for link in step.links.all():
# t = {}
# t['id'] = link.id
# t['name'] = link.name
# t['linl_type'] = link.linl_type
# t['url_link'] = link.url_link
# temp['links'].append(t)
resp['Events'].append(temp)
json = simplejson.dumps(resp)
return HttpResponse(json, mimetype='application/json')
def InitialRegistrationView(request):
# Registration details
if request.POST:
# return HttpResponse(request.POST['name'])
try:
name = request.POST['name']
password = request.POST['password']
email = request.POST['email']
# bio = request.POST['bio']
# website = int(request.POST['website'])
except:
return HttpResponse('An error occured')
member = UserInfo()
member.name = name
member.email = email
member.bio = ''
member.website = ''
#add school related check
registered_members = UserInfo.objects.all()
list_of_registered_emails = [x.email for x in registered_members]
if email in list_of_registered_emails: #check for already registered emails....no need to check if valid as we are using email field on fronted side
status = '{ "status" : 0 , "message" : "This email/phone is already registered! Please Login!" }'
return HttpResponse(status)
#now create the user account
u = User(username=email, email = email)
u.save()
user_pass = password
u.set_password(user_pass)
u.save()
#now assign user
member.user = u
member.save()
status = '{ "status" : 1 , "message" : "Successfully Registered !", "participant_username": participant_username}'
return HttpResponseRedirect('http://bits-oasis.org/hackiton/')
else:
return HttpResponseRedirect('http://bits-oasis.org/hackiton/register.html')
def user_login(request):
context = RequestContext(request)
if request.method == 'POST':
username = request.POST['email']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user:
if user.is_active:
login(request, user)
# name = user.participant_set.all()[0].name
# status = '{"status":"1","message":"login Successfull", "name": "%s"}' % name
# if request.user.is_authenticated():
# return HttpResponse('login ho ra h re')
# request.session['testing'] = request.user
return HttpResponseRedirect('/2014/discoverify/profile/')
else:
status = '{"status":"0","message":"Your account is frozen and edits cannot be made now."}'
return HttpResponse(status)
else:
# print "Invalid login details"
status = '{"status":"0","message":"Invalid Login Details"}'
return HttpResponse(status)
else:
return HttpResponse('Why you do this?')
def profile(request):
if request.user.is_authenticated():
profile = request.user.userinfo_set.all()[0]
all_courses = Path.objects.filter(author=request.user)
context = RequestContext(request)
context_dict = {'all_courses':all_courses,'profile':profile}
return render_to_response('prof.html', context_dict, context)
else:
return HttpResponseRedirect('http://bits-oasis.org/hackiton/login.html')