def json_resp_from_thing(thing, is_already_a_dict=False): # @is_already_a_dict saves a bit of time if you've already converted to dict. if not is_already_a_dict: my_dict = util.todict(thing) json_str = json.dumps(my_dict, sort_keys=True, indent=4) if request.path.endswith(".json") and (os.getenv("FLASK_DEBUG", False) == "True"): logger.info(u"rendering output through debug_api.html template") resp = make_response(render_template( 'debug_api.html', data=json_str)) resp.mimetype = "text/html" return views_helpers.bust_caches(resp) resp = make_response(json_str, 200) resp.mimetype = "application/json" return views_helpers.bust_caches(resp)
def get_json(self): return todict(self)
def profile_products_get(url_slug): action = request.args.get("action", "refresh") source = request.args.get("source", "webapp") timer = util.Timer() load_times = {} just_stubs = request.args.get("stubs", "False").lower() in ["1", "true"] if just_stubs: profile = get_profile_stubs_from_url_slug(url_slug) if not profile: abort_json(404, "This profile does not exist.") load_times["profile"] = timer.elapsed() product_list = [ {"tiid": p.tiid, "genre": p.genre} for p in profile.products_not_removed if p.genre not in ["account"] ] load_times["product_list"] = timer.since_last_check() else: profile = get_profile_from_id(url_slug) if not profile: abort_json(404, "This profile does not exist.") markup = Markup(url_slug, embed=False) load_times["profile"] = timer.elapsed() product_list = profile.get_products_markup( markup=markup, show_keys=[ # for rendering biblio "biblio", "embed_markup", "_tiid", "tiid", "markup", "countries_str", # for sorting "year", "title", "awardedness_score", "metrics_raw_sum", "authors", # misc "genre", "genre_icon" ] ) load_times["product_list"] = timer.since_last_check() product_dicts_list = util.todict(product_list) resp = { "a_load_times": load_times, "is_refreshing": profile.is_refreshing, "list": product_dicts_list } return json_resp_from_thing(resp)