0
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ In the future please make the title describe what the code does rather than your concerns about the code. Please read How do I ask a good question?. \$\endgroup\$
    – pacmaninbw
    Apr 17, 2021 at 12:54
  • \$\begingroup\$ @pacmaninbw Post array is asking the database for all the posts of the current user's friends. And people array is all the friend requests that are not accepted by the current user yet. English is not my main language so i don't know how can i write it more clear. Would you think in general it is safer to make operations in database rather than views? Or it doesn't matter? I couldn't find much information about this topic in google \$\endgroup\$ Apr 17, 2021 at 13:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.