For upgrades
[wolnelektury.git] / src / social / models.py
index c8d0d10..a8bec9c 100644 (file)
@@ -11,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, utc
+from django.utils.timezone import now
 from catalogue.models import Book
 from catalogue.utils import get_random_hash
 from wolnelektury.utils import cached_render, clear_cached_renders
@@ -418,11 +418,11 @@ class UserList(Syncable, models.Model):
 class UserListItem(Syncable, models.Model):
     list = models.ForeignKey(UserList, models.CASCADE)
     uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False, blank=True)
-    order = models.IntegerField()
+    order = models.IntegerField(default=0)
     deleted = models.BooleanField(default=False)
     created_at = models.DateTimeField(auto_now_add=True)
     updated_at = models.DateTimeField(auto_now=True)
-    reported_timestamp = models.DateTimeField()
+    reported_timestamp = models.DateTimeField(default=now)
 
     book = models.ForeignKey('catalogue.Book', models.SET_NULL, null=True, blank=True)
     fragment = models.ForeignKey('catalogue.Fragment', models.SET_NULL, null=True, blank=True)
@@ -436,6 +436,11 @@ class UserListItem(Syncable, models.Model):
     objects = ActiveManager()
     all_objects = models.Manager()
 
+    def save(self, *args, **kwargs):
+        if not self.order:
+            self.order = self.list.userlistitem_set.all().count() + 1
+        super().save(*args, **kwargs)
+
     @classmethod
     def create_from_data(cls, user, data):
         if data.get('favorites'):