def account(): if current_user.role != 'admin': form = UpdateAccountForm() if form.validate_on_submit(): form.validate_name(form.name) form.validate_email(form.email) current_user.name = form.name.data current_user.email = form.email.data db.session.commit() flash('Reader account has been updated', 'success') else: form.name.data = current_user.name form.email.data = current_user.email # return render_template('account.html', title='Account', form=form) else: form = BookForm() if form.validate_on_submit(): books = Book.query.filter_by(title=form.title.data).all() for book in books: if book.author == form.author.data: flash('This book is already in the database', 'danger') else: book = Book() book.title = form.title.data book.author = form.author.data book.summary = form.summary.data db.session.add(book) db.session.commit() flash(f'Added book {book.title}', 'success') form.title.data = '' form.author.data = '' form.summary.data = '' return render_template('account.html', title='Account', form=form)
def addbook(request): print(request.method) if request.method == 'GET': return render(request, 'addbook.html') name = request.POST.get('name') author = request.POST.get('author') book = Book() book.name = name book.author = author book.save() return HttpResponse('添加成功')
def addbook(): form = AddBookForm() book = Book(uploader_id=current_user.id) ts = str(time.time()).split('.')[0] if form.validate_on_submit(): if form.book_img.data: f = form.book_img.data file_name = ts + '_' + f.filename file_path = os.path.join(config['default'].UPLOAD_PATH + '/books/', file_name) f.save(file_path) book.image_path = url_for('static', filename='uploads/books/' + file_name) book.name = form.name.data book.author = form.author.data or u'无' book.description = form.description.data or u'暂无介绍' book.category_id = form.category.data if form.tag.data and len(form.tag.data.split(';')) > 0: for i in form.tag.data.split(';'): tag = Tag.findOrInsert(i) if tag == None: continue book.tags.append(tag) book.status_id = form.status.data book.upload_user = current_user._get_current_object() book.isbn = form.isbn.data or u'暂无' book.total_count = int(form.total_count.data or 0) book.book_number = form.book_number.data or u'暂无' book.publisher = form.publisher.data or u'暂无' try: if form.book_img.data is None: id, img_200_200_path, img_350_350_path = download_images_from_bookname( form.name.data, config['default'].UPLOAD_PATH + '/books/') book.description = get_detail_info(id)[0] book.image_path = img_350_350_path.split('/app')[-1] except Exception as e: raise e db.session.add(book) flash('新增图书成功') return redirect(url_for('main.addbook')) return custom_render_template('book/add_book.html', form=form)
def book_init_func(row): book_instance = Book(row['title']) book_instance.book_id = row['book_id'] book_instance.author = row['author'] book_instance.additional_authors = row['additional_authors'] book_instance.isbn = row['isbn'] book_instance.isbn13 = row['isbn13'] book_instance.my_rating = row['my_rating'] book_instance.avg_rating = row['avg_rating'] book_instance.publisher = row['publisher'] book_instance.binding = row['binding'] book_instance.pages = row['pages'] book_instance.year_publish = row['year_published'] book_instance.year_publish_original = row['year_publish_original'] book_instance.date_read = row['date_read'] book_instance.date_added = row['date_added'] book_instance.bookshelves = row['bookshelves'] '''Add rows to User.books''' user = User.query.filter_by(username='******').first() user.books.append(book_instance) '''populate Dewey Decimal number''' return book_instance
def books(author_username=None): from app.models import Author, Book try: if request.method == 'POST': author_id = request.form['author_id'] or None if not author_id: raise InvalidUsage('Id de autor no fue encontrado.', status_code=503) book = Book(title=request.form['title']) book.author = Author.objects.get(pk=author_id) book.save() return jsonify(book.json) elif request.method == 'DELETE': if not request.form['book_id']: raise InvalidUsage('Olvido el campo book_id') Book.objects(pk=request.form['book_id']).delete() return jsonify( dict(status=200, message='Documento fue eliminado con exito.')) else: if author_username: return jsonify( dict(author_books=[ book.json for book in Book.objects( author=Author.objects.get(name=author_username)) ])) return jsonify(dict(books=[book.json for book in Book.objects])) except Exception as err: raise err.with_traceback(err.__traceback__)
def handle_info_html(self, html, type_tag): soup = BeautifulSoup(html, 'lxml') book = Book() # type_id = db.session.query(Type).filter_by(title=tag).first().id try: title = soup.h1.span.get_text() info = soup.find(class_='article').find(class_='indent').find( class_='subjectwrap clearfix').find( class_='subject clearfix').find(id='info') string = info.get_text().strip() string = string.replace(' ', '') string = string.replace(' ', '') string = string.replace('\n', '') tag_list = [ '出版社:', '出品方:', '副标题:', '原作名:', '译者:', '出版年:', '页数:', '定价:', '装帧:', '丛书:', 'ISBN:' ] value_list = [] if '作者:' in string: string = string.replace('作者:', '') flag = 0 for tag in tag_list: if tag in string: value = string.split(tag)[0] value_list.append(value) if flag != 0: for i in range(flag): value_list.append('') flag = 0 else: flag += 1 continue string = string.split(tag)[1] if tag == 'ISBN:': value_list.append(string) author = value_list[0] publisher = value_list[1] producer = value_list[2] subtitle = value_list[3] original_title = value_list[4] translator = value_list[5] year_of_publisher = value_list[6] pages = value_list[7] price = value_list[8] binding = value_list[9] series = value_list[10] isbn = value_list[11] pic_href = soup.find(class_='article').find(class_='indent').find( class_='subjectwrap clearfix').find( class_='subject clearfix').find(id='mainpic').a['href'] score = soup.find( class_='rating_self clearfix').strong.get_text().strip() score_people = soup.find(class_='rating_people').get_text() related_info = soup.find(class_='related_info') infos = related_info.find_all(class_='indent')[:2] content_info = str(infos[0].find(class_='intro')).replace( '<div class="intro">', '') author_info = str(infos[1].find(class_='intro')).replace( '<div class="intro">', '') book.title = title book.author = author book.publisher = publisher book.producer = producer book.translator = translator book.subtitle = subtitle book.original_title = original_title book.year_of_publisher = year_of_publisher book.pages = pages book.price = price book.binding = binding book.series = series book.isbn = isbn book.score = score book.score_people = score_people book.type = type_tag book.content_info = content_info book.author_info = author_info book.pic_href = pic_href self.safe_commit(book) except Exception as e: self.logger.warning('爬起失败', e) return True return False