Fix rating

This commit is contained in:
Виктор Гладких 2019-10-04 15:39:14 +03:00
parent f9247477f9
commit 9f9c1064bd
3 changed files with 32 additions and 10 deletions

View File

@ -0,0 +1,22 @@
# Generated by Django 2.2.4 on 2019-10-04 09:28
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('contenttypes', '0002_remove_content_type_name'),
('rating', '0001_initial'),
]
operations = [
migrations.AlterModelOptions(
name='rating',
options={},
),
migrations.AlterUniqueTogether(
name='rating',
unique_together={('ip', 'object_id', 'content_type')},
),
]

View File

@ -10,6 +10,9 @@ class Rating(models.Model):
content_object = GenericForeignKey('content_type', 'object_id')
ip = models.GenericIPAddressField(verbose_name=_('ip'))
class Meta:
unique_together = ('ip', 'object_id', 'content_type')
@property
def name(self):
# Check if Generic obj has name or title

View File

@ -1,22 +1,19 @@
from datetime import timedelta
from celery import task
from celery import shared_task
from rating.models import Rating
from django.contrib.contenttypes.models import ContentType
def add_rating(remote_addr, pk, model, app_label):
add.apply_async(
(remote_addr, pk, model, app_label), countdown=60 * 60
(remote_addr, pk, model, app_label), countdown=2 # 60 * 60
)
# TODO Вернуть интервал
@task
@shared_task
def add(remote_addr, pk, model, app_label):
rating = Rating()
rating.ip = remote_addr
rating.object_id = pk
rating.content_type = ContentType.objects.get(app_label=app_label, model=model)
rating.save()
content_type = ContentType.objects.get(app_label=app_label, model=model)
Rating.objects.get_or_create(
ip=remote_addr, object_id=pk, content_type=content_type)