Skip to content

Commit

Permalink
bg-(Return articles):Fix return articles in profiles
Browse files Browse the repository at this point in the history
- Added return articles in getting profile

[starts #166758326]
  • Loading branch information
a-braham authored and Abraham Kamau committed Jun 18, 2019
1 parent 3ac56b6 commit b1c947a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 12 deletions.
5 changes: 2 additions & 3 deletions authors/apps/appnotifications/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,8 @@ class UnreadNotificationsAPIview(NotificationApiView):
list all user's unread notifications
"""

def notifications(self, request):
request.user.notifications.unread()
return request.user.notifications.active()
def notifications(self, request):
return request.user.notifications.unread()


class DeleteSingleNotificationAPIView(NotificationApiView):
Expand Down
1 change: 1 addition & 0 deletions authors/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ def delete(self, request, slug, vote_type):
response, voted = self.process_vote_type(request, slug, vote_type)
if voted:
article = RetrieveUpdateArticleAPIView().retrieve_article(slug)
article.votes.delete(request.user.id)
serializer = ArticleSerializer(
article,
context={'article': slug, 'request': request, },
Expand Down
5 changes: 4 additions & 1 deletion authors/apps/profiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ class Profile(models.Model):
last_name = models.CharField(max_length=30, blank=True)
bio = models.TextField(max_length=500, blank=True)
image = CloudinaryField(
'image', default='https://res.cloudinary.com/grean/image/upload/v1556488518/samples/vbioaj1wwewmtmeryucv.jpg')
'image',
default=
'https://res.cloudinary.com/grean/image/upload/v1560445304/default_ddfimn.png'
)
following = models.ManyToManyField(
'self', symmetrical=False, related_name='w_following')
created_at = models.DateTimeField(auto_now_add=True)
Expand Down
7 changes: 6 additions & 1 deletion authors/apps/profiles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def __init__(self, *args, **kwargs):

username = serializers.ReadOnlyField(source='fetch_username')
img_url = serializers.ReadOnlyField(source='fetch_image')
articles = serializers.SerializerMethodField()
created_at = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")
updated_at = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")
following = serializers.SerializerMethodField()
Expand All @@ -30,10 +31,14 @@ def get_following(self, instance):
follow_status = my_profile.if_following(instance)
return follow_status

def get_articles(self, instance):
articles = self.context.get('articles', None)
return articles

class Meta:
model = Profile
fields = (
'username', 'first_name', 'last_name', 'bio', 'img_url', 'created_at', 'updated_at', 'following'
'username', 'first_name', 'last_name', 'bio', 'img_url', 'articles', 'created_at', 'updated_at', 'following'
)


Expand Down
21 changes: 14 additions & 7 deletions authors/apps/profiles/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.core.exceptions import ValidationError

from rest_framework.exceptions import APIException
from rest_framework.generics import GenericAPIView, ListAPIView
from rest_framework import permissions, status
Expand All @@ -8,9 +7,12 @@
from authors.apps.authentication.models import User
from .exceptions import *
import json
from .serializers import (
UserProfileSerializer, UpdateUserProfileSerializer,
FollowingSerializer, FollowersListSerializer, UserListSerializer)


from .serializers import UserProfileSerializer, UpdateUserProfileSerializer, FollowingSerializer, FollowersListSerializer, UserListSerializer
from authors.apps.articles.serializers import ArticleSerializer
from authors.apps.articles.models import Article
from .models import Profile
from .renderers import FollowersJSONRenderer

Expand All @@ -34,15 +36,20 @@ def get(self, request, username):
'user': ['User does not exist']
}
}, status=status.HTTP_404_NOT_FOUND)

articles = Article.objects.filter(author__username=username)
articles_serializer = ArticleSerializer(
articles, many=True, context={'request': request},
remove_fields=['like_info', 'comments', 'favorites', 'ratings']
)
articles = articles_serializer.data
if request.user.username == username:
serializer = UserProfileSerializer(
profile, context={'request': request},
profile, context={'request': request, 'articles': articles},
remove_fields=['following']
)
else:
serializer = UserProfileSerializer(
profile, context={'request': request}
profile, context={'request': request, 'articles': articles}
)
return Response({
'profile': serializer.data
Expand Down Expand Up @@ -145,7 +152,7 @@ def get(self, request, username):
raise UserNotFound
to_check = Profile.objects.get(user_id=the_user.id)
my_follows = to_check.following_list()
serializer = self.get_serializer(my_follows, many=True)
serializer = UserProfileSerializer(my_follows, many=True)
return Response({"following": serializer.data,
"count": len(serializer.data)},
status=status.HTTP_200_OK)
Expand Down

0 comments on commit b1c947a

Please sign in to comment.