Responsive images.
[wolnelektury.git] / src / dictionary / models.py
index cc2b0ac..d395b23 100644 (file)
@@ -1,4 +1,3 @@
-# -*- coding: utf-8 -*-
 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 #
@@ -7,10 +6,10 @@ 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
 
+task_logger = get_task_logger(__name__)
+
 
 class Qualifier(models.Model):
     qualifier = models.CharField(max_length=128, db_index=True, unique=True)
@@ -19,7 +18,7 @@ class Qualifier(models.Model):
     class Meta:
         ordering = ['qualifier']
 
-    def __unicode__(self):
+    def __str__(self):
         return self.name or self.qualifier
 
 
@@ -37,8 +36,8 @@ class Note(models.Model):
 
 class NoteSource(models.Model):
     """Represents a single annotation from a book."""
-    note = models.ForeignKey(Note)
-    book = models.ForeignKey(Book)
+    note = models.ForeignKey(Note, models.CASCADE)
+    book = models.ForeignKey(Book, models.CASCADE)
     anchor = models.CharField(max_length=64)
 
     class Meta:
@@ -47,6 +46,8 @@ class NoteSource(models.Model):
 
 @task(ignore_result=True)
 def build_notes(book):
+    if not book.findable:
+        return
     task_logger.info(book.slug)
     with transaction.atomic():
         book.notesource_set.all().delete()
@@ -59,10 +60,7 @@ def build_notes(book):
                 sort_key = sortify(text_str).strip()[:128]
 
                 language = book.language
-                note = None
-                notes = Note.objects.filter(sort_key=sort_key,
-                    fn_type=fn_type,
-                    language=language, html=html_str)
+                notes = Note.objects.filter(sort_key=sort_key, fn_type=fn_type, language=language, html=html_str)
                 if notes:
                     note = notes[0]
                 else:
@@ -72,7 +70,6 @@ def build_notes(book):
                         fn_type=fn_type,
                         language=language
                         )
-
                 qualifier_objects = []
                 for qualifier in qualifiers:
                     obj, created = Qualifier.objects.get_or_create(
@@ -80,12 +77,13 @@ def build_notes(book):
                             'name': FN_QUALIFIERS.get(qualifier, '')
                         })
                     qualifier_objects.append(obj)
-                note.qualifiers = qualifier_objects
+                note.qualifiers.set(qualifier_objects)
                 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)
+    if instance.findable:
+        build_notes.delay(instance)
 Book.html_built.connect(notes_from_book)