def bookmarklet(): feed_contr = FeedController(current_user.id) url = (request.args if request.method == "GET" else request.form).get( "url", None) if not url: flash(gettext("Couldn't add feed: url missing."), "error") raise BadRequest("url is missing") feed_exists = list(feed_contr.read(__or__={"link": url, "site_link": url})) if feed_exists: flash(gettext("Couldn't add feed: feed already exists."), "warning") return redirect(url_for("feed.form", feed_id=feed_exists[0].id)) try: feed = construct_feed_from(url) except requests.exceptions.ConnectionError: flash(gettext("Impossible to connect to the address: {}.".format(url)), "danger") return redirect(url_for("home")) except Exception: logger.exception("something bad happened when fetching %r", url) return redirect(url_for("home")) if not feed.get("link"): feed["enabled"] = False flash( gettext("Couldn't find a feed url, you'll need to find a Atom or" " RSS link manually and reactivate this feed"), "warning", ) feed = feed_contr.create(**feed) flash(gettext("Feed was successfully created."), "success") if feed.enabled and application.config["CRAWLING_METHOD"] == "default": misc_utils.fetch(current_user.id, feed.id) flash(gettext("Downloading articles for the new feed..."), "info") return redirect(url_for("feed.form", feed_id=feed.id))
def process_form(feed_id=None): form = AddFeedForm() feed_contr = FeedController(current_user.id) form.set_category_choices(CategoryController(current_user.id).read()) if not form.validate(): return render_template("edit_feed.html", form=form) existing_feeds = list(feed_contr.read(link=form.link.data)) if existing_feeds and feed_id is None: flash(gettext("Couldn't add feed: feed already exists."), "warning") return redirect(url_for("feed.form", feed_id=existing_feeds[0].id)) # Edit an existing feed feed_attr = { "title": form.title.data, "enabled": form.enabled.data, "link": form.link.data, "site_link": form.site_link.data, "filters": [], "category_id": form.category_id.data, "private": form.private.data, } if not feed_attr["category_id"] or feed_attr["category_id"] == "0": del feed_attr["category_id"] for filter_attr in ("type", "pattern", "action on", "action"): for i, value in enumerate( request.form.getlist(filter_attr.replace(" ", "_"))): if i >= len(feed_attr["filters"]): feed_attr["filters"].append({}) feed_attr["filters"][i][filter_attr] = value if feed_id is not None: feed_contr.update({"id": feed_id}, feed_attr) flash( gettext( "Feed %(feed_title)r successfully updated.", feed_title=feed_attr["title"], ), "success", ) return redirect(url_for("feed.form", feed_id=feed_id)) # Create a new feed new_feed = feed_contr.create(**feed_attr) flash( gettext("Feed %(feed_title)r successfully created.", feed_title=new_feed.title), "success", ) if application.config["CRAWLING_METHOD"] == "default": misc_utils.fetch(current_user.id, new_feed.id) flash(gettext("Downloading articles for the new feed..."), "info") return redirect(url_for("feed.form", feed_id=new_feed.id))
def process_form(feed_id=None): form = AddFeedForm() feed_contr = FeedController(current_user.id) form.set_category_choices(CategoryController(current_user.id).read()) if not form.validate(): return render_template("edit_feed.html", form=form) existing_feeds = list( feed_contr.read(link=form.link.data, site_link=form.site_link.data)) if existing_feeds and feed_id is None: flash(gettext("Couldn't add feed: feed already exists."), "warning") return redirect(url_for("feed.form", feed_id=existing_feeds[0].id)) feed_attr = { "title": form.title.data, "enabled": form.enabled.data, "link": form.link.data, "site_link": form.site_link.data, "filters": [], "category_id": form.category_id.data, "private": form.private.data, } if not feed_attr["category_id"] or feed_attr["category_id"] == "0": del feed_attr["category_id"] for filter_attr in ("type", "pattern", "action on", "action"): for i, value in enumerate( request.form.getlist(filter_attr.replace(" ", "_"))): if i >= len(feed_attr["filters"]): feed_attr["filters"].append({}) feed_attr["filters"][i][filter_attr] = value if feed_id is not None: # Edit an existing feed feed_contr.update({"id": feed_id}, feed_attr) flash( gettext( "Feed %(feed_title)r successfully updated.", feed_title=feed_attr["title"], ), "success", ) return redirect(url_for("feed.form", feed_id=feed_id)) # Create a new feed url = form.site_link.data if form.site_link.data else form.link.data try: feed = construct_feed_from(url) except requests.exceptions.ConnectionError: flash(gettext("Impossible to connect to the address: {}.".format(url)), "danger") return redirect(url_for("home")) except Exception: logger.exception("something bad happened when fetching %r", url) return redirect(url_for("home")) del feed_attr["link"] del feed_attr["site_link"] # remove keys with empty strings feed_attr = {k: v for k, v in feed_attr.items() if v != ""} feed.update(feed_attr) new_feed = feed_contr.create(**feed) flash( gettext("Feed %(feed_title)r successfully created.", feed_title=new_feed.title), "success", ) if application.config["CRAWLING_METHOD"] == "default": misc_utils.fetch(current_user.id, new_feed.id) flash(gettext("Downloading articles for the new feed..."), "info") return redirect(url_for("feed.form", feed_id=new_feed.id))