X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/527c5dba85fcdfd70755b6c1f899dbb21f5ec40a..ab81d95154bd35a67078cc6c926b5b9fbdd9011b:/apps/dictionary/models.py?ds=inline diff --git a/apps/dictionary/models.py b/apps/dictionary/models.py index 73fbb0d47..cc2b0ac51 100644 --- a/apps/dictionary/models.py +++ b/apps/dictionary/models.py @@ -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()