-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
d38c9c3
commit 167b342
Showing
2 changed files
with
53 additions
and
30 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
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 |
---|---|---|
@@ -1,58 +1,77 @@ | ||
from django.shortcuts import render | ||
from rest_framework import status | ||
from rest_framework.views import APIView | ||
from requests.exceptions import HTTPError | ||
from rest_framework.response import Response | ||
from rest_framework.permissions import AllowAny, IsAuthenticated, IsAuthenticatedOrReadOnly | ||
|
||
from .models import Article | ||
from .serializers import ArticleSerializer, AuthorSerializer | ||
from .serializers import ArticleSerializer, ArticleAuthorSerializer | ||
|
||
# Create your views here. | ||
|
||
class ArticleAPIView(APIView): | ||
permission_classes = (IsAuthenticated,IsAuthenticatedOrReadOnly) | ||
""" | ||
Class for handling Article. | ||
""" | ||
permission_classes = (IsAuthenticated,IsAuthenticatedOrReadOnly,) | ||
|
||
def post(self, request): | ||
""" | ||
Method for creating an article | ||
""" | ||
article_data = request.data.get('article') | ||
context = {'request':request} | ||
serializer = ArticleSerializer(data=article_data, context=context) | ||
serializer.is_valid(raise_exception = True) | ||
if serializer.is_valid(): | ||
saved_article = serializer.save() | ||
return Response({"message":"lalalaala"}) | ||
return Response({'message':"invalid data"}) | ||
# serializer = ArticleSerializer(data=article_data) | ||
|
||
# serializer.is_valid(raise_exceptions=True) | ||
# print(serializer.data,"nananan") | ||
# serializer.save() | ||
# return Response({'message':"message"}) | ||
|
||
|
||
serializer.is_valid(raise_exception=True) | ||
saved_article = serializer.save() | ||
message = {'message': "The article '{}' has been successfully created.".format(saved_article.title)} | ||
return Response(message, status=status.HTTP_201_CREATED) | ||
|
||
def get(self, request, pk=None): | ||
""" | ||
Method for getting one or all articles. | ||
""" | ||
# If the primary key is provided, a specific article should be returned. | ||
if pk: | ||
article = Article.objects.get(pk=pk) | ||
serializer = AuthorSerializer(article) | ||
return Response({'article':serializer.data}) | ||
serializer = ArticleAuthorSerializer(article) | ||
message = {'message':"Article found.", 'article':serializer.data} | ||
return Response(message, status=status.HTTP_200_OK) | ||
# If no primary is provided, all articles should be returned. | ||
else: | ||
articles = Article.objects.all() | ||
serializer = AuthorSerializer(articles, many=True) | ||
return Response({'articles': serializer.data}) | ||
serializer = ArticleAuthorSerializer(articles, many=True) | ||
message = {'message':"Articles found.", 'articles': serializer.data} | ||
return Response(message, status=status.HTTP_200_OK) | ||
|
||
def put(self, request, pk): | ||
#pk is the primary key | ||
""" | ||
Method for editing an article. | ||
""" | ||
# A primary key should be provided. | ||
# An article should be edited by the person who created it only. | ||
article = Article.objects.get(pk=pk) | ||
data = request.data.get('article') | ||
serializer = ArticleSerializer(instance=article, data=data, partial=True) | ||
|
||
if serializer.is_valid(): | ||
article = serializer.save() | ||
|
||
return Response({'message':"Article has been successfully updated."}) | ||
serializer.is_valid(raise_exception=True) | ||
article = serializer.save() | ||
a = AuthorSerializer(article) | ||
print(a, "1111") | ||
message = { | ||
'message': "Article has been successfully updated.", | ||
'article': article.title | ||
} | ||
return Response(message, status=status.HTTP_200_OK) | ||
|
||
def delete(self, request, pk): | ||
""" | ||
Delete a specific item. | ||
""" | ||
article = Article.objects.get(pk=pk) | ||
article.delete() | ||
return Response({'message':"Successfully deleted."}) | ||
message = { | ||
'message':"article '{}' has been successfully deleted.".format(article.title) | ||
} | ||
return Response(message, status=status.HTTP_200_OK) | ||
|
||
|