views.py 2.85 KB
Newer Older
1
2
from django.db.models import Q

Thibaut Broggi's avatar
Thibaut Broggi committed
3
from rest_framework import viewsets, mixins
luxcem's avatar
luxcem committed
4
5
6
7

from rp.models import Article
from .serializers import ArticleSerializer

8
9
from .mixins import get_viewset_transition_actions_mixin
ArticleMixin = get_viewset_transition_actions_mixin(Article)
luxcem's avatar
luxcem committed
10

11
12

class ArticleViewSet(ArticleMixin, viewsets.ModelViewSet):
13
    """
Okhin's avatar
Okhin committed
14
15
16
17
18
19
20
    articles:
    This viewset describes all the method usable on the API
    using Article.

    The RequestBody is only needed for the create, update and partial_update
    functions.

21
22
23
    list:
    List all known articles in database.

Okhin's avatar
Okhin committed
24
25
    permissions ```None```

26
27
    create:
    Create an article from its URL and inserts it with the
Okhin's avatar
Okhin committed
28
29
30
    status _NEW_.

    permissions ```None```
31
32

    read:
Okhin's avatar
Okhin committed
33
34
35
    Get a specific article by id.

    permissions ```None```
36
37

    update:
Okhin's avatar
Okhin committed
38
39
40
    Change the content of an article.

    permissions ```rp.can_edit```
41
42

    partial_update:
Okhin's avatar
Okhin committed
43
44
45
    Change only some fields of an article.

    permissions ```rp.can_edit```
46
47

    downvote:
Okhin's avatar
Okhin committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    Decrement the score of an article by one. If the score goes below the
    ```ARTICLE_SCORE_THRESHOLD```, the article will stay in _DRAFT_ state.

    permissions ```rp.can_vote```

    upvote:
    Increment the score of an article by one. It it goes above the
    ```ARTICLE_SCORE_THRESHOLD```, its state will change to _DRAFT_.

    permissions ```rp.can_vote```

    recover:
    Force an Article status at _DRAFT_. It is used to recover _REJECTED_
    articles or ones with a score below ```ARTICLE_SCORE_THRESHOLD```.

    permissions ```rp.can_change_status```

    publish:
    Change the state of a _DRAFT_ article to _PUBLISHED_

    permissions ```rp.can_change_status```

    reject:
    Force an article status to _REJECTED_. It can only be brought back with
    the recover function.

    permissions ```rp.can_change_status```

    set_priority:
    Set the boolean priority of an article to True.

    permissions ```rp.can_change_priority```

    unset_priority:
    Set the boolean priority of an article to False.

    permissions ```rp.can_change_priority```
85
    """
luxcem's avatar
luxcem committed
86
87
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer
Thibaut Broggi's avatar
Thibaut Broggi committed
88

89

Thibaut Broggi's avatar
Thibaut Broggi committed
90
91
92
93
94
class ArticleSearch(viewsets.ModelViewSet, mixins.ListModelMixin):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer

    def list(self, request, *args, **kwargs):
95
96
        self.queryset = self.queryset.filter(Q(title__icontains=kwargs['search_keywords'])
                                             | Q(extracts__icontains=kwargs['search_keywords']))
Thibaut Broggi's avatar
Thibaut Broggi committed
97
        return super().list(request, args, kwargs)
98

99

100
101
102
103
104
105
106
class ArticleTag(viewsets.ModelViewSet, mixins.ListModelMixin):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer

    def list(self, request, *args, **kwargs):
        self.queryset = self.queryset.filter(tags__name__in=[kwargs['filter_tag']])
        return super().list(request, args, kwargs)