def authors_from_ids(idlist): ''' build a list of Author objects based on a list of author.gut_id Used to overcome large SELECT IN SQL stmts which peewee complains about. Slower !! ''' authors = [] for author in Author.select().order_by(Author.last_name.asc(), Author.first_names.asc()): if author.gut_id not in idlist: continue if author in authors: continue authors.append(author) return authors
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) # language-specific collections for lang_name, lang, lang_count in avail_langs: # 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 for that lang authors = Author.select().where( Author.gut_id << list(set([book.author.gut_id for book in books.filter(language=lang)]))) logger.info("\t\tDumping authors_lang_{}.js".format(lang)) dumpjs([author.to_array() for author in authors.order_by(Author.last_name.asc(), Author.first_names.asc())], 'authors_lang_{}.js'.format(lang), 'authors_json_data') # author specific collections authors = Author.select().where( Author.gut_id << list(set([book.author.gut_id for book in books]))) for author in authors: # 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.order_by(Author.last_name.asc(), Author.first_names.asc())], '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')