def export_to_json_helpers(books, static_folder, languages, formats): def dumpjs(col, fn, var='json_data'): with open(os.path.join(static_folder, fn), 'w') as f: f.write("var {var} = ".format(var=var)) f.write(json.dumps(col)) f.write(";") # json.dump(col, f) # all books sorted by popularity logger.info("\t\tDumping full_by_popularity.js") dumpjs([book.to_array() for book in books.order_by(Book.downloads.desc())], 'full_by_popularity.js') # all books sorted by title logger.info("\t\tDumping full_by_title.js") dumpjs([book.to_array() for book in books.order_by(Book.title.asc())], 'full_by_title.js') avail_langs = get_langs_with_count(books=books) all_filtered_authors = [] # language-specific collections for lang_name, lang, lang_count in avail_langs: lang_filtered_authors = list( set([book.author.gut_id for book in books.filter(language=lang)])) for aid in lang_filtered_authors: if aid not in all_filtered_authors: all_filtered_authors.append(aid) # by popularity logger.info("\t\tDumping lang_{}_by_popularity.js".format(lang)) dumpjs([ book.to_array() for book in books.where( Book.language == lang).order_by(Book.downloads.desc()) ], 'lang_{}_by_popularity.js'.format(lang)) # by title logger.info("\t\tDumping lang_{}_by_title.js".format(lang)) dumpjs([ book.to_array() for book in books.where( Book.language == lang).order_by(Book.title.asc()) ], 'lang_{}_by_title.js'.format(lang)) authors = authors_from_ids(lang_filtered_authors) logger.info("\t\tDumping authors_lang_{}.js".format(lang)) dumpjs([author.to_array() for author in authors], 'authors_lang_{}.js'.format(lang), 'authors_json_data') # author specific collections authors = authors_from_ids(all_filtered_authors) for author in authors: # all_filtered_authors.remove(author.gut_id) # by popularity logger.info("\t\tDumping auth_{}_by_popularity.js".format( author.gut_id)) dumpjs([ book.to_array() for book in books.where( Book.author == author).order_by(Book.downloads.desc()) ], 'auth_{}_by_popularity.js'.format(author.gut_id)) # by title logger.info("\t\tDumping auth_{}_by_title.js".format(author.gut_id)) dumpjs([ book.to_array() for book in books.where( Book.author == author).order_by(Book.title.asc()) ], 'auth_{}_by_title.js'.format(author.gut_id)) # authors list sorted by name logger.info("\t\tDumping authors.js") dumpjs([author.to_array() for author in authors], 'authors.js', 'authors_json_data') # languages list sorted by code logger.info("\t\tDumping languages.js") dumpjs(avail_langs, 'languages.js', 'languages_json_data') # languages by weight main_languages, other_languages = get_lang_groups(books) logger.info("\t\tDumping main_languages.js") dumpjs(main_languages, 'main_languages.js', 'main_languages_json_data') dumpjs(other_languages, 'other_languages.js', 'other_languages_json_data')
def export_to_json_helpers(books, static_folder, languages, formats): def dumpjs(col, fn, var='json_data'): with open(os.path.join(static_folder, fn), 'w') as f: f.write("var {var} = ".format(var=var)) f.write(json.dumps(col)) f.write(";") # json.dump(col, f) # all books sorted by popularity logger.info("\t\tDumping full_by_popularity.js") dumpjs([book.to_array() for book in books.order_by(Book.downloads.desc())], 'full_by_popularity.js') # all books sorted by title logger.info("\t\tDumping full_by_title.js") dumpjs([book.to_array() for book in books.order_by(Book.title.asc())], 'full_by_title.js') avail_langs = get_langs_with_count(books=books) all_filtered_authors = [] # language-specific collections for lang_name, lang, lang_count in avail_langs: lang_filtered_authors = list( set([book.author.gut_id for book in books.filter(language=lang)])) for aid in lang_filtered_authors: if aid not in all_filtered_authors: all_filtered_authors.append(aid) # by popularity logger.info("\t\tDumping lang_{}_by_popularity.js".format(lang)) dumpjs( [book.to_array() for book in books.where(Book.language == lang) .order_by(Book.downloads.desc())], 'lang_{}_by_popularity.js'.format(lang)) # by title logger.info("\t\tDumping lang_{}_by_title.js".format(lang)) dumpjs( [book.to_array() for book in books.where(Book.language == lang) .order_by(Book.title.asc())], 'lang_{}_by_title.js'.format(lang)) authors = authors_from_ids(lang_filtered_authors) logger.info("\t\tDumping authors_lang_{}.js".format(lang)) dumpjs([author.to_array() for author in authors], 'authors_lang_{}.js'.format(lang), 'authors_json_data') # author specific collections authors = authors_from_ids(all_filtered_authors) for author in authors: # all_filtered_authors.remove(author.gut_id) # by popularity logger.info( "\t\tDumping auth_{}_by_popularity.js".format(author.gut_id)) dumpjs( [book.to_array() for book in books.where(Book.author == author) .order_by(Book.downloads.desc())], 'auth_{}_by_popularity.js'.format(author.gut_id)) # by title logger.info("\t\tDumping auth_{}_by_title.js".format(author.gut_id)) dumpjs( [book.to_array() for book in books.where(Book.author == author) .order_by(Book.title.asc())], 'auth_{}_by_title.js'.format(author.gut_id)) # authors list sorted by name logger.info("\t\tDumping authors.js") dumpjs([author.to_array() for author in authors], 'authors.js', 'authors_json_data') # languages list sorted by code logger.info("\t\tDumping languages.js") dumpjs(avail_langs, 'languages.js', 'languages_json_data') # languages by weight main_languages, other_languages = get_lang_groups(books) logger.info("\t\tDumping main_languages.js") dumpjs(main_languages, 'main_languages.js', 'main_languages_json_data') dumpjs(other_languages, 'other_languages.js', 'other_languages_json_data')
def get_default_context(books): return { 'l10n_strings': json.dumps(l10n_strings), 'ui_languages': ['en', 'fr'], 'languages': get_langs_with_count(books=books), }