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

Restricted user to only like once #1

Open
wants to merge 1 commit 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
9 changes: 8 additions & 1 deletion blogproject/blogapp/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.contrib import admin
from .models import Post, Comment, Category
from .models import Post, Comment, Category,PostLiked

class PostAdmin(admin.ModelAdmin):
list_display = ['title', 'get_full_name', 'post_date', 'category', 'nr_comments', 'likes', 'visible']
Expand All @@ -20,6 +20,13 @@ class CommentAdmin(admin.ModelAdmin):
list_display = ['author', 'post', 'comment_date', 'visible']
search_fields = ['author', 'post__title']


@admin.register(PostLiked)
class PostLikedAdmin(admin.ModelAdmin):
list_display = ['name', 'post_id', ]
search_fields = ['name', 'post_id']


@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
list_display = ['name', 'get_nr_posts', 'nr_comments', 'nr_likes']
Expand Down
6 changes: 6 additions & 0 deletions blogproject/blogapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def __str__(self):
def get_absolute_url(self):
return reverse('blog:PostDetailView', args=[self.pk])


class PostLiked(models.Model):
name = models.ForeignKey(User, on_delete=models.CASCADE)
post_id = models.CharField(max_length=(5),blank=True)


class Comment(models.Model):
body = models.TextField()
author = models.CharField(max_length=128)
Expand Down
17 changes: 12 additions & 5 deletions blogproject/blogapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.views.generic import ListView
from django.views.generic import CreateView, UpdateView
from django.contrib import messages
from .models import Post, Comment, Category
from .models import Post, Comment, Category,PostLiked
from django.contrib.auth import authenticate, login, logout

def MainView(request):
Expand Down Expand Up @@ -36,11 +36,18 @@ def PostDetailView(request, pk):
return redirect('compapp:NoAccess')

def LikePost(request, pk):
user = request.user
post = Post.objects.get(pk=pk)
post.likes = post.likes + 1
post.save()
messages.info(request, 'I\'m glad you liked the article. Thank you for reading!')
return redirect('blog:PostDetailView', pk)
postlike = PostLiked.objects.filter(name=user,post_id=pk).exists()
if postlike:
return redirect('blog:PostDetailView', pk)
else:
likeduser = PostLiked.objects.create(name=user,post_id=pk)
likeduser.save()
post.likes = post.likes + 1
post.save()
messages.info(request, 'I\'m glad you liked the article. Thank you for reading!')
return redirect('blog:PostDetailView', pk)

@method_decorator(login_required(), 'dispatch')
class PostCreateView(CreateView):
Expand Down