- 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])
+ task_logger.info(book.slug)
+ with transaction.atomic():
+ book.notesource_set.all().delete()
+ if book.html_file:
+ from librarian import html
+ 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]
+
+ language = book.language
+ note = None
+ notes = Note.objects.filter(sort_key=sort_key,
+ fn_type=fn_type,
+ language=language, html=html_str)
+ if notes:
+ note = notes[0]
+ else:
+ note = Note.objects.create(
+ sort_key=sort_key,
+ 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()
+