news publication date & time are separate now

This commit is contained in:
Kuroshini 2019-12-18 18:58:55 +03:00
parent 62bee3156c
commit 6de33d82b0
4 changed files with 61 additions and 3 deletions

View File

@ -0,0 +1,38 @@
# Generated by Django 2.2.7 on 2019-12-18 14:37
from django.db import migrations, models
def fill_publication_date_and_time(apps, schema_editor):
News = apps.get_model('news', 'News')
for news in News.objects.all():
if news.start is not None:
news.publication_date = news.start.date()
news.publication_time = news.start.time()
news.save()
class Migration(migrations.Migration):
dependencies = [
('news', '0045_news_must_of_the_week'),
]
operations = [
migrations.AddField(
model_name='news',
name='publication_date',
field=models.DateField(blank=True, help_text='date since when news item is published', null=True, verbose_name='News publication date'),
),
migrations.AddField(
model_name='news',
name='publication_time',
field=models.TimeField(blank=True, help_text='time since when news item is published', null=True, verbose_name='News publication time'),
),
migrations.AlterField(
model_name='news',
name='must_of_the_week',
field=models.BooleanField(default=False, verbose_name='Show in the carousel'),
),
migrations.RunPython(fill_publication_date_and_time, migrations.RunPython.noop),
]

View File

@ -15,6 +15,7 @@ from utils.models import (BaseAttributes, TJSONField, TranslatedFieldsMixin, Has
ProjectBaseMixin, GalleryModelMixin, IntermediateGalleryModelMixin,
FavoritesMixin)
from utils.querysets import TranslationQuerysetMixin
from datetime import datetime
class Agenda(ProjectBaseMixin, TranslatedFieldsMixin):
@ -99,9 +100,13 @@ class NewsQuerySet(TranslationQuerysetMixin):
def published(self):
"""Return only published news"""
now = timezone.now()
return self.filter(models.Q(models.Q(end__gte=now) |
date_now = now.date()
time_now = now.time()
return self.exclude(models.Q(publication_date__isnull=True) | models.Q(publication_time__isnull=True)). \
filter(models.Q(models.Q(end__gte=now) |
models.Q(end__isnull=True)),
state__in=self.model.PUBLISHED_STATES, start__lte=now)
state__in=self.model.PUBLISHED_STATES, publication_date__lte=date_now,
publication_time__lte=time_now)
# todo: filter by best score
# todo: filter by country?
@ -187,6 +192,10 @@ class News(GalleryModelMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixi
help_text='{"en-GB": true, "fr-FR": false}')
start = models.DateTimeField(blank=True, null=True, default=None,
verbose_name=_('Start'))
publication_date = models.DateField(blank=True, null=True, verbose_name=_('News publication date'),
help_text=_('date since when news item is published'))
publication_time = models.TimeField(blank=True, null=True, verbose_name=_('News publication time'),
help_text=_('time since when news item is published'))
end = models.DateTimeField(blank=True, null=True, default=None,
verbose_name=_('End'))
slugs = HStoreField(null=True, blank=True, default=dict,
@ -244,6 +253,14 @@ class News(GalleryModelMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixi
self.duplication_date = timezone.now()
self.save()
@property
def publication_datetime(self):
"""Represents datetime object combined from `publication_date` & `publication_time` fields"""
try:
return datetime.combine(date=self.publication_date, time=self.publication_time)
except TypeError:
return None
@property
def duplicates(self):
"""Duplicates for this news item excluding same country code labeled"""

View File

@ -128,6 +128,7 @@ class NewsDetailSerializer(NewsBaseSerializer):
state_display = serializers.CharField(source='get_state_display',
read_only=True)
gallery = ImageBaseSerializer(read_only=True, source='crop_gallery', many=True)
start = serializers.DateTimeField(source='publication_datetime', read_only=True)
class Meta(NewsBaseSerializer.Meta):
"""Meta class."""
@ -186,6 +187,8 @@ class NewsBackOfficeBaseSerializer(NewsBaseSerializer):
'is_published',
'duplication_date',
'must_of_the_week',
'publication_date',
'publication_time',
)
extra_kwargs = {
'backoffice_title': {'allow_null': False},

View File

@ -45,7 +45,7 @@ class NewsDocument(Document):
},
multi=True)
favorites_for_users = fields.ListField(field=fields.IntegerField())
start = fields.DateField(attr='start')
start = fields.DateField(attr='publication_datetime')
has_any_desc_active = fields.BooleanField()
def prepare_slugs(self, instance):