2 from django.apps import apps
3 from django.db import models
4 from django.utils.timezone import now
5 from social.syncable import Syncable
8 class Bookmark(Syncable, models.Model):
9 uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
10 user = models.ForeignKey('auth.User', models.CASCADE)
11 book = models.ForeignKey('catalogue.Book', models.CASCADE)
12 anchor = models.CharField(max_length=100, blank=True)
13 audio_timestamp = models.IntegerField(null=True, blank=True)
14 mode = models.CharField(max_length=64, choices=[
18 created_at = models.DateTimeField(auto_now_add=True)
19 note = models.TextField(blank=True)
20 updated_at = models.DateTimeField(auto_now=True)
21 reported_timestamp = models.DateTimeField(default=now)
22 deleted = models.BooleanField(default=False)
31 def save(self, *args, **kwargs):
34 audio_l = self.book.get_audio_length()
40 self.audio_timestamp = self.book.sync_elid(self.anchor)
41 if self.audio_timestamp:
43 self.anchor = self.book.sync_ts(self.audio_timestamp)
44 return super().save(*args, **kwargs)
47 def create_from_data(cls, user, data):
48 if data.get('location'):
49 return cls.get_by_location(user, data['location'], create=True)
50 elif data.get('book') and data.get('anchor'):
51 return cls.objects.create(user=user, book=data['book'], anchor=data['anchor'])
52 elif data.get('book') and data.get('audio_timestamp'):
53 return cls.objects.create(user=user, book=data['book'], audio_timestamp=data['audio_timestamp'])
57 return self.updated_at.timestamp()
60 if self.mode == 'text':
61 return f'{self.book.slug}/{self.anchor}'
63 return f'{self.book.slug}/audio/{self.audio_timestamp}'
66 def get_by_location(cls, user, location, create=False):
67 Book = apps.get_model('catalogue', 'Book')
69 slug, anchor = location.split('/', 1)
74 mode, audio_timestamp = anchor.split('/', 1)
75 assert mode == 'audio'
76 audio_timestamp = int(audio_timestamp)
80 instance = cls.objects.filter(
84 audio_timestamp=audio_timestamp,
88 audio_timestamp = None
89 instance = cls.objects.filter(
95 if instance is None and create:
97 book = Book.objects.get(slug=slug)
98 except Book.DoesNotExist:
100 instance = cls.objects.create(
105 audio_timestamp=audio_timestamp,
109 def get_for_json(self):
112 'anchor': self.anchor,
114 'created_at': self.created_at,
118 class Quote(models.Model):
119 uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
120 user = models.ForeignKey('auth.User', models.CASCADE)
121 book = models.ForeignKey('catalogue.Book', models.CASCADE)
122 created_at = models.DateTimeField(auto_now_add=True)
123 start_elem = models.CharField(max_length=100, blank=True)
124 end_elem = models.CharField(max_length=100, blank=True)
125 start_offset = models.IntegerField(null=True, blank=True)
126 end_offset = models.IntegerField(null=True, blank=True)
127 text = models.TextField(blank=True)
130 return str(self.uuid)
132 def get_for_json(self):
135 'startElem': self.start_elem,
136 'endElem': self.end_elem,
137 'startOffset': self.start_offset,
138 'startOffset': self.end_offset,
139 'created_at': self.created_at,
142 def from_path(elem, path):
144 if e.text: yield (e, 'text')
146 if child.attrib.get('id') != 'toc':
149 yield (child, 'tail')
152 elem = list(child_nodes(elem))[n]