+ new_fragment.tags = set(meta_tags + themes + [book_tag] + ancestor_tags)
+ self.save()
+ return True
+ return False
+
+
+ @classmethod
+ def from_xml_file(cls, xml_file, **kwargs):
+ # use librarian to parse meta-data
+ book_info = dcparser.parse(xml_file)
+
+ if not isinstance(xml_file, File):
+ xml_file = File(open(xml_file))
+
+ try:
+ return cls.from_text_and_meta(xml_file, book_info, **kwargs)
+ finally:
+ xml_file.close()
+
+ @classmethod
+ def from_text_and_meta(cls, raw_file, book_info, overwrite=False, build_epub=True, build_txt=True):
+ import re
+
+ # check for parts before we do anything
+ children = []
+ if hasattr(book_info, 'parts'):
+ for part_url in book_info.parts:
+ base, slug = part_url.rsplit('/', 1)
+ try:
+ children.append(Book.objects.get(slug=slug))
+ except Book.DoesNotExist, e:
+ raise Book.DoesNotExist(_('Book with slug = "%s" does not exist.') % slug)
+
+
+ # Read book metadata
+ book_base, book_slug = book_info.url.rsplit('/', 1)
+ if re.search(r'[^a-zA-Z0-9-]', book_slug):
+ raise ValueError('Invalid characters in slug')
+ book, created = Book.objects.get_or_create(slug=book_slug)
+
+ if created:
+ book_shelves = []
+ else:
+ if not overwrite:
+ raise Book.AlreadyExists(_('Book %s already exists') % book_slug)
+ # Save shelves for this book
+ book_shelves = list(book.tags.filter(category='set'))
+
+ book.title = book_info.title
+ book.set_extra_info_value(book_info.to_dict())
+ book.save()
+
+ meta_tags = []
+ categories = (('kinds', 'kind'), ('genres', 'genre'), ('authors', 'author'), ('epochs', 'epoch'))
+ for field_name, category in categories:
+ try:
+ tag_names = getattr(book_info, field_name)
+ except:
+ tag_names = [getattr(book_info, category)]
+ for tag_name in tag_names:
+ tag_sort_key = tag_name
+ if category == 'author':
+ tag_sort_key = tag_name.last_name
+ tag_name = ' '.join(tag_name.first_names) + ' ' + tag_name.last_name
+ tag, created = Tag.objects.get_or_create(slug=slughifi(tag_name), category=category)
+ if created:
+ tag.name = tag_name
+ tag.sort_key = sortify(tag_sort_key.lower())
+ tag.save()
+ meta_tags.append(tag)
+
+ book.tags = set(meta_tags + book_shelves)
+
+ book_tag = book.book_tag()
+
+ for n, child_book in enumerate(children):
+ child_book.parent = book
+ child_book.parent_number = n
+ child_book.save()
+
+ # Save XML and HTML files
+ book.xml_file.save('%s.xml' % book.slug, raw_file, save=False)
+
+ # delete old fragments when overwriting
+ book.fragments.all().delete()
+
+ if book.build_html():
+ if not settings.NO_BUILD_TXT and build_txt:
+ book.build_txt()
+
+ if not settings.NO_BUILD_EPUB and build_epub:
+ book.root_ancestor.build_epub()
+
+ book_descendants = list(book.children.all())
+ # add l-tag to descendants and their fragments
+ # delete unnecessary EPUB files
+ while len(book_descendants) > 0:
+ child_book = book_descendants.pop(0)
+ child_book.tags = list(child_book.tags) + [book_tag]
+ child_book.save()
+ for fragment in child_book.fragments.all():
+ fragment.tags = set(list(fragment.tags) + [book_tag])
+ book_descendants += list(child_book.children.all())
+