X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/6e32f55e3ef50d9f7d3a291c2388c5220851a9b4..b8e94e0e7cc7576cbaacd3c737d6ffca6b12db53:/apps/dictionary/models.py?ds=sidebyside diff --git a/apps/dictionary/models.py b/apps/dictionary/models.py index 52d687181..73fbb0d47 100644 --- a/apps/dictionary/models.py +++ b/apps/dictionary/models.py @@ -2,35 +2,69 @@ # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # -from django.db import models +from django.db import models, transaction from celery.task import task from sortify import sortify +from celery.utils.log import get_task_logger + +task_logger = get_task_logger(__name__) from catalogue.models import Book class Note(models.Model): """Represents a single annotation from a book.""" - book = models.ForeignKey(Book) - anchor = models.CharField(max_length=64) html = models.TextField() sort_key = models.CharField(max_length=128, db_index=True) + fn_type = models.CharField(max_length=10, db_index=True) + qualifier = models.CharField(max_length=128, db_index=True, blank=True) + language = models.CharField(max_length=10, db_index=True) class Meta: ordering = ['sort_key'] +class NoteSource(models.Model): + """Represents a single annotation from a book.""" + note = models.ForeignKey(Note) + book = models.ForeignKey(Book) + anchor = models.CharField(max_length=64) + + class Meta: + ordering = ['book'] + + @task(ignore_result=True) -def build_notes(book_id): - book = Book.objects.get(pk=book_id) - Note.objects.filter(book=book).delete() - if book.html_file: - from librarian import html - for anchor, text_str, html_str in html.extract_annotations(book.html_file.path): - Note.objects.create(book=book, anchor=anchor, - html=html_str, - sort_key=sortify(text_str).strip()[:128]) - -@Book.html_built.connect -def notes_from_book(sender, **kwargs): - build_notes.delat(sender) +def build_notes(book): + task_logger.info(book.slug) + with transaction.atomic(): + book.notesource_set.all().delete() + if book.html_file: + from librarian import html + for anchor, fn_type, qualifier, text_str, html_str in \ + html.extract_annotations(book.html_file.path): + sort_key = sortify(text_str).strip()[:128] + qualifier = (qualifier or '')[:128] + language = book.language + note = None + notes = Note.objects.filter(sort_key=sort_key, + qualifier=qualifier, fn_type=fn_type, + language=language, html=html_str) + if notes: + note = notes[0] + else: + note = Note.objects.create( + sort_key=sort_key, + qualifier=qualifier, + html=html_str, + fn_type=fn_type, + language=language + ) + note.notesource_set.create(book=book, anchor=anchor) + + Note.objects.filter(notesource=None).delete() + + +def notes_from_book(sender, instance, **kwargs): + build_notes.delay(instance) +Book.html_built.connect(notes_from_book)