def subscribe(request): """ Add a subscription, and specify tags to go with the subscription. If a feed can not be found at the given URL, or you already subscribe to the given URL, a failure with an appropriate message is returned. Upon success, the json response contains details about the subsctipion for updating the UI. """ # FIXME: should implement real logging at some point print request.POST feed_url = request.POST.get("feed_url") tag_vals = request.POST.getlist("tags[]") feed = services.feed_for_url(feed_url) if feed == None: print "a" resp_body = _failure_json("A feed could not be found for the supplied url.") else: # Check if the user is already subscribed subs = Subscription.objects.filter(subscriber=request.user, feed=feed) if subs.count() > 0: resp_body = _failure_json("You already have a subscription for the supplied url.") else: if not feed.pk: # then it's a new feed feed.save() new_sub = Subscription(feed=feed, subscriber=request.user) new_sub.save() tags = Tag.get_or_create(tag_vals) new_sub.tags.add(*tags) new_sub.save() resp_body = _subscription_json(new_sub) return _json_http_response(resp_body)
def update_subscription(request): """ Update a subsctiption. For now, only the tag set can be updated. """ # FIXME: should implement real logging at some point print request.POST sub_id = request.POST.get("subscription_id") tag_vals = request.POST.getlist("tags[]") sub = Subscription.objects.filter(id=sub_id)[0] sub.tags.clear() tags = Tag.get_or_create(tag_vals) sub.tags.add(*tags) sub.save() # The tags got trimmed and dupes removed -- see Tag.get_or_create() result_tag_vals = [t.tag for t in tags] # Pass back the subscription_id, too, as a convenience for updating # the displayed tags print "result_tags: " print result_tag_vals resp_content = {"subscription_id": sub_id, "tags": result_tag_vals} return _json_http_response(_success_json("content", resp_content))