Update to onetoone
Update rating annotation Add migrations
This commit is contained in:
parent
75307dbf73
commit
726610d147
20
apps/news/migrations/0035_news_views_count.py
Normal file
20
apps/news/migrations/0035_news_views_count.py
Normal file
|
|
@ -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'),
|
||||
),
|
||||
]
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
25
apps/rating/migrations/0004_auto_20191114_2041.py
Normal file
25
apps/rating/migrations/0004_auto_20191114_2041.py
Normal file
|
|
@ -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',
|
||||
),
|
||||
]
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user