def chapterList(book_id): result = ResponseData(RET.OK) page_num = request.args.get('pageNum', type=int) if not page_num: result.code = RET.NOPARAMS return result.to_dict() page_size = request.args.get('pageSize', type=int, default=30) book = Book.query.get(book_id) if not book: result.code = RET.NODATA return result.to_dict() try: chapter_query = BookChapters.query.filter( BookChapters.book_id == book_id).order_by( BookChapters.chapter_id.asc()) chapters_paginate = chapter_query.paginate(page=page_num, per_page=page_size, error_out=False) chapters = [dict(chapter) for chapter in chapters_paginate.items] page_model = PageModel(page_num=page_num, items=chapters, total_page=chapters_paginate.pages, total_num=chapters_paginate.total) result.data = dict(page_model) return result.to_dict() except Exception as e: current_app.logger.error(e) result.code = RET.DBERR return result.to_dict()
def category_del(id): result = ResponseData(RET.OK) if not id: result.code = RET.NOPARAMS return result.to_dict() cate = BookCategory.query.get(id) db.session.delete(cate) db.session.commit() return result.to_dict()
def getHotKeyword(): result = ResponseData(RET.OK) try: keywords = SearchKeyWord.query.filter_by(is_hot=True).order_by(SearchKeyWord.count.desc()).limit(10) if keywords: result.data = [dict(keyword) for keyword in keywords] except Exception as e: current_app.logger.error(e) result.code = RET.DBERR return result.to_dict()
def chapterDetail(): result = ResponseData(RET.OK) id = request.args.get('id') chapter_id = request.args.get('chapter_id') book_id = request.args.get('book_id') token = request.headers.get('token') user_id = None try: if token: user_id = verify_jwt(token) user_id = user_id.get('user_id') if not id and (not all([chapter_id, book_id])): result.code = RET.NOPARAMS return result.to_dict() chapter = None if id: chapter = BookChapters.query.get(id) elif chapter_id and book_id: chapter = BookChapters.query.filter_by( book_id=book_id, chapter_id=chapter_id).first() if not chapter: result.code = RET.NODATA return result.to_dict() data = dict(chapter) data['content'] = chapter.content.content result.data = data if user_id: history = BrowseHistory.query.filter_by( user_id=user_id, book_id=chapter.book_id).first() if not history: history = BrowseHistory(user_id=user_id, book_id=chapter.book_id) db.session.add(history) current_chapter_id = chapter.chapter_id lastChapter = BookChapters.query.filter( BookChapters.book_id == chapter.book_id).order_by( BookChapters.chapter_id.desc()).limit(1).first() last_chapter_id = lastChapter.chapter_id rate_process = round(current_chapter_id / last_chapter_id, 4) rate = ReadRate.query.filter_by(user_id=user_id, book_id=chapter.book_id).first() if not rate: rate = ReadRate(user_id=user_id, book_id=book_id, chapter_id=current_chapter_id, chapter_name=chapter.chapter_name, rate=rate_process) else: rate.chapter_id = current_chapter_id rate.chapter_name = chapter.chapter_name rate.rate = rate_process db.session.add(rate) db.session.commit() except Exception as e: current_app.logger.error(e) if not result.data: result.code = RET.DBERR return result.to_dict()
def bookList(): result = ResponseData(RET.OK) cates = request.args.get('cates', type=str, default='') keyword = request.args.get('keyword') type = request.args.get('type') page_num = request.args.get('pageNum', type=int, default=1) page_size = request.args.get('pageSize', type=int, default=20) try: books_query = Book.query if cates: books_query = books_query.filter(Book.cate_id.in_( cates.split(","))) if keyword: books_query = books_query.filter(Book.book_name.contains(keyword)) if type: if type == 'hot': books_query = books_query.filter(Book.heat > 0).order_by( Book.heat.desc()) elif type == 'collect': books_query = books_query.filter( Book.collect_count > 0).order_by(Book.collect_count.desc()) books_query = books_query.order_by(Book.create_time.desc()) books_paginate = books_query.paginate(page=page_num, per_page=page_size, error_out=False) if keyword: for book in books_paginate.items: book.heat += 1 keyword_query = SearchKeyWord.query.filter_by( keyword=keyword).first() if keyword_query: keyword_query.count += 1 if keyword_query.count > 10: keyword_query.is_hot = True else: keyword_query = SearchKeyWord(keyword=keyword, count=1) db.session.add(keyword_query) db.session.commit() books = [dict(book) for book in books_paginate.items] page_model = PageModel(page_num=page_num, items=books, total_page=books_paginate.pages, total_num=books_paginate.total) result.data = dict(page_model) except Exception as e: current_app.logger.error(e) result.code = RET.DBERR return result.to_dict()
def bookDetail(book_id): result = ResponseData(RET.OK) if not book_id: result.code = RET.NOPARAMS return result.to_dict() book = Book.query.get(book_id) if not book: result.code = RET.NODATA return result.to_dict() result.data = dict(book) return result.to_dict()
def check_auth(*args, **kwargs): g.user_id = None auth = request.headers.get('token') payload = verify_jwt(auth) if payload: g.user_id = payload.get('user_id') return view_func(*args, **kwargs) else: from utils.response_code import RET, ResponseData return ResponseData(RET.TOKENERROR).to_dict()
def addMyBook(): result = ResponseData(RET.OK) book_id = request.form.get('book_id') user_id = g.user_id if not all([book_id, user_id]): result.code = RET.NOPARAMS return result.to_dict() try: book = Book.query.get(book_id) if not book: result.code = RET.NODATA return result.to_dict() user = User.query.get(user_id) if not user: result.code = RET.NODATA return result.to_dict() book.collect_count += 1 user.book_shelf.append(book) db.session.commit() except Exception as e: current_app.logger.error(e) result.code = RET.DBERR return result.to_dict()
def getLastChapter(): result = ResponseData(RET.OK) book_id = request.args.get('book_id') if not book_id: result.code = RET.NOPARAMS return result.to_dict() try: lastChapter = BookChapters.query.filter( BookChapters.book_id == book_id).order_by( BookChapters.chapter_id.desc()).limit(1).first() result.data = dict(lastChapter) return result.to_dict() except Exception as e: current_app.logger.error(e) result.code = RET.NODATA return result.to_dict()
def getBookUser(): result = ResponseData(RET.OK) book_id = request.args.get('book_id') if not book_id: result.code = RET.NOPARAMS return result.to_dict() book = Book.query.get(book_id) users = [user.to_dict() for user in book.users] result.data = users return result.to_dict()
def chapterUpdate(id): result = ResponseData(RET.OK) content = request.form.get('content') chapter_name = request.form.get('chapter_name') chapter_id = request.form.get('chapter_id') if not all([id, content, chapter_name, chapter_id]): result.code = RET.NOPARAMS return result.to_dict() chapter = BookChapters.query.get(id) if not chapter: result.code = RET.NODATA return result.to_dict() try: chapter.chapter_id = chapter_id chapter.chapter_name = chapter_name chapter.content.content = content db.session.commit() except Exception as e: current_app.logger.error(e) result.code = RET.DBERR return result.to_dict() return result.to_dict()
def getMyBookShelf(): result = ResponseData(RET.OK) user_id = g.user_id keyword = request.args.get('keyword') if not user_id: result.code = RET.NOPARAMS return result.to_dict() try: user = User.query.get(user_id) if not user: result.code = RET.NODATA return result.to_dict() books = user.book_shelf if keyword: for book in books[:]: if not keyword in book.book_name: books.remove(book) books = [dict(book) for book in books] result.data = books except Exception as e: current_app.logger.error(e) result.code = RET.DBERR return result.to_dict()
def chapterAdd(book_id): result = ResponseData(RET.OK) book = Book.query.get(book_id) if not book: result.code = RET.NODATA return result.to_dict() chapter_id = request.form.get('chapter_id') chapter_content = request.form.get('chapter_content') chapter_name = request.form.get('chapter_name') if not chapter_id: result.code = RET.NODATA return result.to_dict() try: chapter = BookChapters.query.filter( BookChapters.book_id == book_id, BookChapters.chapter_id == chapter_id).first() if chapter: if chapter.chapter_name != chapter_name: chapter.chapter_name = chapter_name chapter_detail = BookChapterContent.query.get(chapter.id) if not chapter_detail: chapter.word_count = len( chapter_content) - chapter_content.count( ' ') - chapter_content.count('\n') db.session.add(chapter_detail) else: if len(chapter_detail.content) < len(chapter_content): chapter_detail.content = chapter_content chapter.word_count = len( chapter_content) - chapter_content.count( ' ') - chapter_content.count('\n') else: chapter = BookChapters(book_id=book_id, chapter_id=chapter_id, chapter_name=chapter_name, word_count=len(chapter_content)) db.session.add(chapter) db.session.flush() chapter_detail = BookChapterContent(id=chapter.id, content=chapter_content) db.session.add(chapter_detail) db.session.commit() except Exception as e: current_app.logger.error(e) db.session.rollback() return result.to_dict()
def addBook(): result = ResponseData(RET.OK) book_name = request.form.get('book_name') channel_name = request.form.get('channel_name') channel_url = request.form.get('channel_url') author_name = request.form.get('author_name') cate_id = request.form.get('cate_id') cate_name = request.form.get('cate_name') intro = request.form.get('intro') word_count = request.form.get('word_count') chapter_num = request.form.get('chapter_num') cover = request.form.get('cover') book_id = request.form.get('book_id') if not book_id: if not book_name or not author_name or not cate_id or not cate_name: result.code = RET.NOPARAMS return result.to_dict() if book_name: book = Book.query.filter_by(book_name=book_name).first() if book: result.data = dict(book) return result.to_dict() data = { 'book_name': book_name, 'channel_name': channel_name, 'channel_url': channel_url, 'author_name': author_name, 'cate_id': cate_id, 'cate_name': cate_name, 'intro': intro, 'word_count': word_count, 'chapter_num': chapter_num, 'cover': cover } book = Book(data) if book_id: book.book_id = int(book_id) try: if book_id: old_book = Book.query.get(book_id) for key in book.keys(): if book[key] is not None and key != 'book_id': if hasattr(old_book, key): setattr(old_book, key, book[key]) result.data = dict(old_book) else: db.session.add(book) db.session.flush() result.data = dict(book) db.session.commit() return result.to_dict() except Exception as e: current_app.logger.error(e) result.code = RET.DBERR return result.to_dict()
def login(): result = ResponseData(RET.OK) userName = request.form.get('userName') password = request.form.get('password') if not all([userName, password]): result.code = RET.NOPARAMS return result.to_dict() try: user = User.query.filter_by(userName=userName).first() if not user: result.code = RET.NODATA return result.to_dict() if not check_password_hash(user.password_hash, password): result.code = RET.NODATA return result.to_dict() result.data = user.to_dict() result.data['token'] = generate_jwt({'user_id': user.id}) except Exception as e: current_app.logger.error(e) result.code = RET.NODATA return result.to_dict() return result.to_dict()
def register(): image_data = request.files.get("file") result = ResponseData(RET.OK) if not image_data: result.code = RET.NOPARAMS return result.to_dict() location = request.form.get('location') if not location: location = '北京市/北京市/东城区' ls = location.split('/') if len(ls) != 3: result.code = RET.PARAMERROR return result.to_dict() token = upload_by_qiniu(image_data) if not token: result.code = RET.THIRDPARTYERROR return result.to_dict() try: user = User({ 'userName': request.form.get('userName'), 'password': request.form.get('password'), 'gender': request.form.get('gender'), 'province': ls[0], 'city': ls[1], 'district': ls[2], 'avatarUrl': token }) db.session.add(user) db.session.commit() except Exception as e: current_app.logger.error(e) result.code = RET.DBERR return result.to_dict() result.data = user.to_dict() result.data['token'] = generate_jwt({'user_id': user.id}) return result.to_dict()
def category_add(): result = ResponseData(RET.OK) cate_name = request.form.get('cate_name') if not cate_name: result.code = RET.NOPARAMS return result.to_dict() category = BookCategory.query.filter_by(cate_name=cate_name).first() if not category: category = BookCategory(cate_name=cate_name) else: result.data = category.to_dict() return result.to_dict() file = request.files.get('file') if not file: category.cate_icon = '/static/img/cate_cover.jpeg' else: try: key = upload_by_qiniu(file) category.cate_icon = key except Exception as e: current_app.logger.error(e) result.code = RET.THIRDPARTYERROR return result.to_dict() try: db.session.add(category) db.session.commit() except Exception as e: print(current_app.logger.error(e)) result.code = RET.DBERR return result.to_dict() result.data = category.to_dict() return result.to_dict()
def category_list(): cates = BookCategory.query.all() res = ResponseData(RET.OK) dicts = [cate.to_dict() for cate in cates] res.data = dicts return res.to_dict()