def _subscribe_multi(uid, urls): sources = {url: ensure_source(url) for url in urls} # TODO: make 4 a shared value source_json_futures = {url: source.json(include_articles=True, return_promise=True, article_limit=4) for url, source in sources.iteritems()} source_json = {url: f() for url, f in source_json_futures.iteritems()} source_json = {url: source_json for url, source_json in source_json.iteritems() if len(source_json['articles'])} subscription_futures = {url: Subscription.get_or_insert_async(Subscription.id_for_subscription(source.url, uid)) for url, source in sources.iteritems()} subscriptions = {url: f.get_result() for url, f in subscription_futures.iteritems()} for url, sub in subscriptions.iteritems(): canonical_url = sources[url].url sub.url = canonical_url sub.uid = uid ndb.put_multi(subscriptions.values()) Feed.get_for_user(uid).update_in_place(just_added_sources_json=source_json.values()) sub_json_promises = {url: sub.json(return_promise=True) for url, sub in subscriptions.iteritems()} sub_jsons = {url: f() for url, f in sub_json_promises.iteritems()} return {"source_jsons": source_json, "subscription_jsons": sub_jsons}
def unsubscribe(uid, url): ndb.Key(Subscription, Subscription.id_for_subscription(url, uid)).delete() Feed.get_for_user(uid).update_in_place(just_removed_source_urls=[url]) return True