Skip to content

Commit

Permalink
feat(bookmark article): booking an article
Browse files Browse the repository at this point in the history
User be able to bookmark an article

- create bookmaking article endpoint
- write tests.

[Finishes #162163183]
  • Loading branch information
mulondo moses authored and mulondo moses committed Dec 19, 2018
1 parent 36d39e5 commit 9105fa0
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 6 deletions.
3 changes: 3 additions & 0 deletions authors/apps/articles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,6 @@ class Impressions(TimestampedModel):
default=None
)

class BookMarkArticle(TimestampedModel):
user = models.ForeignKey(User,on_delete=models.CASCADE)
article = models.CharField(max_length=50)
7 changes: 6 additions & 1 deletion authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from rest_framework import serializers
from authors.apps.profiles.serializers import GetUserProfileSerializer
from .models import Article, Rating
from .models import Article, Rating, BookMarkArticle
from authors.apps.profiles.serializers import (
GetUserProfileSerializer)
from .models import (
Expand Down Expand Up @@ -69,4 +69,9 @@ def get_updated_at(self, instance):

return instance.updated_at.isoformat()

class BookMarkArticleSerializer(serializers.ModelSerializer):
class Meta:
model = BookMarkArticle
fields = ['article', 'user', 'updated_at']


57 changes: 57 additions & 0 deletions authors/apps/articles/tests/test_bookmarking_artcle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from rest_framework import status
from .base_test import BaseTest


class TestBookMarking(BaseTest):

def test_bookmark_article(self):

posted_article = self.client.post(
'/api/articles/',
data=self.new_article,
format='json')
slug = self.get_slug(posted_article)
response = self.client.post(
'/api/articles/bookmark/{}'.format(slug)
)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_unbookmarking_article(self):

posted_article = self.client.post(
'/api/articles/',
data=self.new_article,
format='json')
slug = self.get_slug(posted_article)
self.client.post(
'/api/articles/bookmark/{}'.format(slug)
)
response = self.client.post(
'/api/articles/bookmark/{}'.format(slug)
)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)

def test_bookmarking_article_which_doesnot_exist(self):
posted_article = self.client.post(
'/api/articles/',
data=self.new_article,
format='json')
response = self.client.post(
'/api/articles/bookmark/best-of-the-best-1234ghj9'
)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

def test_get_bookmarked_article(self):

posted_article = self.client.post(
'/api/articles/',
data=self.new_article,
format='json')
slug = self.get_slug(posted_article)
self.client.post(
'/api/articles/bookmark/{}'.format(slug)
)
response = self.client.get(
'/api/articles/bookmark/'
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
7 changes: 5 additions & 2 deletions authors/apps/articles/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
from rest_framework.routers import DefaultRouter
from .views import (
ArticleViewSet, ArticleRetrieve,
LikeArticle, DislikeArticle, RatingsView)
LikeArticle, DislikeArticle, RatingsView, BookMarkArticleView,BookMarkView)


urlpatterns = [
path('articles/', ArticleViewSet.as_view()),
path('articles/<slug>', ArticleRetrieve.as_view()),
path('articles/<slug>/ratings/', RatingsView.as_view()),
path('articles/like/<slug>', LikeArticle.as_view()),
path('articles/dislike/<slug>', DislikeArticle.as_view())
path('articles/dislike/<slug>', DislikeArticle.as_view()),
path('articles/bookmark/<slug>', BookMarkArticleView.as_view()),
path('articles/bookmark/', BookMarkView.as_view())

]
43 changes: 41 additions & 2 deletions authors/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
RetrieveUpdateDestroyAPIView,
ListCreateAPIView)
from .models import (
Article, Impressions)
Article, Impressions, BookMarkArticle)
from .renderers import ArticleJSONRenderer
from .serializers import (
ArticleSerializer, ImpressionSerializer,
RatingSerializer)
RatingSerializer, BookMarkArticleSerializer)
from ..authentication.models import User
from django.db.models import Count

Expand Down Expand Up @@ -253,3 +253,42 @@ def updateimpression(self, impression):
)
serializer.is_valid(raise_exception=True)
serializer.save()

class BookMarkArticleView(ListCreateAPIView):
"""
class for bookmarking and unbookmarking an article
"""
permission_classes = (IsAuthenticated,)
queryset = Article.objects.all()
def post(self,request,slug):
try:
serializer_instance = self.queryset.get(slug=slug)
except Article.DoesNotExist:
return Response('An article with this slug does not exist.',status.HTTP_404_NOT_FOUND)
obj = BookMarkArticle.objects.filter(article=slug)

if len(obj)==0:
bk = {'user': request.user.id, 'article': slug}
serializer = BookMarkArticleSerializer(data=bk)
serializer.is_valid()
serializer.save()

if obj:
obj.delete()
return Response("unbookmarked",status.HTTP_204_NO_CONTENT)

return Response("article bookmarked")

class BookMarkView(ListCreateAPIView):
"""
class for getting bookmarked articles
"""
permission_classes = (IsAuthenticated,)
serializer_class = BookMarkArticleSerializer
article = []
def get(self,request):
obj = BookMarkArticle.objects.filter(user_id=request.user.id)
for i in obj:
self.article.append(i.article)
return Response(self.article)

3 changes: 2 additions & 1 deletion authors/apps/profiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def update(self,request,slug):
userprofile_obj.favorite_article.remove(slug)
userprofile_obj.save()
return Response("unfavorited!")

userprofile_obj.favorite_article.append(slug)
userprofile_obj.save(update_fields = ['favorite_article'])
return Response("favorited!")
Expand Down Expand Up @@ -143,3 +143,4 @@ def get(self, request, username):
username = User.objects.get(id=follow['follower']).username
followers.append(username)
return Response({'followers': followers}, status=status.HTTP_200_OK)

0 comments on commit 9105fa0

Please sign in to comment.