Filter recipes news

This commit is contained in:
Kuroshini 2019-10-16 14:11:07 +03:00
parent 995d55302a
commit d3ab910fba
2 changed files with 23 additions and 4 deletions

View File

@ -1,14 +1,21 @@
"""Filters from application News""" """Filters from application News"""
import django_filters from django_filters import rest_framework as filters
from django.utils.translation import gettext_lazy as _
from news import models from news import models
class NewsListFilterSet(django_filters.FilterSet): class NewsListFilterSet(filters.FilterSet):
"""FilterSet for News list""" """FilterSet for News list"""
is_highlighted = django_filters.BooleanFilter() is_highlighted = filters.BooleanFilter()
title = django_filters.CharFilter(method='by_title') title = filters.CharFilter(method='by_title')
tag_group = filters.ChoiceFilter(
choices=(
(models.News.RECIPES_TAG_VALUE, _('Recipes')),
),
method='by_tag_group'
)
class Meta: class Meta:
"""Meta class""" """Meta class"""
@ -16,8 +23,14 @@ class NewsListFilterSet(django_filters.FilterSet):
fields = ( fields = (
'title', 'title',
'is_highlighted', 'is_highlighted',
'tag_group',
) )
def by_tag_group(self, queryset, name, value):
if value == models.News.RECIPES_TAG_VALUE:
queryset = queryset.recipe_news()
return queryset
def by_title(self, queryset, name, value): def by_title(self, queryset, name, value):
"""Crappy search by title according to locale""" """Crappy search by title according to locale"""
if value: if value:

View File

@ -53,6 +53,10 @@ class NewsQuerySet(TranslationQuerysetMixin):
"""Filter collection by country code.""" """Filter collection by country code."""
return self.filter(country__code=code) return self.filter(country__code=code)
def recipe_news(self):
"""Returns news with tag 'cook' qs."""
return self.filter(tags__value=News.RECIPES_TAG_VALUE)
def published(self): def published(self):
"""Return only published news""" """Return only published news"""
now = timezone.now() now = timezone.now()
@ -127,6 +131,8 @@ class News(BaseAttributes, TranslatedFieldsMixin):
(PUBLISHED_EXCLUSIVE, _('Published exclusive')), (PUBLISHED_EXCLUSIVE, _('Published exclusive')),
) )
RECIPES_TAG_VALUE = 'cook'
old_id = models.PositiveIntegerField(_('old id'), blank=True, null=True, default=None) old_id = models.PositiveIntegerField(_('old id'), blank=True, null=True, default=None)
news_type = models.ForeignKey(NewsType, on_delete=models.PROTECT, news_type = models.ForeignKey(NewsType, on_delete=models.PROTECT,
verbose_name=_('news type')) verbose_name=_('news type'))