views.py 1.4 KB
Newer Older
luxcem's avatar
luxcem committed
1
from rest_framework import viewsets
2
3
from rest_framework.decorators import detail_route
from rest_framework.response import Response
luxcem's avatar
luxcem committed
4
5
6
7
8
9
10
11

from rp.models import Article
from .serializers import ArticleSerializer


class ArticleViewSet(viewsets.ModelViewSet):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

    def response_serialized_object(self, object):
        return Response(self.serializer_class(object).data)

    @detail_route(methods=["post"], url_path="publish")
    def publish(self, request, pk=None):
        article = self.get_object()
        article.status = "PUBLISHED"
        article.save()
        return self.response_serialized_object(article)

    @detail_route(methods=["post"], url_path="reject")
    def reject(self, request, pk=None):
        article = self.get_object()
        article.status = "REJECTED"
        article.save()
        return self.response_serialized_object(article)

    @detail_route(methods=["post"], url_path="upvote")
    def upvote(self, request, pk=None):
        article = self.get_object()
        article.upvote(username=request.user.username)
        return self.response_serialized_object(article)

    @detail_route(methods=["post"], url_path="downvote")
    def downvote(self, request, pk=None):
        article = self.get_object()
        article.downvote(username=request.user.username)
        return self.response_serialized_object(article)