views.py 1.04 KB
Newer Older
Thibaut Broggi's avatar
Thibaut Broggi committed
1
from rest_framework import viewsets, mixins
luxcem's avatar
luxcem committed
2
3
4
5

from rp.models import Article
from .serializers import ArticleSerializer

6
7
from .mixins import get_viewset_transition_actions_mixin
ArticleMixin = get_viewset_transition_actions_mixin(Article)
luxcem's avatar
luxcem committed
8

9
10

class ArticleViewSet(ArticleMixin, viewsets.ModelViewSet):
luxcem's avatar
luxcem committed
11
12
    queryset = Article.objects.all()
    serializer_class = ArticleSerializer
Thibaut Broggi's avatar
Thibaut Broggi committed
13
14
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):
        self.queryset = self.queryset.filter(title__icontains=kwargs['search_keywords'])
        return super().list(request, args, kwargs)
21
22
23
24
25
26
27
28
29

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

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