def read_the_book(self, path, f_name, sh_names): """ this method is reading each book, chapter by chapter and verse after verse and put it into the DataBase path - the path to the book f_name - full title of the current book sh_names - list of the shorten names """ try: module = open(os.path.join(self.path, path), 'r') except: print "oops, the module %s doesn't exists" % \ (os.path.join(self.path, path)) return print path, f_name, sh_names book = Book() book.name = f_name book.shortname = sh_names reg = re.compile(ur'(\d+)') tag_remove = re.compile(ur'<.*?>') re_strong = re.compile(ur' [0-9]+') chapt_number = 1 for line in module: uline = line.decode('utf-8', 'replace') if u'<h4>' in uline.lower() or u'<h1>' in uline.lower(): uline = tag_remove.sub('', uline) chapter = Chapter() chapter.name = uline chapter.number = chapt_number chapt_number += 1 book.chapters.append(chapter) session.add(chapter) if u'<p>' in uline.lower() or u'<sup>' in uline.lower(): uline = tag_remove.sub('', uline) uline = re_strong.sub('', uline) verse = Verse() verse.number = int(reg.findall(uline)[0]) verse.text = uline chapter.verses.append(verse) session.add(verse) session.add(chapter) session.add(book) session.commit() module.close()
def addBook(): global apiInfo orgBook = Book() form = EditBookForm() if request.method == 'GET': form.id.data = orgBook.id form.name.data = orgBook.name form.price.data = orgBook.price form.isbn.data = orgBook.isbn form.isObsolete.data = orgBook.isObsolete form.bookType.data = orgBook.bookType if request.method == 'POST' and form.validate( ): # Equivalent to validate_on_submit() newBook = Book() newBook.name = request.form['name'] newBook.isbn = request.form['isbn'] newBook.price = request.form['price'] newBook.isObsolete = form.isObsolete.data # TODO (bug) request.form['<booelan>'] does not return newBook.bookType = request.form['bookType'] # TODO (bug) Error when doing the api-call addedBook = json.loads( requests.post(app.config['API_ROOT_URL'] + '/books', json=vars(newBook)).content) newBook.id = addedBook['id'] flash('Added book {}'.format(vars(newBook))) return redirect('/books') return render_template('books/edit.html', actionTitle='Add book', appTitle=app.config['APP_TITLE'], api=apiInfo, book=vars(orgBook), form=form)