Skip to content

Commit

Permalink
Merge pull request #463 from bounswe/backend/feature/bookmark-a-post-…
Browse files Browse the repository at this point in the history
…#462

[Backend] Bookmark a Post implementation
  • Loading branch information
canberkboun9 authored Dec 23, 2022
2 parents 61cc4f2 + 6a21fdb commit 27a1581
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 2 deletions.
19 changes: 19 additions & 0 deletions app/backend/backend/migrations/0007_customuser_bookmarked_posts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.1.2 on 2022-12-22 17:33

import django.contrib.postgres.fields
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('backend', '0006_alter_customuser_downvoted_articles_and_more'),
]

operations = [
migrations.AddField(
model_name='customuser',
name='bookmarked_posts',
field=django.contrib.postgres.fields.ArrayField(base_field=models.IntegerField(null=True), default=list, null=True, size=None),
),
]
1 change: 1 addition & 0 deletions app/backend/backend/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class CustomUser(AbstractUser):
upvoted_articles = ArrayField(models.IntegerField(null=True), null=True, default=list)
downvoted_articles = ArrayField(models.IntegerField(null=True), null=True, default=list)
followed_categories = ArrayField(models.IntegerField(null=True), null=True, default=list)
bookmarked_posts = ArrayField(models.IntegerField(null=True), null=True, default=list)

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
Expand Down
22 changes: 22 additions & 0 deletions app/backend/forum/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,26 @@ def test_downvote_comment_when_already_exist(self):
**{"HTTP_AUTHORIZATION": f"Token {token.key}"})
response2 = client.post(f'/forum/post/comment/{comment.id}/downvote', content_type="application/json",
**{"HTTP_AUTHORIZATION": f"Token {token.key}"})
self.assertEqual(response2.status_code, 200)

def test_bookmark_post(self):
client = Client()
user = backendModels.CustomUser.objects.create_user(email="joedoetest@gmail.com", password="testpassword",
type=2)
token = Token.objects.create(user=user)
post = models.Post.objects.create(title='test post title', body='test post body', author=user,
date=datetime.now())
response = client.post(f'/forum/post/{post.id}/bookmark', content_type="application/json",
**{"HTTP_AUTHORIZATION": f"Token {token.key}"})
self.assertEqual(response.status_code, 200)

def test_bookmark_remove_post(self):
client = Client()
user = backendModels.CustomUser.objects.create_user(email="joedoetest@gmail.com", password="testpassword",
type=2)
token = Token.objects.create(user=user)
post = models.Post.objects.create(title='test post title', body='test post body', author=user, date=datetime.now())
response1 = client.post(f'/forum/post/{post.id}/bookmark', content_type="application/json", **{"HTTP_AUTHORIZATION": f"Token {token.key}"})
response2 = client.post(f'/forum/post/{post.id}/bookmark', content_type="application/json",
**{"HTTP_AUTHORIZATION": f"Token {token.key}"})
self.assertEqual(response2.status_code, 200)
1 change: 1 addition & 0 deletions app/backend/forum/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
path('post/comment/<int:id>', views.get_comment, name='get comment'),
path('post/<int:id>/comment', views.create_comment, name='create comment'),
path('categories', views.get_all_categories, name='get_all_categories'),
path('post/<int:id>/bookmark', views.bookmark_post, name='bookmark a post'),
]
39 changes: 38 additions & 1 deletion app/backend/forum/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,15 @@ def get_all_posts(request):
serializer_post_data['vote'] = 'downvote'
else:
serializer_post_data['vote'] = None

if post.id in user.bookmarked_posts:
serializer_post_data['bookmark'] = True
else:
serializer_post_data['bookmark'] = False
else:
serializer_post_data['vote'] = None
serializer_post_data['bookmark'] = None

author = post.author
if author.type == 1:
try:
Expand Down Expand Up @@ -168,6 +175,12 @@ def get_posts_of_user(request, user_id):
post_dict['vote'] = 'downvote'
else:
post_dict['vote'] = None

if post.id in request.user.bookmarked_posts:
post_dict['bookmark'] = True
else:
post_dict['bookmark'] = False

response_dict.append(post_dict)

result_page = paginator.paginate_queryset(response_dict, request)
Expand Down Expand Up @@ -314,8 +327,13 @@ def get_post(request,id):
response_dict['vote'] = 'downvote'
else:
response_dict['vote'] = None
if post.id in request.user.bookmarked_posts:
response_dict['bookmark'] = True
else:
response_dict['bookmark'] = False
else:
response_dict['vote'] = None
response_dict['bookmark'] = None



Expand Down Expand Up @@ -695,4 +713,23 @@ def create_comment(request, id):
def get_all_categories(request):
queryset = Category.objects.all()
serializer = CategorySerializer(queryset, many=True)
return Response(serializer.data)
return Response(serializer.data)


@api_view(['POST',])
@permission_classes([IsAuthenticated, ])
def bookmark_post(request, id):
try:
post = Post.objects.get(id=id)
user_info = request.user
except:
return Response({'error': 'Post not found'}, status=400)

if id in user_info.bookmarked_posts :
user_info.bookmarked_posts.remove(id)
user_info.save()
return Response({'response': 'Bookmark removed successfully'}, status=200)
else:
user_info.bookmarked_posts.append(id)
user_info.save()
return Response({'response': 'Bookmark added successfully'}, status=200)
12 changes: 12 additions & 0 deletions app/backend/user_profile/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,16 @@ def test_get_followed_categories(self):
**{"HTTP_AUTHORIZATION": f"Token {token.key}"})
response2 = client.get(f'/profile/followed_categories', content_type="application/json",
**{"HTTP_AUTHORIZATION": f"Token {token.key}"})
self.assertEqual(response2.status_code, 200)
def test_get_bookmarked_posts(self):
client = Client()
user = backendModels.CustomUser.objects.create_user(email="joedoetest@gmail.com", password="testpassword",
type=2, date_of_birth=datetime.now())
token = Token.objects.create(user=user)
post = models.Post.objects.create(title='test post title', body='test post body', author=user, date=datetime.now())

response1 = client.post(f'/forum/post/{post.id}/bookmark', content_type="application/json",
**{"HTTP_AUTHORIZATION": f"Token {token.key}"})
response2 = client.get(f'/profile/bookmarked_posts', content_type="application/json",
**{"HTTP_AUTHORIZATION": f"Token {token.key}"})
self.assertEqual(response2.status_code, 200)
1 change: 1 addition & 0 deletions app/backend/user_profile/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
path('get_doctor_profile/<int:id>', views.get_doctor_profile, name='get_doctor_profile'),
path('follow_category/<int:id>', views.follow_category, name='follow_category'),
path('followed_categories', views.get_followed_categories, name='get_followed_categories'),
path('bookmarked_posts', views.get_bookmarked_posts, name='get_bookmarked_posts'),
]
62 changes: 61 additions & 1 deletion app/backend/user_profile/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,64 @@ def get_followed_categories(request):
return Response({'error': 'User not found'}, status=400)
for id in user_info.followed_categories :
response.append(id)
return Response(response, status=200)
return Response(response, status=200)

@api_view(['GET',])
@permission_classes([IsAuthenticated,])
def get_bookmarked_posts(request):
paginator = PageNumberPagination()
paginator.max_page_size = 10
paginator.page_size = request.GET.get('page_size', 10)
paginator.page = request.GET.get('page', 1)
user = request.user
bookmarked_posts = user.bookmarked_posts

posts = Post.objects.filter(id__in=bookmarked_posts)



temp = request.GET.get('sort', 'desc')
if temp == 'desc' or temp == 'asc':
sort = temp
else:
sort = 'desc'

if sort == 'asc':
posts = posts.order_by('date')
elif sort == 'desc':
posts = posts.order_by('-date')

post_response = []
for post in posts:
serializer_data = PostSerializer(post).data
serializer_data['bookmark'] = True
author = post.author
if author.type == 1:
try:
doctor_data = Doctor.objects.get(user=author)
author_data = {
'id': author.id,
'username': doctor_data.full_name,
'profile_photo': doctor_data.profile_picture,
'is_doctor': True
}
except:
author_data = None

elif author.type == 2:
try:
member_data = Member.objects.get(user=author)
author_data = {
'id': author.id,
'username': member_data.member_username,
'profile_photo': f"https://api.multiavatar.com/{member_data.info.avatar}.svg?apikey={os.getenv('AVATAR')}",
'is_doctor': False
}
except:
author_data = None
serializer_data["author"] = author_data
post_response.append(serializer_data)

result_page = paginator.paginate_queryset(post_response, request)

return paginator.get_paginated_response(result_page)

0 comments on commit 27a1581

Please sign in to comment.