def get_all_genres(): """ Get all genres :return: list of genres """ json_data = app.wiring.cache_db.get_value('genres') if json_data: response = app.response_class( response=json_data, status=200, mimetype='application/json' ) return response dataset = app.wiring.genre_dao.get_all() genres = [row2dict(row) for row in dataset] if not genres: return abort(404) json_data = json.dumps(genres, ensure_ascii=False).encode('utf8') app.wiring.cache_db.set_value('genres', json_data, CACHE_FOUR_WEEK) # four week cache response = app.response_class( response=json_data, status=200, mimetype='application/json' ) return response
def get_statistic(): json_data = app.wiring.cache_db.get_value('stat') if json_data: response = app.response_class(response=json_data, status=200, mimetype='application/json') return response periods = get_periods() top_down_book_ids = app.wiring.stat.top_download_books(10) top_view_book_ids = app.wiring.stat.top_viewed_books(10) downloads = dict() views = dict() for item in periods: downloads[item['name']] = app.wiring.stat.count_download( item['start'], item['end']) views[item['name']] = app.wiring.stat.count_viewed( item['start'], item['end']) top_download_books = [] top_viewed_books = [] for item in top_down_book_ids: book = app.wiring.book_dao.get_by_id(item['book_id']) if book: top_download_books.append(row2dict(book)) for item in top_view_book_ids: book = app.wiring.book_dao.get_by_id(item['book_id']) if book: top_viewed_books.append(row2dict(book)) user_count = app.wiring.users.get_count_users() stats = { 'users': user_count, 'downloads': downloads, 'views': views, 'topDownloadBooks': top_download_books, 'topViewBooks': top_viewed_books } json_data = json.dumps(stats) app.wiring.cache_db.set_value('stat', json_data) response = app.response_class(response=json_data, status=200, mimetype='application/json') return response
def reload_genre(wiring: Wiring): # Reload cache for genre dataset = wiring.genre_dao.get_all() genres = [row2dict(row) for row in dataset] if not genres: return None json_data = json.dumps(genres, ensure_ascii=False).encode('utf8') wiring.cache_db.set_value('genres', json_data, CACHE_FOUR_WEEK)
def get_books_by_language(languageId): limit = request.args.get('limit', app.wiring.settings.DEFAULT_LIMITS, int) skip = request.args.get('skip', app.wiring.settings.DEFAULT_SKIP_RECORD, int) dataset = app.wiring.book_dao.books_by_language(languageId, limit=limit, skip=skip) result = [row2dict(row) for row in dataset] json_data = json.dumps(result, ensure_ascii=False).encode('utf8') response = app.response_class( response=json_data, status=200, mimetype='application/json' ) return response
def popular_books(): limit = request.args.get('limit', app.wiring.settings.DEFAULT_LIMITS, int) books = app.wiring.book_dao.get_popular_books(limit) if not books: abort(404) result = [row2dict(row) for row in books] json_data = json.dumps(result, ensure_ascii=False).encode('utf8') response = app.response_class( response=json_data, status=200, mimetype='application/json' ) return response
def get_language(languageId): try: dataset = app.wiring.language_dao.get_by_id(languageId) except LanguageNotFound: return abort(404) except Exception as e: return abort(400) json_data = json.dumps(row2dict(dataset), ensure_ascii=False).encode('utf8') response = app.response_class( response=json_data, status=200, mimetype='application/json' ) return response
def get_book_by_genre(name): """ Get books by genre :param name: Name of genre :return: """ dataset = app.wiring.book_dao.books_by_genres(name) result = [row2dict(row) for row in dataset] json_data = json.dumps(result, ensure_ascii=False).encode('utf8') response = app.response_class( response=json_data, status=200, mimetype='application/json' ) return response
def get_book_by_name(name): """ Find books by name :param name: :return: """ dataset = app.wiring.book_dao.get_by_name(name) result = [row2dict(row) for row in dataset] if not result: return abort(404) json_data = json.dumps(result, ensure_ascii=False).encode('utf8') response = app.response_class( response=json_data, status=200, mimetype='application/json' ) return response
def get_author_by_id(authorid): """ Get author by uniq Id :param authorid: Id of author :return: author """ try: dataset = app.wiring.author_dao.get_by_id(authorid) except AuthorNotFound: return abort(404) except Exception as err: return abort(400) json_data = json.dumps(row2dict(dataset), ensure_ascii=False).encode('utf8') response = app.response_class(response=json_data, status=200, mimetype='application/json') return response
def get_all_authors(): """ Get all authors sorted by last name :return: """ limit = int( request.args.get('limit', app.wiring.settings.DEFAULT_LIMITS, int)) skip = int( request.args.get('skip', app.wiring.settings.DEFAULT_SKIP_RECORD, int)) dataset = app.wiring.author_dao.get_all(limit, skip) result = [row2dict(row) for row in dataset] if not result: return abort(404) json_data = json.dumps(result, ensure_ascii=False).encode('utf8') response = app.response_class(response=json_data, status=200, mimetype='application/json') return response
def get_authors_startwith(start_text_fullname): """ Get authors last_name startwith start_text :param start_text_fullname :return: """ limit = request.args.get('limit', app.wiring.settings.DEFAULT_LIMITS, int) skip = request.args.get('skip', app.wiring.settings.DEFAULT_SKIP_RECORD, int) dataset = app.wiring.author_dao.get_by_start(start_text_fullname, limit=limit, skip=skip) result = [row2dict(row) for row in dataset] if not result: return abort(404) json_data = json.dumps(result, ensure_ascii=False).encode('utf8') response = app.response_class(response=json_data, status=200, mimetype='application/json') return response
def get_book(bookid): """ Get book by bookId :param bookid: Id of book :return: """ dataset = app.wiring.book_ext_dao.get_by_id(bookid) if not dataset: return abort(404) stat_it(app.wiring, 'bv', bookid, '') book = row2dict(dataset, full_view) cover_name = '{}.jpg'.format(book['id']) cover_path = os.path.join(app.wiring.settings.IMAGE_DIR, cover_name) is_exists = os.path.exists(cover_path) book['cover'] = '/cover/' + cover_name if is_exists else "" json_data = json.dumps(book, ensure_ascii=False).encode('utf8') response = app.response_class( response=json_data, status=200, mimetype='application/json' ) return response
def get_book_by_search(): """ Search book by name, series, genre, keyword :return: """ f_name = request.args.get('name', '') f_lang = request.args.get('lang', '') f_series = request.args.get('series', '') f_keyword = request.args.get('keyword', '') f_genre = request.args.get('genre', '') f_limit = request.args.get('limit', app.wiring.settings.DEFAULT_LIMITS, int) f_skip = request.args.get('skip', app.wiring.settings.DEFAULT_SKIP_RECORD, int) dataset = app.wiring.book_dao.search_book( name=f_name, lang=f_lang, series=f_series, keyword=f_keyword, genre=f_genre, skip=f_skip, limit=f_limit) result = [row2dict(row) for row in dataset] json_data = json.dumps(result, ensure_ascii=False).encode('utf8') response = app.response_class( response=json_data, status=200, mimetype='application/json' ) return response