Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
Luis García
collabmap
Commits
48b2fc25
Commit
48b2fc25
authored
Mar 12, 2020
by
Luis García
Browse files
Fixes
parent
f2479511
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
6 deletions
+17
-6
blog/views/api.py
blog/views/api.py
+17
-6
No files found.
blog/views/api.py
View file @
48b2fc25
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.permissions
import
IsAuthenticated
from
rest_framework_extensions.mixins
import
NestedViewSetMixin
from
api.mixins
import
OwnerFieldMixin
,
DestroyToInvisibleMixin
from
api.permissions
import
IsSuperOrReadOnly
,
IsAuthOrReadOnly
,
IsStaffOrReadOnly
#from blog.models import Post, Comment, Reply, Category, Tag, PostTag
from
blog.models
import
Post
,
Category
,
Tag
,
PostTag
...
...
@@ -11,7 +13,7 @@ from blog.serializers import PostSerializer, CategorySerializer, TagSerializer,
############################################################
#REST API for Post
############################################################
class
PostViewSet
(
N
est
edViewSet
Mixin
,
ModelViewSet
):
class
PostViewSet
(
D
est
royToInvisibleMixin
,
OwnerField
Mixin
,
ModelViewSet
):
#Everyone can read, just staff user can modificate
permission_classes
=
(
IsStaffOrReadOnly
,)
queryset
=
Post
.
objects
.
filter
(
visible
=
True
)
...
...
@@ -20,6 +22,15 @@ class PostViewSet(NestedViewSetMixin, ModelViewSet):
filter_backends
=
(
DjangoFilterBackend
,)
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
...
...
@@ -64,7 +75,7 @@ class ReplyViewSet(NestedViewSetMixin, ModelViewSet):
############################################################
#REST API for Category
############################################################
class
CategoryViewSet
(
N
est
edViewSet
Mixin
,
ModelViewSet
):
class
CategoryViewSet
(
D
est
royToInvisible
Mixin
,
ModelViewSet
):
permission_classes
=
(
IsStaffOrReadOnly
,
)
queryset
=
Category
.
objects
.
filter
(
visible
=
True
)
serializer_class
=
CategorySerializer
...
...
@@ -75,7 +86,7 @@ class CategoryViewSet(NestedViewSetMixin, ModelViewSet):
############################################################
#REST API for Tag
############################################################
class
TagViewSet
(
NestedViewSetMixin
,
ModelViewSet
):
class
TagViewSet
(
ModelViewSet
):
permission_classes
=
(
IsStaffOrReadOnly
,
)
queryset
=
Tag
.
objects
.
all
()
serializer_class
=
TagSerializer
...
...
@@ -86,7 +97,7 @@ class TagViewSet(NestedViewSetMixin, ModelViewSet):
############################################################
#REST API for PostTag
############################################################
class
PostTagViewSet
(
NestedViewSetMixin
,
ModelViewSet
):
class
PostTagViewSet
(
ModelViewSet
):
permission_classes
=
(
IsStaffOrReadOnly
,
)
queryset
=
PostTag
.
objects
.
all
()
serializer_class
=
PostTagSerializer
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment