moves on api
[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     text = models.TextField()
11     search_vector = SearchVectorField()
12
13     class Meta:
14         indexes = [
15             GinIndex('search_vector', name='search_vector_idx'),
16         ]
17             
18     def save(self, *args, **kwargs):
19         super().save(*args, **kwargs)
20         if not self.search_vector:
21             self.update()
22
23     def update(self):
24         self.search_vector = UnaccentSearchVector('text')
25         self.save()
26
27     @classmethod
28     def update_all(cls):
29         cls.objects.all().update(search_vector = build_search_vector('text'))