Пример #1
0
def new() -> Union[str, Response]:
    """
    Handles the RESTful NEW (GET method) and CREATE (POST method) routes.

    Returns
    -------
    str
        The INDEX template if POST method, NEW template otherwise.
    """
    if request.method == 'POST':
        try:
            alert_name = request.form['name']
            item_url = request.form['item_url']
            price_limit = float(request.form['price_limit'])

            store = Store.find_by_url(item_url)

            item = Item(item_url, store.tag_name, store.query)
            item.load_price()
            logger.debug(f"item: {item}")
            item.save_to_mongo()

            alert = Alert(alert_name, price_limit, session['email'], item._id)
            logger.debug(f"alert: {alert}")
            alert.save_to_mongo()

            alert.notify_if_price_reached()

            return redirect(url_for('.index'))
        except requests.exceptions.RequestException as err:
            logger.debug(f"Error with Alert NEW POST request: {err}")
            flash(
                "Sorry, there was an issue connecting to that website!" +
                " Try again later, or try another website.", 'danger')
        except Exception as err:
            logger.debug(f"Error with Alert NEW POST request: {err}")
            flash(
                "There was problem creating your Alert, please check your form input again.",
                'danger')
        return redirect(url_for('.new'))

    return render_template('alerts/new.html')
Пример #2
0
from models.alert import Alert

alert = Alert(item_id="26beafafee7e4391a02d54b54fe90d73", price_limit=2000)
alert.save_to_mongo()