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

3
from rest_framework import viewsets, mixins
4
from djangorestframework_fsm.viewset_mixins import get_drf_fsm_mixin
luxcem's avatar
luxcem committed
5 6

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

9 10 11
from .serializers import ArticleSerializer

ArticleMixin = get_drf_fsm_mixin(Article, fieldname='status')
luxcem's avatar
luxcem committed
12

13

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

Okhin's avatar
Okhin committed
23 24 25 26 27 28 29 30 31 32
    You can add query parameters to do some filtering, Those are :
        q="search terms" # to do a plaintext search into the title and extract
                         # of articles
        archive="true|[false]|both" # to filter on the archive flag or to
                                    # disable filtering on this flag.
        speak="true|false|[both]" # to filter on the speak flag, or to disable
                                  # filtering on this flag.
        quote="true|false|[both]" # to filter on the quote flag, or to disable
                                  # filtering on this flag.

33 34 35
    list:
    List all known articles in database.

Okhin's avatar
Okhin committed
36 37
    permissions ```None```

38 39
    create:
    Create an article from its URL and inserts it with the
40 41 42
    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
43 44

    permissions ```None```
45 46

    read:
Okhin's avatar
Okhin committed
47 48 49
    Get a specific article by id.

    permissions ```None```
50 51

    update:
Okhin's avatar
Okhin committed
52 53 54
    Change the content of an article.

    permissions ```rp.can_edit```
55 56

    partial_update:
Okhin's avatar
Okhin committed
57 58 59
    Change only some fields of an article.

    permissions ```rp.can_edit```
60 61

    downvote:
Okhin's avatar
Okhin committed
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 88 89 90 91 92 93 94 95 96 97 98
    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```
99
    """
luxcem's avatar
luxcem committed
100 101
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer
102

103

104 105 106 107 108
class ArticleSearch(viewsets.ModelViewSet, mixins.ListModelMixin):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer

    def list(self, request, *args, **kwargs):
109 110
        self.queryset = self.queryset.filter(Q(title__icontains=kwargs['search_keywords'])
                                             | Q(extracts__icontains=kwargs['search_keywords']))
111
        return super().list(request, args, kwargs)
112

113

114 115 116 117 118
class ArticleTag(viewsets.ModelViewSet, mixins.ListModelMixin):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer

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