]> git.phdru.name Git - m_librarian.git/blobdiff - m_librarian/inp.py
Prevent duplicate authors
[m_librarian.git] / m_librarian / inp.py
index d4e6cc1ff730f09f3dc403bdb213e928da4dbd54..09cd42be2151a5e7b11dda19673cbada15578721 100644 (file)
@@ -4,7 +4,8 @@ __all__ = ['import_inpx']
 import os
 from zipfile import ZipFile
 from sqlobject import sqlhub, SQLObjectNotFound
-from .db import Author, Book, Extension, Genre, Language, insert_name
+from .db import Author, Book, Extension, Genre, Language, \
+    insert_name, insert_author
 
 
 EOT = chr(4)  # INP field separator
@@ -44,9 +45,24 @@ def import_inp_line(archive, parts):
                 lib_id=lib_id, deleted=deleted,
                 extension=extension_row, date=date,
                 language=language_row)
-    for author in authors.split(':'):
+    authors = authors.split(':')
+    seen_authors = set()
+    for author in authors:
         if author:
-            author_row = insert_name(Author, author)
+            if author in seen_authors:
+                continue
+            seen_authors.add(author)
+            alist = author.split(',', 2)
+            surname = alist[0]
+            if len(alist) > 1:
+                name = alist[1]
+                if len(alist) == 3:
+                    misc_name = alist[2]
+                else:
+                    misc_name = ''
+            else:
+                name = misc_name = ''
+            author_row = insert_author(surname, name, misc_name)
             book.addAuthor(author_row)
     for genre in genres.split(':'):
         if genre: