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 = audio_l * .4
41 if self.audio_timestamp:
43 if self.audio_timestamp > audio_l:
44 self.audio_timestamp = audio_l
47 return super().save(*args, **kwargs)
50 def create_from_data(cls, user, data):
51 if data.get('location'):
52 return cls.get_by_location(user, data['location'], create=True)
53 elif data.get('book') and data.get('anchor'):
54 return cls.objects.create(user=user, book=data['book'], anchor=data['anchor'])
55 elif data.get('book') and data.get('audio_timestamp'):
56 return cls.objects.create(user=user, book=data['book'], audio_timestamp=data['audio_timestamp'])
60 return self.updated_at.timestamp()
63 if self.mode == 'text':
64 return f'{self.book.slug}/{self.anchor}'
66 return f'{self.book.slug}/audio/{self.audio_timestamp}'
69 def get_by_location(cls, user, location, create=False):
70 Book = apps.get_model('catalogue', 'Book')
72 slug, anchor = location.split('/', 1)
77 mode, audio_timestamp = anchor.split('/', 1)
78 assert mode == 'audio'
79 audio_timestamp = int(audio_timestamp)
83 instance = cls.objects.filter(
87 audio_timestamp=audio_timestamp,
91 audio_timestamp = None
92 instance = cls.objects.filter(
98 if instance is None and create:
100 book = Book.objects.get(slug=slug)
101 except Book.DoesNotExist:
103 instance = cls.objects.create(
108 audio_timestamp=audio_timestamp,
112 def get_for_json(self):
115 'anchor': self.anchor,
117 'created_at': self.created_at,
121 class Quote(models.Model):
122 uuid = models.UUIDField(unique=True, default=uuid.uuid4, editable=False)
123 user = models.ForeignKey('auth.User', models.CASCADE)
124 book = models.ForeignKey('catalogue.Book', models.CASCADE)
125 created_at = models.DateTimeField(auto_now_add=True)
126 start_elem = models.CharField(max_length=100, blank=True)
127 end_elem = models.CharField(max_length=100, blank=True)
128 start_offset = models.IntegerField(null=True, blank=True)
129 end_offset = models.IntegerField(null=True, blank=True)
130 text = models.TextField(blank=True)
133 return str(self.uuid)
135 def get_for_json(self):
138 'startElem': self.start_elem,
139 'endElem': self.end_elem,
140 'startOffset': self.start_offset,
141 'startOffset': self.end_offset,
142 'created_at': self.created_at,
145 def from_path(elem, path):
147 if e.text: yield (e, 'text')
149 if child.attrib.get('id') != 'toc':
152 yield (child, 'tail')
155 elem = list(child_nodes(elem))[n]