def subscribe(rss_url): '''Logic to add a given rss feed to the db, if required, and subscribe the current user to this feed''' try: feed = Feed.get_or_construct(rss_url) except NotAFeed: return jsonify(dict(status = 'Error', message='The given url is not an rss feed url')) new_subscription = Subscription(feed_id = feed.id) #atomically add new feed subscription to current user number_of_items_affected = User.objects(id = current_user.id).update_one(add_to_set__subscriptions = new_subscription) new_reader = Reader(user_id = current_user.id) #atomically add current user to readers of all articles in subscribed feed Article.objects(feed_id = feed.id).update(add_to_set__readers = new_reader) if number_of_items_affected is 1: return jsonify(dict(status = 'Success')) else: return jsonify(dict(\ status = 'Error'\ , message = 'Unable to subscribe'\ ))
def subscribe(rss_url): '''Logic to add a given rss feed to the db, if required, and subscribe the current user to this feed''' try: feed = Feed.get_or_construct(rss_url) except NotAFeed: return jsonify( dict(status='Error', message='The given url is not an rss feed url')) new_subscription = Subscription(feed_id=feed.id) #atomically add new feed subscription to current user number_of_items_affected = User.objects(id=current_user.id).update_one( add_to_set__subscriptions=new_subscription) new_reader = Reader(user_id=current_user.id) #add current user to readers of all articles in subscribed feed, currently cannot be chained into one statement for article in Article.objects( feed_id=feed.id).order_by("-time_stamp").limit(10): article.update(add_to_set__readers=new_reader) if number_of_items_affected is 1: return jsonify(dict(status='Success')) else: return jsonify(dict(\ status = 'Error'\ , message = 'Unable to subscribe'\ ))