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

from rp.models import Article
from .serializers import ArticleSerializer
7
from rp.views.articles import ArticleFilterMixin
luxcem's avatar
luxcem committed
8

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

12

13
class ArticleViewSet(ArticleMixin, ArticleFilterMixin, viewsets.ModelViewSet):
14
    """
Okhin's avatar
Okhin committed
15
16
17
18
19
20
21
    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.

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

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

27
28
    create:
    Create an article from its URL and inserts it with the
29
30
31
    status _NEW_. If the article already exists, it is upvoted
    by one, and the tags are merged (in case new tags are given
    to this command).
Okhin's avatar
Okhin committed
32
33

    permissions ```None```
34
35

    read:
Okhin's avatar
Okhin committed
36
37
38
    Get a specific article by id.

    permissions ```None```
39
40

    update:
Okhin's avatar
Okhin committed
41
42
43
    Change the content of an article.

    permissions ```rp.can_edit```
44
45

    partial_update:
Okhin's avatar
Okhin committed
46
47
48
    Change only some fields of an article.

    permissions ```rp.can_edit```
49
50

    downvote:
Okhin's avatar
Okhin committed
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
85
86
87
    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```
88
    """
luxcem's avatar
luxcem committed
89
90
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer
Thibaut Broggi's avatar
Thibaut Broggi committed
91

92

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

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

102

103
104
105
106
107
108
109
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)