commit tx
[wolnelektury.git] / apps / dictionary / models.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from django.db import models
6
7 from sortify import sortify
8
9 from catalogue.models import Book
10
11
12 class Note(models.Model):
13     book = models.ForeignKey(Book)
14     anchor = models.CharField(max_length=64)
15     html = models.TextField()
16     sort_key = models.CharField(max_length=128, db_index=True)
17
18     class Meta:
19         ordering = ['sort_key']
20
21
22 def notes_from_book(sender, **kwargs):
23     from librarian import html
24
25     Note.objects.filter(book=sender).delete()
26     if sender.html_file:
27         for anchor, text_str, html_str in html.extract_annotations(sender.html_file.path):
28             Note.objects.create(book=sender, anchor=anchor,
29                                html=html_str, 
30                                sort_key=sortify(text_str).strip()[:128])
31
32 # always re-extract notes after making a HTML in a Book
33 Book.html_built.connect(notes_from_book)