Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add category title #52

Merged
merged 1 commit into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
class ArticleSerializer(TaggitSerializer, serializers.ModelSerializer):
"""Serializer for articles."""
author = serializers.ReadOnlyField(source='author.username')
category_title = serializers.ReadOnlyField(source='category.title')
read_time = serializers.ReadOnlyField(source='read')
tags = TagListSerializerField()
is_published = serializers.ReadOnlyField()

class Meta:
model = Article
""" List all of the fields that could possibly be included in a request
Expand All @@ -24,7 +26,7 @@ class Meta:
'author', 'title', 'slug', 'description', 'body', 'created_at',
'updated_at', 'read_time', 'average_rating', 'likes', 'dislikes',
'tags', 'category', 'favorites_count', 'image', 'published_on',
'is_published',)
'is_published', 'category_title')
read_only_fields = ('slug', 'author_id', 'is_published')


Expand Down
24 changes: 13 additions & 11 deletions authors/apps/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from django.shortcuts import redirect

from authors.apps.authentication.backends import JWTAuthentication
from authors.apps.authentication.models import User
Expand Down Expand Up @@ -45,14 +46,15 @@ def sendEmailVerification(self, user, request, user_data):
username = user['username']
subject = "Ah haven Account Verification"
body = "Hello {}, Thank you for creating an account with us, kindly click on the link below to activate your account! \n\n \
{}/api/users/verify_account/{}/" .format(username,request.get_host(), user_data['token'])
{}/api/users/verify_account/{}/" .format(username, request.get_host(), user_data['token'])
to_email = [user["email"]]
email = EmailMessage(subject, body, to=to_email,)
email.send()
user_data.update({
'message': 'A verification link has been sent to your Email, please visit your Email and activate your account'
})


class LoginAPIView(APIView):

"""
Expand Down Expand Up @@ -110,24 +112,24 @@ def update(self, request, *args, **kwargs):

return Response(serializer.data, status=status.HTTP_200_OK)

class UserAccountVerifyView(APIView,JWTAuthentication):

class UserAccountVerifyView(APIView, JWTAuthentication):
"""
get user email:
send verification link to the user email.

"""
renderer_classes = (UserJSONRenderer,)
serializer_class = UserSerializer

def get(self, request, token):

try:
user, user_token = self.authenticate_credentials(request, token)
if not user.is_verified:
user.is_verified=True
user.is_verified = True
user.save()
return Response({
'message': 'Your Account has been verified, continue to login',
}, status=status.HTTP_200_OK)
return redirect('https://ah-frontend-lannister.herokuapp.com/login')

raise serializers.ValidationError(
'Activation link invalid or expired'
Expand All @@ -137,12 +139,12 @@ def get(self, request, token):
'Activation link invalid or expired'
)


class UserPasswordReset(RetrieveUpdateAPIView):
permission_classes = (AllowAny,)
renderer_classes = (UserJSONRenderer,)
serializer_class = PasswordResetSerializer


def post(self, request):
user = request.data.get('user', {})
# check data with serializer
Expand All @@ -152,12 +154,12 @@ def post(self, request):
# send email method
email = data_from_serializer.data['email']
token = data_from_serializer.data['token']
host='http://'+request.get_host()+'/api/users/password_reset/confirm/'
url=user.get('url',host)+token
response = send_gridmail(email,url)
host = 'http://'+request.get_host()+'/api/users/password_reset/confirm/'
url = user.get('url', host)+token
response = send_gridmail(email, url)
return Response(response, status=status.HTTP_200_OK)

def retrieve(self, request,token, *args, **kwargs):
def retrieve(self, request, token, *args, **kwargs):
url = request.get_host()
#validate url here if its valid ie token
# else send them invalid token message
Expand Down
7 changes: 5 additions & 2 deletions authors/apps/comments/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ class Meta:


class CommentChildSerializer(serializers.ModelSerializer):
author = serializers.ReadOnlyField(source='author.username')

author = serializers.SerializerMethodField()
class Meta:
model = Comment
fields = ('id', 'body', 'author', 'created_at', 'updated_at', 'parent')
Expand All @@ -71,6 +70,10 @@ def validate(self, data):
'Please insert the body of the comment'
)
return data

def get_author(self, obj):
x = get_object_or_404(Profile, user=obj.author)
return ProfileSerializer(x).data


class CommentHistorySerializer(serializers.ModelSerializer):
Expand Down
6 changes: 3 additions & 3 deletions authors/apps/comments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class CommentListCreateView(generics.ListCreateAPIView):

"""
serializer_class = CommentSerializer
permission_classes = (IsAuthenticated,)
permission_classes = (IsAuthenticatedOrReadOnly,)
renderer_classes = (CommentJSONRenderer,)
queryset = Comment.objects.all().filter(parent=None)
lookup_field = 'slug'
Expand Down Expand Up @@ -95,7 +95,7 @@ def notify_follower_reciever(sender, instance, created, **kwargs):

class CommentsView(generics.RetrieveUpdateDestroyAPIView):
serializer_class = CommentSerializer
permission_classes = (IsAuthenticated,)
permission_classes = (IsAuthenticatedOrReadOnly,)
renderer_classes = (CommentJSONRenderer,)
lookup_fields = 'id', 'slug'
queryset = Comment.objects.all().filter(parent__isnull=True)
Expand Down Expand Up @@ -132,7 +132,7 @@ def get_object(self):


class CommentThreadListCreateView(generics.ListCreateAPIView):
permission_classes = (IsAuthenticated,)
permission_classes = (IsAuthenticatedOrReadOnly,)
serializer_class = CommentChildSerializer
renderer_classes = (CommentThreadJSONRenderer,)
lookup_fields = 'id', 'slug'
Expand Down
3 changes: 1 addition & 2 deletions tests/test_authentication/test_account_verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ def test_verify_account(self):
user1 = User.objects.get(email='kfddfssss@andela.com')
res = user1.is_verified
self.assertTrue(res)
self.assertIn("message", str(respond.data))
self.assertEqual(status.HTTP_200_OK,respond.status_code)
self.assertEqual(status.HTTP_302_FOUND, respond.status_code)

def test_failed_verify_account(self):
self.user = User.objects.get(email=self.email)
Expand Down