changes from server
[wolnelektury.git] / apps / dictionary / models.py
index 73fbb0d..cc2b0ac 100644 (file)
@@ -12,12 +12,23 @@ task_logger = get_task_logger(__name__)
 from catalogue.models import Book
 
 
+class Qualifier(models.Model):
+    qualifier = models.CharField(max_length=128, db_index=True, unique=True)
+    name = models.CharField(max_length=255)
+
+    class Meta:
+        ordering = ['qualifier']
+
+    def __unicode__(self):
+        return self.name or self.qualifier
+
+
 class Note(models.Model):
     """Represents a single annotation from a book."""
     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)
+    qualifiers = models.ManyToManyField(Qualifier)
     language = models.CharField(max_length=10, db_index=True)
 
     class Meta:
@@ -41,25 +52,35 @@ def build_notes(book):
         book.notesource_set.all().delete()
         if book.html_file:
             from librarian import html
-            for anchor, fn_type, qualifier, text_str, html_str in \
+            from librarian.fn_qualifiers import FN_QUALIFIERS
+
+            for anchor, fn_type, qualifiers, 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,
+                    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
                         )
+
+                qualifier_objects = []
+                for qualifier in qualifiers:
+                    obj, created = Qualifier.objects.get_or_create(
+                        qualifier=qualifier, defaults={
+                            'name': FN_QUALIFIERS.get(qualifier, '')
+                        })
+                    qualifier_objects.append(obj)
+                note.qualifiers = qualifier_objects
                 note.notesource_set.create(book=book, anchor=anchor)
 
         Note.objects.filter(notesource=None).delete()