diff --git a/apps/news/migrations/0035_news_views_count.py b/apps/news/migrations/0035_news_views_count.py new file mode 100644 index 00000000..6b94f737 --- /dev/null +++ b/apps/news/migrations/0035_news_views_count.py @@ -0,0 +1,20 @@ +# Generated by Django 2.2.4 on 2019-11-14 20:43 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('rating', '0004_auto_20191114_2041'), + ('news', '0034_merge_20191030_1714'), + ] + + operations = [ + migrations.AddField( + model_name='news', + name='views_count', + field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='rating.ViewCount'), + ), + ] diff --git a/apps/news/models.py b/apps/news/models.py index d52d04b8..27152182 100644 --- a/apps/news/models.py +++ b/apps/news/models.py @@ -37,7 +37,8 @@ class NewsQuerySet(TranslationQuerysetMixin): return self.order_by('-start') def rating_value(self): - return self.annotate(rating=models.Count('ratings__ip', distinct=True)) + # return self.annotate(rating=models.Count('ratings__ip', distinct=True)) + return self.annotate(rating='views_count__count') # TODO: совместить новый и старый функционал def with_base_related(self): """Return qs with related objects.""" @@ -182,7 +183,7 @@ class News(BaseAttributes, TranslatedFieldsMixin): tags = models.ManyToManyField('tag.Tag', related_name='news', verbose_name=_('Tags')) gallery = models.ManyToManyField('gallery.Image', through='news.NewsGallery') - views_count = generic.GenericRelation(ViewCount, blank=True, null=True) + views_count = models.OneToOneField('rating.ViewCount', blank=True, null=True, on_delete=models.SET_NULL) ratings = generic.GenericRelation(Rating) favorites = generic.GenericRelation(to='favorites.Favorites') agenda = models.ForeignKey('news.Agenda', blank=True, null=True, diff --git a/apps/rating/migrations/0004_auto_20191114_2041.py b/apps/rating/migrations/0004_auto_20191114_2041.py new file mode 100644 index 00000000..c8af8c71 --- /dev/null +++ b/apps/rating/migrations/0004_auto_20191114_2041.py @@ -0,0 +1,25 @@ +# Generated by Django 2.2.4 on 2019-11-14 20:41 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('rating', '0003_viewcount'), + ] + + operations = [ + migrations.AlterUniqueTogether( + name='viewcount', + unique_together=set(), + ), + migrations.RemoveField( + model_name='viewcount', + name='content_type', + ), + migrations.RemoveField( + model_name='viewcount', + name='object_id', + ), + ] diff --git a/apps/rating/models.py b/apps/rating/models.py index 7a49857e..5db8332e 100644 --- a/apps/rating/models.py +++ b/apps/rating/models.py @@ -23,18 +23,4 @@ class Rating(models.Model): class ViewCount(models.Model): - content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) - object_id = models.PositiveIntegerField() - content_object = GenericForeignKey('content_type', 'object_id') count = models.IntegerField() - - class Meta: - unique_together = ('object_id', 'content_type') - - @property - def name(self): - # Check if Generic obj has name or title - if hasattr(self.content_object, 'name'): - return self.content_object.name - if hasattr(self.content_object, 'title'): - return self.content_object.title_translated diff --git a/apps/rating/transfer_data.py b/apps/rating/transfer_data.py index 7ae4c289..f7875bfe 100644 --- a/apps/rating/transfer_data.py +++ b/apps/rating/transfer_data.py @@ -17,10 +17,13 @@ def transfer_news_view_count(): continue view_count = ViewCount.objects.create( - count=mysql_views_count.count, - content_object=news_object + count=mysql_views_count.count ) + news_object.views_count = view_count + news_object.save() + + data_types = { "rating_count": [transfer_news_view_count] }