views.py 1.5 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
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    """
    list:
    List all known articles in database.

    create:
    Create an article from its URL and inserts it with the
    status NEW

    read:
    Get a specific article by id

    update:
    Change the content of an article

    partial_update:
    Change only some fields of an article

    downvote:
    Decrement the score of an article by one.
    """
luxcem's avatar
luxcem committed
33
34
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer
Thibaut Broggi's avatar
Thibaut Broggi committed
35

36

Thibaut Broggi's avatar
Thibaut Broggi committed
37
38
39
40
41
class ArticleSearch(viewsets.ModelViewSet, mixins.ListModelMixin):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer

    def list(self, request, *args, **kwargs):
42
43
        self.queryset = self.queryset.filter(Q(title__icontains=kwargs['search_keywords'])
                                             | Q(extracts__icontains=kwargs['search_keywords']))
Thibaut Broggi's avatar
Thibaut Broggi committed
44
        return super().list(request, args, kwargs)
45

46

47
48
49
50
51
52
53
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)