Exemplo n.º 1
0
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))
Exemplo n.º 2
0
def reset_errors(feed_id):
    feed_contr = FeedController(current_user.id)
    feed = feed_contr.get(id=feed_id)
    feed_contr.update({"id": feed_id}, {"error_count": 0, "last_error": ""})
    flash(
        gettext("Feed %(feed_title)r successfully updated.",
                feed_title=feed.title),
        "success",
    )
    return redirect(request.referrer or url_for("home"))
Exemplo n.º 3
0
def process_form(category_id=None):
    form = CategoryForm()
    form.set_feed_choices(FeedController(current_user.id).read())
    cat_contr = CategoryController(current_user.id)
    feed_contr = FeedController(current_user.id)

    if not form.validate():
        return render_template("edit_category.html", form=form)
    existing_cats = list(cat_contr.read(name=form.name.data))
    if existing_cats and category_id is None:
        flash(gettext("Couldn't add category: already exists."), "warning")
        return redirect(
            url_for("category.form", category_id=existing_cats[0].id))

    # Edit an existing category
    category_attr = {"name": form.name.data}
    if category_id is not None:
        cat_contr.update({"id": category_id}, category_attr)
        # Update the relation with feeds
        for feed_id in form.feeds.data:
            feed_contr.update({"id": feed_id}, {"category_id": category_id})
        for feed in current_user.feeds:
            if feed.category_id == category_id and feed.id not in form.feeds.data:
                feed_contr.update({"id": feed.id}, {"category_id": None})

        flash(
            gettext(
                "Category %(cat_name)r successfully updated.",
                cat_name=category_attr["name"],
            ),
            "success",
        )
        return redirect(url_for("category.form", category_id=category_id))

    # Create a new category
    new_category = cat_contr.create(**category_attr)
    # Update the relation with feeds
    for feed_id in form.feeds.data:
        feed_contr.update({"id": feed_id}, {"category_id": new_category.id})

    flash(
        gettext(
            "Category %(category_name)r successfully created.",
            category_name=new_category.name,
        ),
        "success",
    )

    return redirect(url_for("category.form", category_id=new_category.id))
Exemplo n.º 4
0
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))