news publication date & time are separate now
This commit is contained in:
parent
62bee3156c
commit
6de33d82b0
38
apps/news/migrations/0046_auto_20191218_1437.py
Normal file
38
apps/news/migrations/0046_auto_20191218_1437.py
Normal 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),
|
||||||
|
]
|
||||||
|
|
@ -15,6 +15,7 @@ from utils.models import (BaseAttributes, TJSONField, TranslatedFieldsMixin, Has
|
||||||
ProjectBaseMixin, GalleryModelMixin, IntermediateGalleryModelMixin,
|
ProjectBaseMixin, GalleryModelMixin, IntermediateGalleryModelMixin,
|
||||||
FavoritesMixin)
|
FavoritesMixin)
|
||||||
from utils.querysets import TranslationQuerysetMixin
|
from utils.querysets import TranslationQuerysetMixin
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
class Agenda(ProjectBaseMixin, TranslatedFieldsMixin):
|
class Agenda(ProjectBaseMixin, TranslatedFieldsMixin):
|
||||||
|
|
@ -99,9 +100,13 @@ class NewsQuerySet(TranslationQuerysetMixin):
|
||||||
def published(self):
|
def published(self):
|
||||||
"""Return only published news"""
|
"""Return only published news"""
|
||||||
now = timezone.now()
|
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)),
|
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 best score
|
||||||
# todo: filter by country?
|
# todo: filter by country?
|
||||||
|
|
@ -187,6 +192,10 @@ class News(GalleryModelMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixi
|
||||||
help_text='{"en-GB": true, "fr-FR": false}')
|
help_text='{"en-GB": true, "fr-FR": false}')
|
||||||
start = models.DateTimeField(blank=True, null=True, default=None,
|
start = models.DateTimeField(blank=True, null=True, default=None,
|
||||||
verbose_name=_('Start'))
|
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,
|
end = models.DateTimeField(blank=True, null=True, default=None,
|
||||||
verbose_name=_('End'))
|
verbose_name=_('End'))
|
||||||
slugs = HStoreField(null=True, blank=True, default=dict,
|
slugs = HStoreField(null=True, blank=True, default=dict,
|
||||||
|
|
@ -244,6 +253,14 @@ class News(GalleryModelMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixi
|
||||||
self.duplication_date = timezone.now()
|
self.duplication_date = timezone.now()
|
||||||
self.save()
|
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
|
@property
|
||||||
def duplicates(self):
|
def duplicates(self):
|
||||||
"""Duplicates for this news item excluding same country code labeled"""
|
"""Duplicates for this news item excluding same country code labeled"""
|
||||||
|
|
|
||||||
|
|
@ -128,6 +128,7 @@ class NewsDetailSerializer(NewsBaseSerializer):
|
||||||
state_display = serializers.CharField(source='get_state_display',
|
state_display = serializers.CharField(source='get_state_display',
|
||||||
read_only=True)
|
read_only=True)
|
||||||
gallery = ImageBaseSerializer(read_only=True, source='crop_gallery', many=True)
|
gallery = ImageBaseSerializer(read_only=True, source='crop_gallery', many=True)
|
||||||
|
start = serializers.DateTimeField(source='publication_datetime', read_only=True)
|
||||||
|
|
||||||
class Meta(NewsBaseSerializer.Meta):
|
class Meta(NewsBaseSerializer.Meta):
|
||||||
"""Meta class."""
|
"""Meta class."""
|
||||||
|
|
@ -186,6 +187,8 @@ class NewsBackOfficeBaseSerializer(NewsBaseSerializer):
|
||||||
'is_published',
|
'is_published',
|
||||||
'duplication_date',
|
'duplication_date',
|
||||||
'must_of_the_week',
|
'must_of_the_week',
|
||||||
|
'publication_date',
|
||||||
|
'publication_time',
|
||||||
)
|
)
|
||||||
extra_kwargs = {
|
extra_kwargs = {
|
||||||
'backoffice_title': {'allow_null': False},
|
'backoffice_title': {'allow_null': False},
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ class NewsDocument(Document):
|
||||||
},
|
},
|
||||||
multi=True)
|
multi=True)
|
||||||
favorites_for_users = fields.ListField(field=fields.IntegerField())
|
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()
|
has_any_desc_active = fields.BooleanField()
|
||||||
|
|
||||||
def prepare_slugs(self, instance):
|
def prepare_slugs(self, instance):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user