-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
457 lines (260 loc) · 11.5 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
from django.shortcuts import render, redirect
from forms import SignUpForm, LoginForm, PostForm, LikeForm, CommentForm, UpvoteForm
from models import UserModel, SessionToken, PostModel, LikeModel, CommentModel, UpvoteModel
from django.contrib.auth.hashers import make_password, check_password
from datetime import timedelta
from django.utils import timezone
from mysite.settings import BASE_DIR
from keys import CLIENT_SECRET, CLIENT_ID, CLARIFI_API_KEY, SENDGRID_API_KEY, FROM_EMAIL,CLOUDINARY_API_KEY, CLOUDINARY_API_SECRET, CLOUDINARY_ENVIRONMENT_VARIABLE
import sendgrid
from sendgrid.helpers.mail import *
import cloudinary
import cloudinary.uploader
import cloudinary.api
from imgurpython import ImgurClient
from clarifai.rest import ClarifaiApp
from django.contrib import messages
cloudinary.config(
cloud_name = "mycloud",
api_key = CLOUDINARY_API_KEY,
api_secret = CLOUDINARY_API_SECRET
)
# views here.
# Sign up
def signup_view(request):
# Storing data for sendgrid api
user_data={}
if request.method == "POST":
form = SignUpForm(request.POST)
if form.is_valid():
# Getting the form data
username = form.cleaned_data['username']
name = form.cleaned_data['name']
email = form.cleaned_data['email']
password = form.cleaned_data['password']
# Validating the data
if len(username)>3 and len(password)>4 and len(name)>0 and len(email)>0:
# saving data to DB
user = UserModel(name=name, password=make_password(password), email=email, username=username)
user.save()
user_data['to_email'] = email
user_data['subject'] = 'Smart P2P MarketPlace'
user_data['content'] = 'Signup sucessful on Smart P2P Marketplace'
# Sending mail to the new user
sendmail(user_data)
# redirecting the user
return render(request, 'success.html')
# return redirect('login/')
else:
# Using built in messages view to display alerts
messages.error(request, 'Username should have min 4 characters and password should have 5. No field should be empty')
else:
form = SignUpForm()
return render(request, 'index.html', {'form': form})
# Login view
def login_view(request):
response_data = {}
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
# Finding user in dB
user = UserModel.objects.filter(username=username).first()
if user:
# Authenticating user
if check_password(password, user.password):
# Preparing session token
token = SessionToken(user=user)
token.create_token()
token.save()
response = redirect('feed/')
# Setting cookies for easier experience
response.set_cookie(key='session_token', value=token.session_token)
messages.info(request, "LogIn successful! Enjoy!")
return response
else:
messages.error(request, "Invalid credentials! Try again!")
elif request.method == 'GET':
form = LoginForm()
response_data['form'] = form
return render(request, 'login.html', response_data)
# View for uploading posts
def post_view(request):
# Checking if the user is logged in
user = check_validation(request)
if user:
if request.method == 'POST':
form = PostForm(request.POST, request.FILES)
if form.is_valid():
image = form.cleaned_data.get('image')
caption = form.cleaned_data.get('caption')
# Saving the post
post = PostModel(user=user, image=image, caption=caption)
post.save()
# Preparing path for saving the image
path = str(BASE_DIR +'/'+ post.image.url)
#client = cloudinary.uploader.upload(path)
#post.image_url = client['url']
client = ImgurClient(CLIENT_ID, CLIENT_SECRET)
# Using imgur to get image url for easier access
post.image_url = client.upload_from_path(path, anon=True)['link']
post.save()
# Using Clarifai to categorise the image
app = ClarifaiApp(api_key=CLARIFI_API_KEY)
model = app.models.get('general-v1.3')
result = model.predict_by_url(post.image_url)
post.category = result['outputs'][0]['data']['concepts'][0]['name']
# Saving the post details
post.save()
messages.info(request, "Post Successful!")
return redirect('/feed/')
else:
form = PostForm()
messages.error(request, 'Post unsuccessful! Try again!')
return render(request, 'post.html', {'form': form})
else:
return redirect('/login/')
# Setting the feed
def feed_view(request):
user = check_validation(request)
if user:
# Sorting posts based on creation date
posts = PostModel.objects.all().order_by('created_on')
# Checking likes, comments and upvotes on the posts
for post in posts:
existing_like = LikeModel.objects.filter(post_id=post.id, user=user).first()
for comment in post.comments:
existing_upvote = UpvoteModel.objects.filter(comment=comment.id, user=user).first()
if existing_upvote:
comment.has_upvoted = True
if existing_like:
post.has_liked = True
# Rendering the posts to feed template
return render(request, 'feed.html', {'posts': posts})
else:
return redirect('/login/')
# Like controller
def like_view(request):
user = check_validation(request)
# Saving data for sendgrid
user_data={}
if user and request.method == 'POST':
form = LikeForm(request.POST)
if form.is_valid():
post_id = form.cleaned_data.get('post')
user_data['to_email'] = form.cleaned_data.get('post').user.email
# Checking if already liked
existing_like = LikeModel.objects.filter(post_id = post_id, user=user).first()
# If not then liked
if not existing_like:
LikeModel.objects.create(post=post_id, user=user)
user_data['subject'] = 'Liked'
user_data['content'] = 'Your post was liked by ' + form.cleaned_data.get('post').user.name
sendmail(user_data)
# Otherwise unliked
else:
existing_like.delete()
user_data['subject'] = 'Unliked'
user_data['content'] = 'Your post was unliked by ' + form.cleaned_data.get('post').user.name
sendmail(user_data)
return redirect('/feed/')
else:
return redirect('/login/')
# Comment controller
def comment_view(request):
# You should know this by now
user_data = {}
user = check_validation(request)
if user and request.method == 'POST':
form = CommentForm(request.POST)
if form.is_valid():
post_id = form.cleaned_data.get('post').id
user_data['to_email'] = form.cleaned_data.get('post').user.email
user_data['subject'] = 'Commented'
user_data['content'] = 'Your post was commented by ' + form.cleaned_data.get('post').user.name
#Getting comment form data
comment_text = form.cleaned_data.get('comment_text')
# Saving comment in dB
comment = CommentModel.objects.create(user=user, post_id=post_id, comment_text=comment_text)
comment.save()
sendmail(user_data)
messages.info(request, 'Your comment has been posted!')
return redirect('/feed/')
else:
return redirect('/feed/')
else:
return redirect('/login')
# Upvote controller (as in reddit )
def upvote_view(request):
user = check_validation(request)
user_data = {}
if user and request.method == 'POST':
form = UpvoteForm(request.POST)
if form.is_valid():
comment_id = form.cleaned_data.get('comment')
user_data['to_email'] = form.cleaned_data.get('comment').user.email
# Checking upvotes on comments:>
existing_upvote = UpvoteModel.objects.filter(comment_id=comment_id, user=user).first()
# If not then upvote
if not existing_upvote:
UpvoteModel.objects.create(comment=comment_id, user=user)
user_data['subject'] = 'Upvoted'
user_data['content'] = 'Your comment was upvoted by ' + form.cleaned_data.get('comment').user.name
sendmail(user_data)
# Otherwise downvote(opposie of upvoting :<
else:
existing_upvote.delete()
user_data['subject'] = 'Downvoted'
user_data['content'] = 'Your comment was downvoted by ' + form.cleaned_data.get('comment').user.name
sendmail(user_data)
return redirect('/feed/')
else:
return redirect('/login/')
# For validating the session
def check_validation(request):
if request.COOKIES.get('session_token'):
# Getting related session token from dB
session = SessionToken.objects.filter(session_token=request.COOKIES.get('session_token')).first()
# Checking the expiration date
if session:
time_to_live = session.created_on + timedelta(days=1)
if time_to_live > timezone.now():
return session.user
else:
return None
#To logout a user
def logout_view(request):
user = check_validation(request)
if user:
# Flushing the session from the database:)
SessionToken.objects.filter(session_token=request.COOKIES.get('session_token')).first().delete()
return redirect('/login/')
else:
return redirect('/login/')
# Sending mails to everyone
def sendmail(user_data):
sg = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY)
from_email = Email(FROM_EMAIL)
to_email = Email(user_data['to_email'])
subject = user_data['subject']
content = Content("text/plain", user_data['content'])
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())
# Url query
def query_view(request):
user = check_validation(request)
uid = int(request.GET.get('uid', user.id))
if user:
posts = PostModel.objects.filter(user=uid).order_by('created_on')
for post in posts:
existing_like = LikeModel.objects.filter(post_id=post.id, user=user).first()
for comment in post.comments:
existing_upvote = UpvoteModel.objects.filter(comment_id=comment.id, user=user).first()
if existing_upvote:
comment.has_upvoted = True
if existing_like:
post.has_liked = True
return render(request, 'feed.html', {'posts': posts})
else:
return redirect('/login/')