update to Django 1.3; cache some api calls
[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 librarian import html
8 from sortify import sortify
9
10 from catalogue.models import Book
11
12
13 class Note(models.Model):
14     book = models.ForeignKey(Book)
15     anchor = models.CharField(max_length=64)
16     html = models.TextField()
17     sort_key = models.CharField(max_length=128, db_index=True)
18
19     class Meta:
20         ordering = ['sort_key']
21
22
23 def notes_from_book(sender, **kwargs):
24     Note.objects.filter(book=sender).delete()
25     if sender.html_file:
26         for anchor, text_str, html_str in html.extract_annotations(sender.html_file.path):
27             Note.objects.create(book=sender, anchor=anchor,
28                                html=html_str, 
29                                sort_key=sortify(text_str).strip()[:128])
30
31 # always re-extract notes after making a HTML in a Book
32 Book.html_built.connect(notes_from_book)