diff --git a/apps/news/migrations/0042_news_duplication_date.py b/apps/news/migrations/0042_news_duplication_date.py new file mode 100644 index 00000000..61a258d3 --- /dev/null +++ b/apps/news/migrations/0042_news_duplication_date.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.7 on 2019-12-12 13:47 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('news', '0041_auto_20191211_1528'), + ] + + operations = [ + migrations.AddField( + model_name='news', + name='duplication_date', + field=models.DateTimeField(blank=True, default=None, null=True, verbose_name='Duplication datetime'), + ), + ] diff --git a/apps/news/models.py b/apps/news/models.py index b4e1b5d5..6ebdca76 100644 --- a/apps/news/models.py +++ b/apps/news/models.py @@ -211,6 +211,8 @@ class News(GalleryModelMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixi verbose_name=_('banner')) site = models.ForeignKey('main.SiteSettings', blank=True, null=True, on_delete=models.SET_NULL, verbose_name=_('site settings')) + duplication_date = models.DateTimeField(blank=True, null=True, default=None, + verbose_name=_('Duplication datetime')) objects = NewsQuerySet.as_manager() class Meta: @@ -220,7 +222,16 @@ class News(GalleryModelMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixi verbose_name_plural = _('news') def __str__(self): - return f'news: {self.slug}' + return f'news: {next(iter(self.slugs.values()))}' + + def create_duplicate(self, new_country, view_count_model): + self.pk = None + self.state = self.WAITING + 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 + self.duplication_date = timezone.now() + self.save() @property def is_publish(self): diff --git a/apps/news/serializers.py b/apps/news/serializers.py index 896ff420..5b9a8162 100644 --- a/apps/news/serializers.py +++ b/apps/news/serializers.py @@ -183,9 +183,11 @@ class NewsBackOfficeBaseSerializer(NewsBaseSerializer): 'subtitle', 'slugs', 'is_published', + 'duplication_date', ) extra_kwargs = { 'backoffice_title': {'allow_null': False}, + 'duplication_date': {'read_only': True}, } def create(self, validated_data): @@ -356,11 +358,6 @@ class NewsCloneCreateSerializer(NewsBackOfficeBaseSerializer, instance = get_object_or_404(models.News, pk=kwargs['pk']) new_country = get_object_or_404(location_models.Country, code=kwargs['country_code']) view_count_model = rating_models.ViewCount.objects.create(count=0) - instance.pk = None - instance.state = models.News.WAITING - instance.slugs = {locale: f'{slug}-{kwargs["country_code"]}'for locale, slug in instance.slugs.items()} - instance.country = new_country - instance.views_count = view_count_model - instance.save() + instance.create_duplicate(new_country, view_count_model) return instance