From 094d91123d937778e2a6885241ba5939b268de01 Mon Sep 17 00:00:00 2001 From: Fatih Demir <101219059+fatih260@users.noreply.github.com> Date: Sun, 12 May 2024 12:23:57 +0300 Subject: [PATCH] profile_view_of_other_users --- backend/nba_app/urls.py | 2 +- backend/nba_app/views.py | 29 +++++++++++++++++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/backend/nba_app/urls.py b/backend/nba_app/urls.py index 02807dcf..022e7da1 100644 --- a/backend/nba_app/urls.py +++ b/backend/nba_app/urls.py @@ -9,12 +9,12 @@ path('signup/', views.sign_up, name='signup'), path('login/', views.log_in, name='login'), path('feed/', views.feed, name='feed'), - path('profile_view_edit/', views.profile_view_edit, name='profile_view_edit'), path('user_followings/', views.user_followings, name='user_followings'), path('user_followers/', views.user_followers, name='user_followers'), path('follow_user/', views.follow_user, name='follow_user'), path('unfollow_user/', views.unfollow_user, name='unfollow_user'), path('profile_view_edit/', views.profile_view_edit, name='profile_view_edit'), + path('profile_view_of_other_users/', views.profile_view_of_other_users, name='profile_view_of_other_users'), path('reset_password/', views.reset_password, name='reset_password'), path('post/', views.post, name='post'), path('search/', views.search, name='search'), diff --git a/backend/nba_app/views.py b/backend/nba_app/views.py index a91d0dcf..ec06871a 100644 --- a/backend/nba_app/views.py +++ b/backend/nba_app/views.py @@ -159,7 +159,6 @@ def profile_view_edit(request): following_count = user.following.count() followers_count = user.followers.count() posts = Post.objects.filter(user=user) - #post_contents = [post.content for post in posts] data = { 'username': user.username, 'email': user.email, @@ -169,11 +168,33 @@ def profile_view_edit(request): 'following_count': following_count, 'followers_count': followers_count, 'profile_picture': user.profile_picture.url if user.profile_picture else None, - 'posts': [{'content': post.content, 'created_at': post.created_at} for post in posts] + 'posts': [{'content': post.content, 'created_at': post.created_at, 'image':post.image} for post in posts] } - return render(request, 'profile_view_edit.html', data) - #return JsonResponse(data, status=200) + return JsonResponse(data, status=200) + #return render(request, 'profile_view_edit.html', data) +def profile_view_of_other_users(request, user_id): + try: + user = User.objects.get(pk=user_id) + except User.DoesNotExist: + return JsonResponse({'error': 'User not found.'}, status=404) + + following_count = user.following.count() + followers_count = user.followers.count() + posts = Post.objects.filter(user=user) + + is_following = Follow.objects.filter(follower=request.user, followed=user).exists() + + data = { + 'username': user.username, + 'bio': user.bio, + 'profile_picture': user.profile_picture.url if user.profile_picture else None, + 'following_count': following_count, + 'followers_count': followers_count, + 'is_following': is_following, + 'posts': [{'content': post.content, 'created_at': post.created_at, 'image':post.image} for post in posts] + } + return JsonResponse(data, status=200) def reset_password(request): if request.method != 'POST':