views.py 1.14 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):
luxcem's avatar
luxcem committed
13
14
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer
Thibaut Broggi's avatar
Thibaut Broggi committed
15

16

Thibaut Broggi's avatar
Thibaut Broggi committed
17
18
19
20
21
class ArticleSearch(viewsets.ModelViewSet, mixins.ListModelMixin):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer

    def list(self, request, *args, **kwargs):
22
23
        self.queryset = self.queryset.filter(Q(title__icontains=kwargs['search_keywords'])
                                             | Q(extracts__icontains=kwargs['search_keywords']))
Thibaut Broggi's avatar
Thibaut Broggi committed
24
        return super().list(request, args, kwargs)
25

26

27
28
29
30
31
32
33
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)