Skip to content

Commit

Permalink
feat(articles): Format code
Browse files Browse the repository at this point in the history
  • Loading branch information
SnyderMbishai committed Dec 10, 2018
1 parent d38c9c3 commit 167b342
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 30 deletions.
10 changes: 7 additions & 3 deletions authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
from ..authentication.serializers import UserSerializer

class ArticleSerializer(serializers.ModelSerializer):
"""
Class to serialize article details.
"""
title = serializers.CharField(max_length=200)
description = serializers.CharField()
body = serializers.CharField()
author = serializers.HiddenField(
default = serializers.CurrentUserDefault()
)
# author = UserSerializer(read_only = True)
# author = serializers.SerializerMethodField()

class Meta:
model = Article
Expand All @@ -31,7 +32,10 @@ def update(self, instance, data):
def get_author(self,Article):
return Article.author.pk

class AuthorSerializer(serializers.ModelSerializer):
class ArticleAuthorSerializer(serializers.ModelSerializer):
"""
Class to serialize article and return the full owner information.
"""
title = serializers.CharField(max_length=200)
description = serializers.CharField()
body = serializers.CharField()
Expand Down
73 changes: 46 additions & 27 deletions authors/apps/articles/views.py
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)


0 comments on commit 167b342

Please sign in to comment.