Skip to content

Commit

Permalink
166044739-bg(likes and favorites): fix likes and favorites
Browse files Browse the repository at this point in the history
- added two serializer methods

[feature #166044739]
  • Loading branch information
Oluwagbenga Joloko authored and Abraham Kamau committed May 16, 2019
2 parents b52906b + ce4e766 commit 134d880
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 39 deletions.
2 changes: 2 additions & 0 deletions authors/apps/appnotifications/tests/basetest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class NotificationBaseTest(BaseTest):
notification_url = reverse("notifications:all-notifications")
unread_notification_url = reverse("notifications:unread-notifications")
subscribe_unsubscribe_url = reverse("notifications:subscription")
delete_single_url = reverse("notifications:deleteone", args=[2])
delete_single_invalid_id = reverse("notifications:deleteone", args=[56])

def follow_user(self):
self.is_authenticated("jim@gmail.com", "@Us3r.com")
Expand Down
24 changes: 24 additions & 0 deletions authors/apps/appnotifications/tests/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,27 @@ def test_delete_unfound_notifications(self):
response = self.client.delete(self.notification_url)
self.assertEqual(response.data['message'], 'No notifications found')
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_delete_one_notifications_successfully(self):
"""
Test deletion of a single notification
"""
self.follow_user()
self.create_article()
self.is_authenticated("jim@gmail.com", "@Us3r.com")
response = self.client.delete(self.delete_single_url)
self.assertEqual(response.data['message'],
'Notification deleted successfully')
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_delete_one_notifications_unsuccessfully(self):
"""
Test deletion of a single notification provided invalid id
"""
self.follow_user()
self.create_article()
self.is_authenticated("jim@gmail.com", "@Us3r.com")
response = self.client.delete(self.delete_single_invalid_id)
self.assertEqual(response.data['message'],
'No notification found with that id')
self.assertEqual(response.status_code, status.HTTP_200_OK)
11 changes: 8 additions & 3 deletions authors/apps/appnotifications/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@
'notifications/',
views.AllNotificationsAPIview.as_view(),
name="all-notifications"
),
),
path(
'notifications/unread/',
views.UnreadNotificationsAPIview.as_view(),
name="unread-notifications"
),
),
path(
'notifications/subscription/',
views.SubscribeUnsubscribeAPIView.as_view(),
name="subscription"
),
),
path(
'notifications/unsubscribe_email/<str:token>/',
views.UnsubscribeEmailAPIView.as_view(),
name="opt_out_link"
),
path(
'notifications/<id>/',
views.DeleteSingleNotificationAPIView.as_view(),
name="deleteone"
)
]
20 changes: 20 additions & 0 deletions authors/apps/appnotifications/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,23 @@ class UnreadNotificationsAPIview(NotificationApiView):
def notifications(self, request):
request.user.notifications.unread()
return request.user.notifications.active()


class DeleteSingleNotificationAPIView(NotificationApiView):
"""
delete a single notification
"""

def delete(self, request, *args, **kwargs):
try:
notif = request.user.notifications.active().get(id=kwargs['id'])
notif.deleted = True
notif.save()
resp = {
'message': 'Notification deleted successfully'
}
except Exception:
resp = {
'message': 'No notification found with that id'
}
return Response(resp)
30 changes: 22 additions & 8 deletions authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,34 @@ def get_ratings(self, obj):
ratings = RatingModel().ratings(article.id, request.user.id)
return ratings

like = serializers.SerializerMethodField()
dislike = serializers.SerializerMethodField()
favorite = serializers.SerializerMethodField()
likesCount = serializers.ReadOnlyField(source='num_vote_up')
dislikesCount = serializers.ReadOnlyField(source='num_vote_down')
def get_like_info(self, obj):
mapped_data = {
"like": self.get_like(self),
"dislike": self.get_dislike(self),
"likeCount": obj.num_vote_up,
"dislikeCount": obj.num_vote_down
}

return mapped_data

def get_favorites(self, obj):
mapped_data = {
"favorite": self.get_favorite(self),
"favoritesCount": obj.favoritesCount,
}

return mapped_data

like_info = serializers.SerializerMethodField()
favorites = serializers.SerializerMethodField()
ratings = serializers.SerializerMethodField()

class Meta:
model = Article
fields = (
'slug', 'title', 'description', 'body', 'created_at',
'updated_at', 'author', 'ratings', 'tagList', 'like', 'dislike',
'likesCount', 'dislikesCount', 'comments', 'favorite',
'favoritesCount', 'readtime'
'updated_at', 'author', 'comments',
'tagList', 'ratings', 'like_info', 'favorites','readtime'
)


Expand Down
34 changes: 14 additions & 20 deletions authors/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,8 @@ def create(self, request):
article["author"] = request.user.pk
serializer = self.serializer_class(
data=article,
remove_fields=['like', 'created_at', 'updated_at',
'favorite', 'dislike', 'likesCount',
'dislikesCount', 'ratings'])
remove_fields=['like_info', 'created_at', 'updated_at',
'favorites', 'ratings'])
serializer.is_valid(raise_exception=True)
article = serializer.save(author=request.user)
data = serializer.data
Expand Down Expand Up @@ -120,10 +119,9 @@ def get(self, request):
paginator.page_size = page_limit
result = paginator.paginate_queryset(articles, request)
serializer = ArticleSerializer(result, many=True,
remove_fields=['like',
remove_fields=['like_info',
'comments',
'favorite',
'dislike',
'favorites',
'ratings'])
response = paginator.get_paginated_response({
"articles": serializer.data
Expand All @@ -141,14 +139,14 @@ class RetrieveUpdateArticleAPIView(GenericAPIView):
serializer_class = ArticleSerializer

def retrieve_article(self, slug):
"""
Fetch one article
"""
try:
article = Article.objects.get(slug=slug)
return article
except Article.DoesNotExist:
raise ArticleNotFound
"""
Fetch one article
"""
try:
article = Article.objects.get(slug=slug)
return article
except Article.DoesNotExist:
raise ArticleNotFound

def get(self, request, slug):
"""
Expand Down Expand Up @@ -177,10 +175,8 @@ def patch(self, request, slug):
instance=article,
data=request.data,
partial=True,
remove_fields=['like', 'created_at', 'updated_at',
'favorite', 'dislike', 'likesCount',
'dislikesCount',
'ratings']
remove_fields=['like_info', 'created_at', 'updated_at',
'favorites', 'ratings']
)
serializer.is_valid(raise_exception=True)
article = serializer.save()
Expand Down Expand Up @@ -313,8 +309,6 @@ def delete(self, request, slug, vote_type):
A method to delete a vote for a certain article
"""

# def get_vot_response(votetype):

response, voted = self.process_vote_type(request, slug, vote_type)
if voted:
article = RetrieveUpdateArticleAPIView().retrieve_article(slug)
Expand Down
7 changes: 0 additions & 7 deletions authors/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,6 @@
'social_core.backends.twitter.TwitterOAuth',
'django.contrib.auth.backends.ModelBackend',
)
# email settings
EMAIL_BACKEND = os.getenv('EMAIL_BACKEND')
EMAIL_HOST = os.getenv('EMAIL_HOST')
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD')
EMAIL_PORT = os.getenv('EMAIL_PORT')
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER')
EMAIL_USE_TLS = os.getenv('EMAIL_USE_TLS')

DOMAIN = os.getenv("DOMAIN")
VERIFICATION_URL = DOMAIN+'/api/users/activate/'
Expand Down
2 changes: 1 addition & 1 deletion authors/utils/notification_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def email_notification_handler(user, description):
send_mail(
"User Notification",
'',
settings.EMAIL_HOST_USER,
settings.DEFAULT_EMAIL,
[user.user.email],
html_message=html_content)

Expand Down

0 comments on commit 134d880

Please sign in to comment.