X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/9b46fa5812ae6d152cef155956b435b4ba034df8..fc245a481d9adf2621de1873783c32ada3f76d2f:/src/social/models.py?ds=inline diff --git a/src/social/models.py b/src/social/models.py index e50ff0775..862db4cdd 100644 --- a/src/social/models.py +++ b/src/social/models.py @@ -4,7 +4,6 @@ from datetime import datetime import uuid from oauthlib.common import urlencode, generate_token -from pytz import utc from random import randint from django.db import models from django.conf import settings @@ -12,7 +11,7 @@ from django.contrib.auth.models import User from django.core.exceptions import ValidationError from django.core.mail import send_mail from django.urls import reverse -from django.utils.timezone import now +from django.utils.timezone import now, utc from catalogue.models import Book from catalogue.utils import get_random_hash from wolnelektury.utils import cached_render, clear_cached_renders @@ -337,12 +336,14 @@ class UserList(Syncable, models.Model): favorites=True ) except cls.DoesNotExist: + n = now() if create: return cls.objects.create( user=user, favorites=True, - slug=get_random_hash(name), - updated_at=now() + slug=get_random_hash('favorites'), + updated_at=n, + reported_timestamp=n, ) else: return None @@ -364,14 +365,23 @@ class UserList(Syncable, models.Model): return ls.userlistitem_set.filter(deleted=False, book=book).exists() def append(self, book): - # TODO: check for duplicates? n = now() - item = self.userlistitem_set.create( + items = self.userlistitem_set.filter( book=book, - order=(self.userlistitem_set.aggregate(m=models.Max('order'))['m'] or 0) + 1, - updated_at=n, - reported_timestamp=n, ) + if items.exists(): + items.update( + deleted=False, + reported_timestamp=n, + ) + item = items.first() + else: + item = self.userlistitem_set.create( + book=book, + order=(self.userlistitem_set.aggregate(m=models.Max('order'))['m'] or 0) + 1, + updated_at=n, + reported_timestamp=n, + ) book.update_popularity() return item