Skip to content

Commit

Permalink
Merge pull request #209 from bounswe/fatih/follow-unfollow
Browse files Browse the repository at this point in the history
Fatih/follow unfollow
  • Loading branch information
fatih260 authored May 12, 2024
2 parents aedd3a5 + 7986f31 commit 0228f15
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 44 deletions.
3 changes: 1 addition & 2 deletions backend/nba_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
path('user_followers/', views.user_followers, name='user_followers'),
path('follow_user/<int:user_id>', views.follow_user, name='follow_user'),
path('unfollow_user/<int:user_id>', views.unfollow_user, name='unfollow_user'),
path('profile_view_edit/', views.profile_view_edit, name='profile_view_edit'),
path('profile_view_of_other_users/<int:user_id>', views.profile_view_of_other_users, name='profile_view_of_other_users'),
path('profile_view_edit/<int:user_id>', views.profile_view_edit, name='profile_view_edit'),
path('reset_password/', views.reset_password, name='reset_password'),
path('post/', views.post, name='post'),
path('search/', views.search, name='search'),
Expand Down
64 changes: 22 additions & 42 deletions backend/nba_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def user_followers(request):
return JsonResponse({'followers_info': followers_info}, status=200)


def profile_view_edit(request):
def profile_view_edit(request, user_id):
if request.method == 'POST':
new_username = request.POST.get('username')
new_email = request.POST.get('email')
Expand Down Expand Up @@ -154,47 +154,27 @@ def profile_view_edit(request):

return JsonResponse({'message': 'Account information updated successfully.'}, status=200)

# if request.method == 'GET':
user = request.user
following_count = user.following.count()
followers_count = user.followers.count()
posts = Post.objects.filter(user=user)
data = {
'username': user.username,
'email': user.email,
'bio': user.bio,
'profile_picture': user.profile_picture.url if user.profile_picture else None,
# Add any other fields you want to include in the response
'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, 'image':post.image} for post in posts]
}
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)
if request.method == 'GET':
user = User.objects.get(user_id=user_id)
following_count = user.following.count()
followers_count = user.followers.count()
posts = Post.objects.filter(user=user)
if request.user == user:
is_following = None
else:
is_following = Follow.objects.filter(follower=request.user, followed=user).exists()
data = {
'username': user.username,
'email': user.email,
'bio': user.bio,
'following_count': following_count,
'followers_count': followers_count,
'profile_picture': user.profile_picture.url if user.profile_picture else None,
'is_following': is_following, # True if the authenticated user is following the user, False otherwise, None if the authenticated user is the user
'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':
Expand Down

0 comments on commit 0228f15

Please sign in to comment.