change news states

This commit is contained in:
Kuroshini 2020-01-28 17:50:38 +03:00
parent a0aad97a4d
commit 4604c81ede
3 changed files with 34 additions and 11 deletions

View File

@ -0,0 +1,18 @@
# Generated by Django 2.2.7 on 2020-01-28 14:31
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('news', '0052_auto_20200121_0940'),
]
operations = [
migrations.AlterField(
model_name='news',
name='state',
field=models.PositiveSmallIntegerField(choices=[(0, 'remove'), (1, 'hidden'), (2, 'published'), (3, 'not published')], default=3, verbose_name='State'),
),
]

View File

@ -101,6 +101,10 @@ class NewsQuerySet(TranslationQuerysetMixin):
"""Return qs with related objects."""
return self.select_related('created_by', 'agenda', 'banner')
def visible(self):
"""Narrows qs by excluding invisible for API (at all) news"""
return self.exclude(state=self.model.REMOVE)
def by_type(self, news_type):
"""Filter News by type"""
return self.filter(news_type__name=news_type)
@ -259,18 +263,18 @@ class News(GalleryMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixin,
)
# STATE CHOICES
WAITING = 0
REMOVE = 0
HIDDEN = 1
PUBLISHED = 2
PUBLISHED_EXCLUSIVE = 3
UNPUBLISHED = 3
PUBLISHED_STATES = [PUBLISHED, PUBLISHED_EXCLUSIVE]
PUBLISHED_STATES = [PUBLISHED]
STATE_CHOICES = (
(WAITING, _('Waiting')),
(HIDDEN, _('Hidden')),
(PUBLISHED, _('Published')),
(PUBLISHED_EXCLUSIVE, _('Published exclusive')),
(REMOVE, _('remove')), # simply stored in DB news. not shown anywhere
(HIDDEN, _('hidden')), # not shown in api/web or api/mobile
(PUBLISHED, _('published')), # shown everywhere
(UNPUBLISHED, _('not published')), # newly created news
)
INTERNATIONAL_TAG_VALUE = 'international'
@ -302,7 +306,7 @@ class News(GalleryMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixin,
slugs = HStoreField(null=True, blank=True, default=dict,
verbose_name=_('Slugs for current news obj'),
help_text='{"en-GB":"some slug"}')
state = models.PositiveSmallIntegerField(default=WAITING, choices=STATE_CHOICES,
state = models.PositiveSmallIntegerField(default=UNPUBLISHED, choices=STATE_CHOICES,
verbose_name=_('State'))
is_highlighted = models.BooleanField(default=False,
verbose_name=_('Is highlighted'))
@ -347,7 +351,7 @@ class News(GalleryMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixin,
def create_duplicate(self, new_country, view_count_model):
self.pk = None
self.state = self.WAITING
self.state = self.UNPUBLISHED
self.slugs = {locale: f'{slug}-{new_country.code}' for locale, slug in self.slugs.items()}
self.country = new_country
self.views_count = view_count_model

View File

@ -11,7 +11,7 @@ from news import filters, models, serializers
from rating.tasks import add_rating
from utils.permissions import IsCountryAdmin, IsContentPageManager
from utils.views import CreateDestroyGalleryViewMixin, FavoritesCreateDestroyMixinView, CarouselCreateDestroyMixinView
from utils.serializers import ImageBaseSerializer, EmptySerializer
from utils.serializers import ImageBaseSerializer
class NewsMixinView:
@ -24,6 +24,7 @@ class NewsMixinView:
"""Override get_queryset method."""
qs = models.News.objects.published() \
.with_base_related() \
.visible() \
.annotate_in_favorites(self.request.user) \
.order_by('-is_highlighted', '-publication_date', '-publication_time')
@ -41,7 +42,7 @@ class NewsMixinView:
return qs
def get_object(self):
instance = self.get_queryset().filter(
instance = self.get_queryset().visible().with_base_related().filter(
slugs__values__contains=[self.kwargs['slug']]
).first()