Fixes
[wolnelektury.git] / src / catalogue / models / snippet.py
1 from django.db import models
2 from django.contrib.postgres.search import SearchVector, SearchVectorField
3 from search.utils import build_search_vector
4
5
6 class Snippet(models.Model):
7     book = models.ForeignKey('Book', models.CASCADE)
8     sec = models.IntegerField()
9     # header_type ?
10     # header_span ?
11     text = models.TextField()
12     search_vector = SearchVectorField()
13
14     def save(self, *args, **kwargs):
15         super().save(*args, **kwargs)
16         if not self.search_vector:
17             self.update()
18
19     def update(self):
20         self.search_vector = build_search_vector('text')
21         self.save()
22
23     @classmethod
24     def update_all(cls):
25         cls.objects.all().update(search_vector = build_search_vector('text'))