Filter recipes news
This commit is contained in:
parent
995d55302a
commit
d3ab910fba
|
|
@ -1,14 +1,21 @@
|
|||
"""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
|
||||
|
||||
|
||||
class NewsListFilterSet(django_filters.FilterSet):
|
||||
class NewsListFilterSet(filters.FilterSet):
|
||||
"""FilterSet for News list"""
|
||||
|
||||
is_highlighted = django_filters.BooleanFilter()
|
||||
title = django_filters.CharFilter(method='by_title')
|
||||
is_highlighted = filters.BooleanFilter()
|
||||
title = filters.CharFilter(method='by_title')
|
||||
tag_group = filters.ChoiceFilter(
|
||||
choices=(
|
||||
(models.News.RECIPES_TAG_VALUE, _('Recipes')),
|
||||
),
|
||||
method='by_tag_group'
|
||||
)
|
||||
|
||||
class Meta:
|
||||
"""Meta class"""
|
||||
|
|
@ -16,8 +23,14 @@ class NewsListFilterSet(django_filters.FilterSet):
|
|||
fields = (
|
||||
'title',
|
||||
'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):
|
||||
"""Crappy search by title according to locale"""
|
||||
if value:
|
||||
|
|
|
|||
|
|
@ -53,6 +53,10 @@ class NewsQuerySet(TranslationQuerysetMixin):
|
|||
"""Filter collection by country 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):
|
||||
"""Return only published news"""
|
||||
now = timezone.now()
|
||||
|
|
@ -127,6 +131,8 @@ class News(BaseAttributes, TranslatedFieldsMixin):
|
|||
(PUBLISHED_EXCLUSIVE, _('Published exclusive')),
|
||||
)
|
||||
|
||||
RECIPES_TAG_VALUE = 'cook'
|
||||
|
||||
old_id = models.PositiveIntegerField(_('old id'), blank=True, null=True, default=None)
|
||||
news_type = models.ForeignKey(NewsType, on_delete=models.PROTECT,
|
||||
verbose_name=_('news type'))
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user