Skip to content

Commit

Permalink
Merge 216bf0a into 45571ac
Browse files Browse the repository at this point in the history
  • Loading branch information
pbkabali committed Feb 12, 2019
2 parents 45571ac + 216bf0a commit 1300781
Show file tree
Hide file tree
Showing 15 changed files with 742 additions and 16 deletions.
2 changes: 1 addition & 1 deletion authors/apps/articles/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ class ArticleJSONRenderer(renderers.JSONRenderer):
charset = "utf-8"

def render(self, data, media_type=None, renderer_context=None):
# Method is responsible for displaying user profile
# Method is responsible for displaying articles
return json.dumps({"articles": data})
36 changes: 24 additions & 12 deletions authors/apps/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from .social_login import(login_or_register_social_user)
from .models import User


class RegistrationAPIView(GenericAPIView):
# Allow any user (authenticated or not) to hit this endpoint.
permission_classes = (AllowAny,)
Expand Down Expand Up @@ -56,7 +57,7 @@ def post(self, request):
new_user.save()
current_site = get_current_site(request)
resp_data = self.email_notification(
new_user, current_site, token)
new_user, current_site, token)

return Response(resp_data, status=status.HTTP_201_CREATED)

Expand Down Expand Up @@ -110,12 +111,12 @@ def get(self, request, token):
user_id = User.decode_token(token)
if not isinstance(user_id, int):
return Response(
{'err_msg': user_id}, status=status.HTTP_400_BAD_REQUEST)
{'err_msg': user_id}, status=status.HTTP_400_BAD_REQUEST)
try:
user = User.objects.get(pk=user_id)
except User.DoesNotExist as e:
return Response(
{'err_msg': str(e)}, status=status.HTTP_400_BAD_REQUEST)
{'err_msg': str(e)}, status=status.HTTP_400_BAD_REQUEST)
if user and not user.is_active:
user.is_active = True
user.save()
Expand All @@ -127,7 +128,7 @@ def get(self, request, token):
elif user.is_active:
return Response({
"msg": "This account is already activated"
}, status=status.HTTP_400_BAD_REQUEST)
}, status=status.HTTP_400_BAD_REQUEST)


class LoginAPIView(GenericAPIView):
Expand All @@ -148,6 +149,7 @@ def post(self, request):

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


class UserRetrieveUpdateAPIView(RetrieveUpdateAPIView):
permission_classes = (IsAuthenticated,)
renderer_classes = (UserJSONRenderer,)
Expand Down Expand Up @@ -175,6 +177,7 @@ def update(self, request, *args, **kwargs):

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


class TwitterAuthAPIView(GenericAPIView):
"""
Handle login of a Twitter user via the Twitter Api.
Expand All @@ -196,7 +199,7 @@ def post(self, request):
access_token_secret = request.data.get('access_token_secret', {})

serializer = self.serializer_class(data={'access_token': access_token,
'access_token_secret': access_token_secret
'access_token_secret': access_token_secret
})
serializer.is_valid(raise_exception=True)

Expand All @@ -214,6 +217,7 @@ def post(self, request):

return login_or_register_social_user(twitter_user)


class GoogleAuthAPIView(GenericAPIView):
"""
Handle login of a Google user via the Google oauth2.
Expand Down Expand Up @@ -243,6 +247,7 @@ def post(self, request):

return login_or_register_social_user(google_user)


class FacebookAuthAPIView(GenericAPIView):
"""
Handle login of a Facebook user via the Facebook Graph API.
Expand All @@ -269,6 +274,7 @@ def post(self, request):

return login_or_register_social_user(facebook_user)


class PasswordResetRequestAPIView(GenericAPIView):
"""
This handles receiving the requester's email address and sends
Expand All @@ -285,13 +291,16 @@ def post(self, request):
serializer.is_valid(raise_exception=True)
user_email = user.get("email", None)
PasswordResetManager(request).send_password_reset_email(user_email)
msg = "An email has been sent to your \
mailbox with instructions to reset your password"
return Response(
{
"message":"An email has been sent to your mailbox with instructions to reset your password",
"message": msg,
},
status=status.HTTP_200_OK
)


class PasswordResetAPIView(GenericAPIView):
"""
This handles verification of reset link and updates password field
Expand All @@ -304,29 +313,32 @@ class PasswordResetAPIView(GenericAPIView):
def get(self, request, token):
user = PasswordResetManager(request).get_user_from_encoded_token(token)
if user is None:
return Response({"message":"Invalid token!"}, status=status.HTTP_400_BAD_REQUEST)
return Response({"message": "Invalid token!"},
status=status.HTTP_400_BAD_REQUEST)
return Response(
{
"message":"Token is valid. OK to send new password information"
"message": (
"Token is valid. OK to send new password information"
)
},
status=status.HTTP_200_OK
)

def post(self, request, token):
data = request.data.get(
'user', {}) if 'user' in request.data else request.data
print(data)
user = PasswordResetManager(request).get_user_from_encoded_token(token)
if user is None:
return Response(
{
"message":"Your token has expired. Please start afresh."
"message": "Your token has expired. Please start afresh."
},
status=status.HTTP_400_BAD_REQUEST
)
)
email = user.get('email')
serializer = self.serializer_class(data=data)
serializer.is_valid(raise_exception=True)
new_password = data.get("new_password")
PasswordResetManager(request).update_password(email, new_password)
return Response({"message":"Your password has been reset"} , status=status.HTTP_200_OK)
return Response({"message": "Your password has been reset"},
status=status.HTTP_200_OK)
Empty file.
45 changes: 45 additions & 0 deletions authors/apps/comments/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Generated by Django 2.1.5 on 2019-02-07 14:12

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
('profiles', '0001_initial'),
('articles', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='Comment',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('body', models.TextField()),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True, null=True)),
('article', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='articles.Article')),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='profiles.Profile')),
],
options={
'ordering': ['created_at'],
},
),
migrations.CreateModel(
name='CommentReply',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('body', models.TextField()),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True, null=True)),
('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='profiles.Profile')),
('comment', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='comments.Comment')),
],
options={
'ordering': ['created_at'],
},
),
]
21 changes: 21 additions & 0 deletions authors/apps/comments/migrations/0002_auto_20190211_1918.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 2.1.5 on 2019-02-11 19:18

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('comments', '0001_initial'),
]

operations = [
migrations.AlterModelOptions(
name='comment',
options={'ordering': ['-created_at']},
),
migrations.AlterModelOptions(
name='commentreply',
options={'ordering': ['-created_at']},
),
]
Empty file.
43 changes: 43 additions & 0 deletions authors/apps/comments/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from django.db import models

from ..profiles.models import Profile
from ..articles.models import Article


class Comment(models.Model):
"""The model defines the Comments table as stored in the DB
"""
body = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True, null=True)
author = models.ForeignKey(Profile, on_delete=models.CASCADE)
article = models.ForeignKey(Article, on_delete=models.CASCADE)

class Meta:
"""
Ensure that the comments are returned in the order they were created
"""
ordering = ['-created_at']

def __str__(self):
return self.body


class CommentReply(models.Model):
"""The model defines the Comment-Replies table as stored in the DB
"""
body = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True, null=True)
comment = models.ForeignKey(Comment, on_delete=models.CASCADE)
author = models.ForeignKey(Profile, on_delete=models.CASCADE)

class Meta:
"""
Ensure that the comment-replies are returned in the order they were
created
"""
ordering = ['-created_at']

def __str__(self):
return self.body
58 changes: 58 additions & 0 deletions authors/apps/comments/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from rest_framework import serializers

from .models import Comment, CommentReply
from ..profiles import serializers as ProfileSerializers


class CommentSerializer(serializers.ModelSerializer):
"""This serializer class specifies fields to render to the user
for reteriving, updating or creating of a comment on an article.
It specifies the author, article, created_at and updated_at fields
as read-only since these fields data is auto generated
"""
author = ProfileSerializers.ProfileSerializer(read_only=True)

class Meta:
model = Comment
fields = (
"created_at",
"updated_at",
"body",
"author",
"article",
"id"
)
read_only_fields = (
"article",
"author",
"created_at",
'updated_at'
"id"
)


class CommentReplySerializer(serializers.ModelSerializer):
"""This serializer class specifies fields to render to the user for
reteriving,updating or creating of a reply to a comment. It specifies
the author, comment,created_at and updated_at fields as read-only
since these fields data is auto generated
"""
author = ProfileSerializers.ProfileSerializer(read_only=True)

class Meta:
model = CommentReply
fields = (
"comment",
"created_at",
"updated_at",
"body",
"author",
"id"
)
read_only_fields = (
'author',
'created_at',
'updated_at',
'comment',
"id"
)
Empty file.
Loading

0 comments on commit 1300781

Please sign in to comment.