+ def save(self, *args, **kwargs):
+ audio_l = self.book.get_audio_length()
+ if audio_l:
+ if self.anchor and self.audio_timestamp is None:
+ self.mode = 'text'
+ self.audio_timestamp = self.book.sync_elid(self.anchor)
+ elif not self.anchor and self.audio_timestamp is not None:
+ self.mode = 'audio'
+ self.anchor = self.book.sync_ts(self.audio_timestamp) or ''
+ return super().save(*args, **kwargs)
+
+ @classmethod
+ def create_from_data(cls, user, data):
+ if data.get('location'):
+ return cls.get_by_location(user, data['location'], create=True)
+ elif data.get('book') and data.get('anchor'):
+ return cls.objects.create(user=user, book=data['book'], anchor=data['anchor'])
+ elif data.get('book') and data.get('audio_timestamp'):
+ return cls.objects.create(user=user, book=data['book'], audio_timestamp=data['audio_timestamp'])
+
+ @property
+ def timestamp(self):
+ return self.updated_at.timestamp()
+