-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
243 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 0 additions & 48 deletions
48
authors/apps/articles/migrations/0002_auto_20190128_1814.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# local imports | ||
from rest_framework import status | ||
from rest_framework.generics import (ListCreateAPIView, | ||
RetrieveUpdateDestroyAPIView) | ||
from rest_framework.permissions import IsAuthenticated | ||
from rest_framework.response import Response | ||
from rest_framework.views import APIView | ||
|
||
from authors.apps.articles.models import Article, FavoriteArticle | ||
from authors.apps.articles.renderers import ArticleJSONRenderer | ||
from authors.apps.articles.response_messages import (success_messages, | ||
error_messages) | ||
|
||
from authors.apps.articles.serializers import (FavouriteSerializer, | ||
ArticleSerializer) | ||
|
||
|
||
# Create your views here. | ||
class CreateArticleView(ListCreateAPIView): | ||
permission_classes = (IsAuthenticated,) | ||
serializer_class = ArticleSerializer | ||
renderer_classes = (ArticleJSONRenderer,) | ||
queryset = Article.objects.all() | ||
|
||
def perform_create(self, serializer): | ||
serializer.save(author=self.request.user) | ||
|
||
|
||
class FavouriteArticle(RetrieveUpdateDestroyAPIView): | ||
permission_classes = (IsAuthenticated,) | ||
serializer_class = FavouriteSerializer | ||
|
||
def put(self, request, *args, **kwargs): | ||
|
||
article = Article.objects.filter(slug=kwargs['slug']).first() | ||
if not article: | ||
msg = {"message": error_messages['article_404']} | ||
return Response(msg, status.HTTP_404_NOT_FOUND) | ||
|
||
favorite_article = FavoriteArticle.objects.filter( | ||
user=request.user.id, article=article.id).exists() | ||
|
||
# if article is already a favorite, remove from favorites | ||
if favorite_article: | ||
instance = FavoriteArticle.objects.filter( | ||
user=request.user.id, article=article.id) | ||
|
||
self.perform_destroy(instance) | ||
msg = {"message": success_messages['remove_favorite']} | ||
return Response(msg, status.HTTP_200_OK) | ||
|
||
# if article is not a favorite, then make it favorite | ||
data = {"article": article.id, "user": request.user.id} | ||
serializer = self.serializer_class(data=data) | ||
serializer.is_valid(raise_exception=True) | ||
serializer.save() | ||
msg = {"message": success_messages['add_favorite']} | ||
return Response(msg, status.HTTP_200_OK) | ||
|
||
|
||
class GetFavouriteArticles(APIView): | ||
permission_classes = (IsAuthenticated,) | ||
serializer_class = FavouriteSerializer | ||
|
||
def get(self, request): | ||
fav_article = FavoriteArticle.objects.filter(user=request.user.id) | ||
serializer = FavouriteSerializer(fav_article, many=True) | ||
return Response(serializer.data, status.HTTP_200_OK) |
Oops, something went wrong.