added fields to model News, refactored NewsList queryset, added filters to NewsList view
This commit is contained in:
parent
1d18d654e3
commit
1747525f0f
17
apps/news/filters.py
Normal file
17
apps/news/filters.py
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
"""Filters from application News"""
|
||||||
|
import django_filters
|
||||||
|
|
||||||
|
from news import models
|
||||||
|
|
||||||
|
|
||||||
|
class NewsListFilterSet(django_filters.FilterSet):
|
||||||
|
"""FilterSet for News list"""
|
||||||
|
|
||||||
|
is_highlighted = django_filters.BooleanFilter()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
"""Meta class"""
|
||||||
|
model = models.News
|
||||||
|
fields = (
|
||||||
|
'is_highlighted',
|
||||||
|
)
|
||||||
25
apps/news/migrations/0005_auto_20190823_1149.py
Normal file
25
apps/news/migrations/0005_auto_20190823_1149.py
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
# Generated by Django 2.2.4 on 2019-08-23 11:49
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import easy_thumbnails.fields
|
||||||
|
import utils.methods
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('news', '0004_auto_20190823_1034'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='news',
|
||||||
|
name='image',
|
||||||
|
field=easy_thumbnails.fields.ThumbnailerImageField(blank=True, default=None, null=True, upload_to=utils.methods.image_path, verbose_name='Image'),
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='news',
|
||||||
|
name='is_highlighted',
|
||||||
|
field=models.BooleanField(default=False, verbose_name='Is highlighted'),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -2,7 +2,8 @@ from django.contrib.postgres.fields import JSONField
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from utils.models import ProjectBaseMixin, BaseAttributes, LocaleManagerMixin
|
from utils.models import (ProjectBaseMixin, BaseAttributes,
|
||||||
|
LocaleManagerMixin, ImageMixin)
|
||||||
|
|
||||||
|
|
||||||
class NewsType(models.Model):
|
class NewsType(models.Model):
|
||||||
|
|
@ -36,7 +37,7 @@ class NewsQuerySet(models.QuerySet):
|
||||||
return self.filter(is_publish=True)
|
return self.filter(is_publish=True)
|
||||||
|
|
||||||
|
|
||||||
class News(BaseAttributes):
|
class News(ImageMixin, BaseAttributes):
|
||||||
"""News model."""
|
"""News model."""
|
||||||
news_type = models.ForeignKey(
|
news_type = models.ForeignKey(
|
||||||
NewsType, verbose_name=_('news type'), on_delete=models.CASCADE)
|
NewsType, verbose_name=_('news type'), on_delete=models.CASCADE)
|
||||||
|
|
@ -63,6 +64,8 @@ class News(BaseAttributes):
|
||||||
country = models.ForeignKey(
|
country = models.ForeignKey(
|
||||||
'location.Country', blank=True, null=True,
|
'location.Country', blank=True, null=True,
|
||||||
verbose_name=_('country'), on_delete=models.CASCADE)
|
verbose_name=_('country'), on_delete=models.CASCADE)
|
||||||
|
is_highlighted = models.BooleanField(
|
||||||
|
default=False, verbose_name=_('Is highlighted'))
|
||||||
# TODO: metadata_keys - описание ключей для динамического построения полей метаданных
|
# TODO: metadata_keys - описание ключей для динамического построения полей метаданных
|
||||||
# TODO: metadata_values - Описание значений для динамических полей из MetadataKeys
|
# TODO: metadata_values - Описание значений для динамических полей из MetadataKeys
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,8 @@ class NewsSerializer(NewsLocalizationMixinSerializer):
|
||||||
'end',
|
'end',
|
||||||
'playlist',
|
'playlist',
|
||||||
'address',
|
'address',
|
||||||
|
'is_highlighted',
|
||||||
|
'image',
|
||||||
# Localized fields
|
# Localized fields
|
||||||
'title_trans',
|
'title_trans',
|
||||||
'subtitle_trans',
|
'subtitle_trans',
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
from rest_framework import generics, permissions
|
from rest_framework import generics, permissions
|
||||||
|
|
||||||
|
from news import filters
|
||||||
from news.models import News, NewsType
|
from news.models import News, NewsType
|
||||||
from news.serializers import common as serializers
|
from news.serializers import common as serializers
|
||||||
from utils.views import (JWTGenericViewMixin,
|
from utils.views import (JWTGenericViewMixin,
|
||||||
|
|
@ -19,12 +20,14 @@ class NewsList(NewsViewMixin, JWTListAPIView):
|
||||||
"""News list view."""
|
"""News list view."""
|
||||||
permission_classes = (permissions.AllowAny, )
|
permission_classes = (permissions.AllowAny, )
|
||||||
serializer_class = serializers.NewsSerializer
|
serializer_class = serializers.NewsSerializer
|
||||||
|
filter_class = filters.NewsListFilterSet
|
||||||
|
|
||||||
def get_queryset(self, *args, **kwargs):
|
def get_queryset(self, *args, **kwargs):
|
||||||
"""Override get_queryset method"""
|
"""Override get_queryset method"""
|
||||||
return News.objects.annotate_localized_fields(locale=self.request.locale)\
|
return News.objects.annotate_localized_fields(locale=self.request.locale)\
|
||||||
.published()\
|
.published()\
|
||||||
.by_country_code(code=self.request.country_code)
|
.by_country_code(code=self.request.country_code)\
|
||||||
|
.order_by('-created')
|
||||||
|
|
||||||
|
|
||||||
class NewsCreate(generics.CreateAPIView):
|
class NewsCreate(generics.CreateAPIView):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user