def form_valid(self, form): logger.debug('ImporterView::form_valid') book_file = form.cleaned_data.get('book_file') ext = get_file_extension(book_file.name) logger.debug('ImporterView::Importing file extension is "{}".'.format(ext.encode('utf8'))) default_book_title = self.get_default_title(book_file, ext) book_title = form.cleaned_data.get('book_title', default_book_title) logger.debug('ImporterView::book_title="{}" default_book_title="{}".'.format( book_title.encode('utf8'), default_book_title.encode('utf8'))) # in case book title in form is empty string if len(book_title) == 0: book_title = default_book_title if not check_book_availability(book_title): registered = Book.objects.filter(title__startswith=book_title).count() book_title = '%s %s' % (book_title, registered) logger.debug('ImporterView::Checking book availability: "{}".'.format(book_title.encode('utf8'))) book_url = booktype_slugify(book_title) book = create_book(self.request.user, book_title, book_url=book_url) logger.debug('ImporterView::Book created with url title="{}".'.format(book_url)) # check if book will be hidden and set to book book.hidden = form.cleaned_data.get('hidden') book.save() notifier = CollectNotifier() delegate = Delegate() response = {} try: book_importer = importer_utils.get_importer_module(ext) except KeyError: logger.error('ImporterView::No importer for this extension') response_data = dict(errors=[ugettext('Extension not supported!')]) return self.render_json_response(response_data) try: book_importer( book_file, book, notifier=notifier, delegate=delegate) logger.debug('ImporterView::Book imported.') response['url'] = reverse('reader:infopage', args=[book.url_title]) except Exception as e: logger.error('ImporterView::Some kind of error while importing book.') logger.exception(e) notifier.errors.append(str(e)) response['infos'] = notifier.infos response['warnings'] = notifier.warnings response['errors'] = notifier.errors return self.render_json_response(response)
def create(self, validated_data): n = Book.objects.count() book_title = validated_data['title'] owner = validated_data['owner'] url_title = '%s-%s' % (n, booktype_slugify(book_title)) book = create_book(owner, book_title, book_url=url_title) book.language = validated_data.get('language', None) book.save() import_book_url = validated_data.get('import_book_url') import_format = validated_data.get('import_book_format') if import_book_url: book_file = self._get_book_file(import_book_url) try: book_importer = importer_utils.get_importer_module( import_format) except Exception as err: error = "Wrong importer format {}".format(err) logger.warn('BookCreateSerializer create: {}'.format(error)) raise serializers.ValidationError(error) delegate = Delegate() notifier = CollectNotifier() try: book_importer(book_file, book, notifier=notifier, delegate=delegate) except Exception as err: error_msg = "Unexpected error while importing the file {}".format( err) logger.warn( 'BookCreateSerializer create: {}'.format(error_msg)) raise APIException(error_msg) if len(notifier.errors) > 0: err = "\n".join(notifier.errors) error_msg = "Something went wrong: {}".format(err) logger.warn( 'BookCreateSerializer create: {}'.format(error_msg)) raise APIException(error_msg) return book
def create(self, validated_data): n = Book.objects.count() book_title = validated_data['title'] owner = validated_data['owner'] url_title = '%s-%s' % (n, booktype_slugify(book_title)) book = create_book(owner, book_title, book_url=url_title) book.language = validated_data.get('language', None) book.save() import_book_url = validated_data.get('import_book_url') import_format = validated_data.get('import_book_format') if import_book_url: book_file = self._get_book_file(import_book_url) try: book_importer = importer_utils.get_importer_module(import_format) except Exception as err: error = "Wrong importer format {}".format(err) logger.warn('BookCreateSerializer create: {}'.format(error)) raise serializers.ValidationError(error) delegate = Delegate() notifier = CollectNotifier() try: book_importer(book_file, book, notifier=notifier, delegate=delegate) except Exception as err: error_msg = "Unexpected error while importing the file {}".format(err) logger.warn('BookCreateSerializer create: {}'.format(error_msg)) raise APIException(error_msg) if len(notifier.errors) > 0: err = "\n".join(notifier.errors) error_msg = "Something went wrong: {}".format(err) logger.warn('BookCreateSerializer create: {}'.format(error_msg)) raise APIException(error_msg) return book
def import_based_on_file(import_file, book_dest): """ It will import the content from a given file (docx or epub for now) into a existent book on the system. Note that this is not going to import section settings Keyword arguments: import_file -- EPUB/DOCX file to be imported into book_dest book_dest -- Destiny book """ ext = get_file_extension(import_file.name) notifier = CollectNotifier() delegate = Delegate() result = {} try: book_importer = importer_utils.get_importer_module(ext) except KeyError: logger.error('ImporterView::No importer for this extension') raise NotImplementedError('Extension not supported!') try: book_importer(import_file, book_dest, notifier=notifier, delegate=delegate) logger.debug('ImporterView::Book imported.') except Exception as e: logger.error('ImporterView::Some kind of error while importing book.') logger.exception(e) notifier.errors.append(str(e)) result['infos'] = notifier.infos result['warnings'] = notifier.warnings result['errors'] = notifier.errors return result