views.py 2.15 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
        article.save()
21
22
23
24
25
        return self.response_serialized_object(article)

    @detail_route(methods=["post"], url_path="reject")
    def reject(self, request, pk=None):
        article = self.get_object()
26
27
28
29
30
31
32
33
        article.reject()
        article.save()
        return self.response_serialized_object(article)

    @detail_route(methods=["post"], url_path="recover")
    def recover(self, request, pk=None):
        article = self.get_object()
        article.recover()
34
35
36
37
38
39
        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
40
        article.upvote(user_object=request.user.username)
41
42
43
44
45
        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
46
        article.downvote(user_object=request.user.username)
47
        return self.response_serialized_object(article)
48
49
50
51

    @detail_route(methods=["post"], url_path="priority/on")
    def priority_on(self, request, pk=None, priority=True):
        article = self.get_object()
52
        article.set_priority(True)
53
54
55
56
57
58
        article.save()
        return self.response_serialized_object(article)

    @detail_route(methods=["post"], url_path="priority/off")
    def priority_off(self, request, pk=None, priority=True):
        article = self.get_object()
59
        article.set_priority(False)
60
61
        article.save()
        return self.response_serialized_object(article)