fix for toc events
[wolnelektury.git] / src / catalogue / models / snippet.py
1 from django.db import models
2 from django.contrib.postgres.indexes import GinIndex
3 from django.contrib.postgres.search import SearchVector, SearchVectorField
4 from search.utils import UnaccentSearchVector
5
6
7 class Snippet(models.Model):
8     book = models.ForeignKey('Book', models.CASCADE)
9     sec = models.IntegerField()
10     anchor = models.CharField(max_length=64)
11     text = models.TextField()
12     search_vector = SearchVectorField()
13
14     class Meta:
15         indexes = [
16             GinIndex('search_vector', name='search_vector_idx'),
17         ]
18             
19     def save(self, *args, **kwargs):
20         super().save(*args, **kwargs)
21         if not self.search_vector:
22             self.update()
23
24     def update(self):
25         self.search_vector = UnaccentSearchVector('text')
26         self.save()
27
28     @classmethod
29     def update_all(cls):
30         cls.objects.all().update(search_vector = build_search_vector('text'))