views.py 1.37 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

    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()
19
        article.publish()
20
21
22
23
24
25
26
27
28
29
30
31
        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()
luxcem's avatar
luxcem committed
32
        article.upvote(user_object=request.user.username)
33
34
35
36
37
        return self.response_serialized_object(article)

    @detail_route(methods=["post"], url_path="downvote")
    def downvote(self, request, pk=None):
        article = self.get_object()
luxcem's avatar
luxcem committed
38
        article.downvote(user_object=request.user.username)
39
        return self.response_serialized_object(article)