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
17
18
19
20

class ArticleSearch(viewsets.ModelViewSet, mixins.ListModelMixin):
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer

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

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)