def unsubscribe(email, category_id): category = Category.by_id(category_id) subs = Subscription.find_subscription(email, category_id) subs.delete_instance() category.decrement()
def get_category(category_id=None): """ Return a JSON version of the category. :param category_id: :return: Response """ try: category = Category.by_id(category_id) except Exception: return jsonify(Category.all()) else: return jsonify(category.to_dict())
def subscribe(email, category_id): category = Category.by_id(category_id) try: Subscription.find_subscription(email, category_id) except Subscription.NotFound: subs = Subscription.create(user=User.get_user_by_email(email), category=category) else: raise Subscription.Duplicate category.increment() return subs
def edit_category(category_id): """ Update the given category by the ID. :return: """ try: name = request.json["name"] except KeyError: return make_json_response(status=400) else: try: category = Category.by_id(category_id) except Exception: return make_json_response(status=404) else: category.update_with(name) return jsonify(category.to_dict())