Bookmarks sync
[wolnelektury.git] / src / social / models.py
index 0b366fc..2692538 100644 (file)
@@ -16,6 +16,7 @@ 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
+from .syncable import Syncable
 
 
 class BannerGroup(models.Model):
@@ -207,44 +208,6 @@ class UserConfirmation(models.Model):
         ).send()
 
 
-
-class Syncable:
-    @classmethod
-    def sync(cls, user, instance, data):
-        ts = data.get('timestamp')
-        if ts is None:
-            ts = now()
-        else:
-            ts = datetime.fromtimestamp(ts, tz=utc)
-
-        if instance is not None:
-            if ts and ts < instance.reported_timestamp:
-                return
-
-        if instance is None:
-            if data.get('deleted'):
-                return
-            instance = cls.create_from_data(user, data)
-            if instance is None:
-                return
-
-        instance.reported_timestamp = ts
-        for f in cls.syncable_fields:
-            if f in data:
-                setattr(instance, f, data[f])
-        
-        instance.save()
-        return instance
-
-    @property
-    def timestamp(self):
-        return self.updated_at.timestamp()
-    
-    @classmethod
-    def create_from_data(cls, user, data):
-        raise NotImplementedError
-
-
 class Progress(Syncable, models.Model):
     user = models.ForeignKey(User, models.CASCADE)
     book = models.ForeignKey('catalogue.Book', models.CASCADE)