So i created a social media website in django and here is how i managed accepting friend requests and showing friends posts in home page.
Would it be better to do this operation in models.py file, create a function and call it in views using django forms? My concern is security
def home(request):
if request.user.is_authenticated:
people = []
posts = []
for eachs in request.user.friend_requests.all():
person_prof = User.objects.get(id=eachs.current_id)
people.append({'id':person_prof.id, 'profile':person_prof.profile, 'picture':person_prof.profile.picture.url, 'isfriend':False, 'sent':False, 'rec':True})
for friend in request.user.friends.all():
user = User.objects.get(id=friend.friend_id)
for post in user.post.all():
posts.append({'body':post.body, 'image':post.image.url, 'created':post.created, 'fr_name':user.profile.wholename(),'user_id':user.id, 'pr_pic':user.profile.picture.url,'post_id':post.id, 'comments':[ {'comment':comment.comment, 'user':comment.user} for comment in post.comments.all()]})
return render(request, 'newsfeed.html',{'people':people,'posts':posts})
else:
return render(request, 'landing_page.html',{})
Here the post = [] array is asking the database for all the posts of current user's friends. And the people = [] array is all the friend requests that are not accepted by the current user yet.