Commit 48b2fc25 authored by Luis García's avatar Luis García
Browse files

Fixes

parent f2479511
from django_filters.rest_framework import DjangoFilterBackend from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.response import Response as Http_Response
from rest_framework.viewsets import ModelViewSet from rest_framework.viewsets import ModelViewSet
from rest_framework.permissions import IsAuthenticated from api.mixins import OwnerFieldMixin, DestroyToInvisibleMixin
from rest_framework_extensions.mixins import NestedViewSetMixin
from api.permissions import IsSuperOrReadOnly, IsAuthOrReadOnly, IsStaffOrReadOnly from api.permissions import IsSuperOrReadOnly, IsAuthOrReadOnly, IsStaffOrReadOnly
#from blog.models import Post, Comment, Reply, Category, Tag, PostTag #from blog.models import Post, Comment, Reply, Category, Tag, PostTag
from blog.models import Post, Category, Tag, PostTag from blog.models import Post, Category, Tag, PostTag
...@@ -11,7 +13,7 @@ from blog.serializers import PostSerializer, CategorySerializer, TagSerializer, ...@@ -11,7 +13,7 @@ from blog.serializers import PostSerializer, CategorySerializer, TagSerializer,
############################################################ ############################################################
#REST API for Post #REST API for Post
############################################################ ############################################################
class PostViewSet(NestedViewSetMixin, ModelViewSet): class PostViewSet(DestroyToInvisibleMixin, OwnerFieldMixin, ModelViewSet):
#Everyone can read, just staff user can modificate #Everyone can read, just staff user can modificate
permission_classes = (IsStaffOrReadOnly,) permission_classes = (IsStaffOrReadOnly,)
queryset = Post.objects.filter(visible=True) queryset = Post.objects.filter(visible=True)
...@@ -20,6 +22,15 @@ class PostViewSet(NestedViewSetMixin, ModelViewSet): ...@@ -20,6 +22,15 @@ class PostViewSet(NestedViewSetMixin, ModelViewSet):
filter_backends = (DjangoFilterBackend,) filter_backends = (DjangoFilterBackend,)
filterset_fields = ('slug','category','tags__tag') filterset_fields = ('slug','category','tags__tag')
@action(detail=True, methods=['post'], permission_classes=[AllowAny])
def increase_rating(self, request, pk=None):
post = self.get_object()
data = {'rating': post.rating + 1}
serializer = self.get_serializer(post, data=data, partial=True)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
return Http_Response(serializer.data)
""" """
############################################################ ############################################################
#REST API for Comment #REST API for Comment
...@@ -64,7 +75,7 @@ class ReplyViewSet(NestedViewSetMixin, ModelViewSet): ...@@ -64,7 +75,7 @@ class ReplyViewSet(NestedViewSetMixin, ModelViewSet):
############################################################ ############################################################
#REST API for Category #REST API for Category
############################################################ ############################################################
class CategoryViewSet(NestedViewSetMixin, ModelViewSet): class CategoryViewSet(DestroyToInvisibleMixin, ModelViewSet):
permission_classes = (IsStaffOrReadOnly, ) permission_classes = (IsStaffOrReadOnly, )
queryset = Category.objects.filter(visible=True) queryset = Category.objects.filter(visible=True)
serializer_class = CategorySerializer serializer_class = CategorySerializer
...@@ -75,7 +86,7 @@ class CategoryViewSet(NestedViewSetMixin, ModelViewSet): ...@@ -75,7 +86,7 @@ class CategoryViewSet(NestedViewSetMixin, ModelViewSet):
############################################################ ############################################################
#REST API for Tag #REST API for Tag
############################################################ ############################################################
class TagViewSet(NestedViewSetMixin, ModelViewSet): class TagViewSet(ModelViewSet):
permission_classes = (IsStaffOrReadOnly, ) permission_classes = (IsStaffOrReadOnly, )
queryset = Tag.objects.all() queryset = Tag.objects.all()
serializer_class = TagSerializer serializer_class = TagSerializer
...@@ -86,7 +97,7 @@ class TagViewSet(NestedViewSetMixin, ModelViewSet): ...@@ -86,7 +97,7 @@ class TagViewSet(NestedViewSetMixin, ModelViewSet):
############################################################ ############################################################
#REST API for PostTag #REST API for PostTag
############################################################ ############################################################
class PostTagViewSet(NestedViewSetMixin, ModelViewSet): class PostTagViewSet(ModelViewSet):
permission_classes = (IsStaffOrReadOnly, ) permission_classes = (IsStaffOrReadOnly, )
queryset = PostTag.objects.all() queryset = PostTag.objects.all()
serializer_class = PostTagSerializer serializer_class = PostTagSerializer
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment