def subscribe_multi(self, podcasts): """Subscribe the user to multiple podcasts. podcasts should be an iterable of Podcast objects.""" not_already_subscribed = [] for podcast in podcasts: if podcast.url not in self.subscriptions: not_already_subscribed.append(podcast.url) self.run(self.table.get(self.id).update( {"subscriptions": r.row["subscriptions"].set_union(not_already_subscribed)} )) Podcast.run(Podcast.get_table().get_all(r.args(not_already_subscribed)).update({ "subscribers": (r.row["subscribers"].default(0) + 1) })) return SubscribeResult(success=True)
def get_multi_by_url(cls, urls=[]): """Given a list of urls, returns a dictionary mapping from url to podcast.""" urls = set(urls) d = {} podcasts = [cls.from_dict(p) for p in cls.run(cls.get_table().get_all(r.args(urls)))] for pod in podcasts: urls.remove(pod.url) d[pod.url] = pod moved_podcasts = cls.run( cls.get_table().filter(lambda pod: pod["previous_urls"].set_intersection(urls).count() > 0) ) for pod in moved_podcasts: for key in urls.intersection(pod["previous_urls"]): d[key] = cls.from_dict(pod) return d
def get_multi_by_url(cls, urls=[]): """Given a list of urls, returns a dictionary mapping from url to podcast.""" urls = set(urls) d = {} podcasts = [ cls.from_dict(p) for p in cls.run(cls.get_table().get_all(r.args(urls))) ] for pod in podcasts: urls.remove(pod.url) d[pod.url] = pod moved_podcasts = cls.run(cls.get_table().filter(lambda pod: pod[ "previous_urls"].set_intersection(urls).count() > 0)) for pod in moved_podcasts: for key in urls.intersection(pod["previous_urls"]): d[key] = cls.from_dict(pod) return d
def get_subscriptions(self): """Get the Podcast objects of podcasts the user has subscribed to.""" if len(self.subscriptions) == 0: return [] res = self.run(Podcast.get_table().get_all(r.args(self.subscriptions))) return [Podcast.from_dict(p) for p in res]