Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge main into own branch #214

Merged
merged 5 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
72 changes: 29 additions & 43 deletions backend/nba_app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,18 @@ def post_detail(request, post_id):
print({'post': post, 'comments': comments})
return render(request, 'post_detail.html', {'post': post, 'comments': comments})
"""

"""
def post_detail(request, post_id):
post = Post.objects.get(post_id=post_id)
comments = Comment.objects.filter(post = post) #post.comments.all()
print({'post': post, 'image': post.image, 'comments': comments})
return render(request, 'post_detail.html', {'post': post, 'image': post.image, 'comments': comments})
"""
def post_detail(request, post_id):
post = Post.objects.get(post_id=post_id)
comments = post.comments.all()
print({'post': post, 'image': post.image.url, 'comments': comments})
return render(request, 'post_detail.html', {'post': post, 'image': post.image.url, 'comments': comments})


def user_followings(request):
Expand All @@ -118,7 +124,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 +160,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