-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.py
82 lines (67 loc) · 4.3 KB
/
filters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import django_filters
from issue.models import Issue
from podcast.models import Podcast
from blog.models import BlogPost
CHOICES = (
('ascending', 'Ascending'),
('descending', 'Descending')
)
class IssueFilter(django_filters.FilterSet):
publishing_date__gte = django_filters.DateFilter(field_name='publishing_date',
lookup_expr='gte',
label='Publishing Date Start')
publishing_date__lte = django_filters.DateFilter(field_name='publishing_date',
lookup_expr='lte',
label='Publishing Date End')
name = django_filters.CharFilter(field_name='name', lookup_expr='icontains', label='Name')
subject = django_filters.ChoiceFilter(field_name='subject', lookup_expr='icontains', label='Subject')
publishing_date__year = django_filters.NumberFilter(field_name='publishing_date',
lookup_expr='year', label='Publishing Year')
ordering = django_filters.ChoiceFilter(label='Order by Views', choices=CHOICES, method='filter_by_order')
class Meta:
model = Issue
exclude = ('id', 'created', 'modified', 'raw_file', 'cover_image',
'authors', 'pages_number', 'is_issue',)
def filter_by_order(self, queryset, name, value):
expression = 'views_count' if value.lower() == 'ascending' else '-views_count'
return queryset.order_by(expression)
class PodcastFilter(django_filters.FilterSet):
publishing_date__gte = django_filters.DateFilter(field_name='publishing_date',
lookup_expr='gte',
label='Publishing Date Start')
publishing_date__lte = django_filters.DateFilter(field_name='publishing_date',
lookup_expr='lte',
label='Publishing Date End')
name = django_filters.CharFilter(field_name='name', lookup_expr='icontains', label='Name')
subject = django_filters.CharFilter(field_name='subject', lookup_expr='icontains', label='Subject')
publishing_date__year = django_filters.NumberFilter(field_name='publishing_date',
lookup_expr='year', label='Publishing Year')
ordering = django_filters.ChoiceFilter(label='Order by Views', choices=CHOICES, method='filter_by_order')
def filter_by_order(self, queryset, name, value):
expression = 'views_count' if value.lower() == 'ascending' else '-views_count'
return queryset.order_by(expression)
class Meta:
model = Podcast
exclude = ('id', 'created', 'modified', 'raw_file', 'cover_image',
'contributors', 'length')
class BlogFilter(django_filters.FilterSet):
posting_date__gte = django_filters.DateFilter(field_name='posting_date',
lookup_expr='gte',
label='Posting Date Start')
posting_date__lte = django_filters.DateFilter(field_name='posting_date',
lookup_expr='lte',
label='Posting Date End')
posting_date__year = django_filters.NumberFilter(field_name='posting_date',
lookup_expr='year', label='Posting Year')
subject = django_filters.CharFilter(field_name='subject', lookup_expr='icontains', label='Subject')
ordering = django_filters.ChoiceFilter(label='Order by Views', choices=CHOICES, method='filter_by_order')
contributor = django_filters.CharFilter(field_name="contributors", lookup_expr='icontains')
class Meta:
model = BlogPost
exclude = ('created', 'modified', 'post_content', 'contributors',
'reading_time',)
def filter_by_order(self, queryset, name, value):
expression = 'views_count' if value.lower() == 'ascending' else '-views_count'
return queryset.order_by(expression)
# TODO a method for searching for specific author (with details on the author) or getting most recent posts can
# be added