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

Implemented an up vote(like) system for the blog comments #111

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"python.pythonPath": "/usr/local/bin/python3"
"python.pythonPath": "/usr/bin/python3"
}
44 changes: 40 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,8 @@ Simplified tree diagram
"updated": time.current,
"anonymous": BOOLEAN,
"publish": BOOLEAN,
"comments" : NUM_COMMENTS
"comments" : NUM_COMMENTS,
"likes": NUM_OF_LIKES

}

Expand Down Expand Up @@ -484,7 +485,8 @@ Simplified tree diagram
"updated": time.current,
"anonymous": BOOLEAN,
"publish": BOOLEAN,
"comments" : NUM_COMMENTS
"comments" : NUM_COMMENTS,
"likes": NUM_OF_LIKES

}
```
Expand Down Expand Up @@ -536,7 +538,8 @@ returns
"updated": time.current,
"anonymous": BOOLEAN,
"publish": BOOLEAN,
"comments" : NUM_COMMENTS
"comments" : NUM_COMMENTS,
"likes": NUM_OF_LIKES
}
```

Expand All @@ -558,12 +561,45 @@ returns
}
```

### Like a Blog Post
PATCH /blogs/id/<BLOG_ID>/likes/
like the post if the user hasn't liked this post yet. Cancel the like if the user has already liked the post.
returns
```javascript
{
"id": BLOG.ID,
"author": "FIRST_NAME + LAST_NAME,
"user": USER.ID,
"body": UPDATED_BODY,
"title": UPDATED_TITLE,
"images": [
{
"id": IMAGE.ID,
"filename": FILENAME,
"blog": BLOG.ID,
"picture": FILEURL,
},
.
.
.
],
"published": time.publish,
"created": time.created,
"updated": time.current,
"anonymous": BOOLEAN,
"publish": BOOLEAN,
"comments" : NUM_COMMENTS,
"likes": NUM_OF_LIKES
}
```



### Create Comment
POST /blogs/comment/

Enter blog or comment id to associate created comment with blog or comment.
Enter type <STRING> either 'blog' or 'post'
Enter type <STRING> either 'blog' or 'comment'
Comments are connected like a single-directional tree with a blog root node.

```
Expand Down
22 changes: 22 additions & 0 deletions src/blog/migrations/0006_blogpost_likes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.7 on 2020-05-27 21:07
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('blog', '0005_auto_20190527_0852'),
]

operations = [
migrations.AddField(
model_name='blogpost',
name='likes',
field=models.ManyToManyField(blank=True, to=settings.AUTH_USER_MODEL),
),
]
5 changes: 5 additions & 0 deletions src/blog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class BlogPost(models.Model):
updated = models.DateTimeField(auto_now=True,editable=False)
publish = models.BooleanField(default=False)
anonymous = models.BooleanField(default=False)
likes = models.ManyToManyField(User, blank=True)

class Meta:
ordering = ('-publish',)
Expand All @@ -36,6 +37,10 @@ def getComments(self):
return self.commentblog.all().count()
else:
return 0

@property
def getLikes(self):
return self.likes.count()


class BlogPicture(models.Model):
Expand Down
3 changes: 2 additions & 1 deletion src/blog/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ class Meta:

class BlogPostSerializer(serializers.ModelSerializer):
comments = serializers.IntegerField(source='getComments')
likes = serializers.IntegerField(source='getLikes')
images = BlogPictureSerializer(many=True)
class Meta:
model = BlogPost
fields = ('id','author','user','body','title','images','publish','anonymous','created','published','updated','comments')
fields = ('id','author','user','body','title','images','publish','anonymous','created','published','updated','comments', 'likes')
read_only_fields = ('id','anonymous','created','updated')


Expand Down
1 change: 1 addition & 0 deletions src/blog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
url(r'^blogs/comment/?$', views.CreateCommentView.as_view(),name='createcomment'),
url(r'^blogs/comment/id/(?P<comment_id>[0-9]+)/?$', views.RUDCommentView.as_view(),name='RUDComment'),
url(r'^blogs/comment/id/(?P<comment_id>[0-9]+)/likes/?$',views.UpdateCommentLikesView.as_view(),name='commentlikes'),
url(r'^blogs/id/(?P<blog_id>[0-9]+)/likes/?$',views.UpdateBlogLikesView.as_view(),name='bloglikes'),
url(r'^blogs/(?P<username>[\w.@+-]+)/?$', views.CreateBlogView.as_view(), name="createview"),
url(r'^blogs/?$', views.BlogView.as_view(), name='blogs'),
url(r'^blogs/id/(?P<blog_id>[0-9]+)/?$', views.RUDBlogView.as_view(), name='RUDBlog'),
Expand Down
23 changes: 21 additions & 2 deletions src/blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def post(self, request):
if(blogpost != None):
new_comment = Comment.objects.create(
user = self.request.user,
author=self.request.user.first_name + ' ' + request.user.last_name,
author=self.request.user.first_name + ' ' + self.request.user.last_name,
blog = blogpost,
body = self.request.data['body'],
)
Expand All @@ -175,7 +175,7 @@ def post(self, request):
if(commentpost != None):
new_comment = Comment.objects.create(
user = self.request.user,
author=self.request.user.first_name + ' ' + request.user.last_name,
author=self.request.user.first_name + ' ' + self.request.user.last_name,
comment = commentpost,
body = self.request.data['body'],
)
Expand Down Expand Up @@ -247,6 +247,25 @@ def get_queryset(self):
queryset = queryset.all()[:num]
return queryset

#Increment or decrement likes
class UpdateBlogLikesView(generics.UpdateAPIView):
serializer_class = BlogPostSerializer
queryset = BlogPost.objects.all()



def update(self, request, *args, **kwargs):
blog = get_object_or_404(BlogPost, id=int(self.kwargs['blog_id']))

if(blog.likes.filter(id = self.request.user.id).exists()):
blog.likes.remove(self.request.user)
else:
blog.likes.add(self.request.user)


return Response(BlogPostSerializer(blog).data, status=200)


#Increment or decrement likes
class UpdateCommentLikesView(generics.UpdateAPIView):
serializer_class = CommentSerializer
Expand Down