Add rating.views_count transfer
This commit is contained in:
parent
322318a4f9
commit
4e72487558
|
|
@ -6,7 +6,7 @@ from django.utils import timezone
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from rest_framework.reverse import reverse
|
from rest_framework.reverse import reverse
|
||||||
|
|
||||||
from rating.models import Rating
|
from rating.models import Rating, ViewCount
|
||||||
from utils.models import BaseAttributes, TJSONField, TranslatedFieldsMixin, ProjectBaseMixin
|
from utils.models import BaseAttributes, TJSONField, TranslatedFieldsMixin, ProjectBaseMixin
|
||||||
from utils.querysets import TranslationQuerysetMixin
|
from utils.querysets import TranslationQuerysetMixin
|
||||||
|
|
||||||
|
|
@ -182,6 +182,7 @@ class News(BaseAttributes, TranslatedFieldsMixin):
|
||||||
tags = models.ManyToManyField('tag.Tag', related_name='news',
|
tags = models.ManyToManyField('tag.Tag', related_name='news',
|
||||||
verbose_name=_('Tags'))
|
verbose_name=_('Tags'))
|
||||||
gallery = models.ManyToManyField('gallery.Image', through='news.NewsGallery')
|
gallery = models.ManyToManyField('gallery.Image', through='news.NewsGallery')
|
||||||
|
views_count = generic.GenericRelation(ViewCount, blank=True, null=True)
|
||||||
ratings = generic.GenericRelation(Rating)
|
ratings = generic.GenericRelation(Rating)
|
||||||
favorites = generic.GenericRelation(to='favorites.Favorites')
|
favorites = generic.GenericRelation(to='favorites.Favorites')
|
||||||
agenda = models.ForeignKey('news.Agenda', blank=True, null=True,
|
agenda = models.ForeignKey('news.Agenda', blank=True, null=True,
|
||||||
|
|
|
||||||
27
apps/rating/migrations/0003_viewcount.py
Normal file
27
apps/rating/migrations/0003_viewcount.py
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
# Generated by Django 2.2.4 on 2019-11-14 17:15
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('contenttypes', '0002_remove_content_type_name'),
|
||||||
|
('rating', '0002_auto_20191004_0928'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='ViewCount',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('object_id', models.PositiveIntegerField()),
|
||||||
|
('count', models.IntegerField()),
|
||||||
|
('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='contenttypes.ContentType')),
|
||||||
|
],
|
||||||
|
options={
|
||||||
|
'unique_together': {('object_id', 'content_type')},
|
||||||
|
},
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -20,3 +20,21 @@ class Rating(models.Model):
|
||||||
return self.content_object.name
|
return self.content_object.name
|
||||||
if hasattr(self.content_object, 'title'):
|
if hasattr(self.content_object, 'title'):
|
||||||
return self.content_object.title_translated
|
return self.content_object.title_translated
|
||||||
|
|
||||||
|
|
||||||
|
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
|
||||||
|
|
|
||||||
28
apps/rating/transfer_data.py
Normal file
28
apps/rating/transfer_data.py
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
from transfer.models import PageTexts, PageCounters
|
||||||
|
from news.models import News
|
||||||
|
from rating.models import ViewCount
|
||||||
|
|
||||||
|
|
||||||
|
def transfer_news_view_count():
|
||||||
|
news_list = News.objects.filter(old_id__isnull=False)
|
||||||
|
views_count_list = {}
|
||||||
|
for news_object in news_list:
|
||||||
|
try:
|
||||||
|
mysql_page_text = PageTexts.objects.get(id=news_object.old_id)
|
||||||
|
except PageTexts.DoesNotExist:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if mysql_page_text.page_id not in views_count_list:
|
||||||
|
try:
|
||||||
|
mysql_views_count = PageCounters.objects.get(page_id=mysql_page_text.page_id)
|
||||||
|
except PageCounters.DoesNotExist:
|
||||||
|
continue
|
||||||
|
|
||||||
|
views_count_list[mysql_page_text.page_id] = ViewCount.objects.create(count=mysql_views_count.count)
|
||||||
|
|
||||||
|
news_object.views_count = views_count_list[mysql_page_text.page_id]
|
||||||
|
news_object.save()
|
||||||
|
|
||||||
|
data_types = {
|
||||||
|
"rating_count": [transfer_news_view_count]
|
||||||
|
}
|
||||||
|
|
@ -36,6 +36,7 @@ class Command(BaseCommand):
|
||||||
'souvenir',
|
'souvenir',
|
||||||
'establishment_note',
|
'establishment_note',
|
||||||
'assemblage',
|
'assemblage',
|
||||||
|
'rating_count'
|
||||||
]
|
]
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
def handle(self, *args, **options):
|
||||||
|
|
|
||||||
|
|
@ -36,14 +36,15 @@ THUMBNAIL_DEBUG = True
|
||||||
|
|
||||||
|
|
||||||
# ADDED TRANSFER APP
|
# ADDED TRANSFER APP
|
||||||
# INSTALLED_APPS.append('transfer.apps.TransferConfig')
|
INSTALLED_APPS.append('transfer.apps.TransferConfig')
|
||||||
|
|
||||||
|
|
||||||
# DATABASES
|
# DATABASES
|
||||||
DATABASES.update({
|
DATABASES.update({
|
||||||
'legacy': {
|
'legacy': {
|
||||||
'ENGINE': 'django.db.backends.mysql',
|
'ENGINE': 'django.db.backends.mysql',
|
||||||
'HOST': 'mysql_db',
|
'HOST': '172.22.0.1',
|
||||||
|
# 'HOST': 'mysql_db',
|
||||||
'PORT': 3306,
|
'PORT': 3306,
|
||||||
'NAME': 'dev',
|
'NAME': 'dev',
|
||||||
'USER': 'dev',
|
'USER': 'dev',
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user