def new_alert():
    """
    For making a new alert we have to follow the following steps
    ~ Get the form data
    ~ Find the store to which the alert is associated to
    ~ Create an Item object
    ~ Find the price of that particular item
    ~ Save the item into the database
    ~ Create an Alert object
    ~ Save the Alert object into the database
    :return: render_template()
    """
    if request.method == 'POST':
        item_url = request.form['item_url']
        price_limit = float(request.form['price_limit'])
        alert_name = request.form['name']

        store = Store.find_by_url(item_url)
        item = Item(item_url, store.tag_name, store.query)

        item.load_price()
        item.save_to_mongo()

        Alert(alert_name, item._id, price_limit,
              session['email']).save_to_mongo()

    return render_template('alerts/new_alert.html')
Exemple #2
0
def new_alert():
    if request.method == "POST":
        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()
        item.save_to_mongo()
        Alert(alert_name, item._id, price_limit, session["email"]).save_to_mongo()
    return render_template("alerts/new_alert.html")
Exemple #3
0
def new_alert():
    if request.method == 'POST':
        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, name)
        item.load_price()
        item.save_to_mongo()
        Alert(price_limit, item._id, session['user_id']).save_to_mongo()
        return redirect(url_for('.all_alerts'))
    return render_template('alerts/new_alert.html')
Exemple #4
0
def new_alert():
    if request.method == 'POST':
        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()
        item.save_to_mongo()
        Alert(alert_name, item._id, price_limit,
              session['email']).save_to_mongo()
    return render_template('alerts/new_alert.html')
def new_alert():
    if request.method == "POST":
        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.save_to_mongo()

        Alert(item._id, price_limit).save_to_mongo()
        return make_response(index())

    return render_template("alerts/new_alert.html")
Exemple #6
0
def create_alert():
    if request.method == 'POST':
        item_url = request.form['item_url']

        store = Store.find_by_url(item_url)
        item = Item(item_url, store.tag_name, store.query)
        item.save_to_mongo()

        price_limit = request.form['price_limit']

        Alert(item._id, price_limit).save_to_mongo()

    # What happens if it's a GET request
    return render_template("alerts/new_alert.html")
Exemple #7
0
def create_alert():
    if request.method == 'POST':
        item_url = request.form["item_url"]

        store = Store.find_by_url(item_url)
        item = Item(item_url, store.tag_name, store.query)
        item.load_price()
        item.save_to_mongo()

        alert_name = request.form['name']
        price_limit = float(request.form["price_limit"])
        Alert(alert_name, item._id, price_limit,
              session['email']).save_to_mongo()
    # What happens if it's a GET request
    return render_template("alerts/new_alert.html")
Exemple #8
0
def new_alert():
    if request.method == 'POST':
        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()
        item.persist()
        alert = Alert(alert_name, item._id, price_limit, session['email'])
        alert.persist()
        alerts = Alert.all()
        return redirect(url_for('.index', alerts=alerts))
    else:
        return render_template('/alerts/new_alert.html')
Exemple #9
0
def new_alert():
    if request.method == 'POST':
        item_url = request.form['item_url']
        price_limit = float(request.form['price_limit'])
        name = request.form['name']
        store = Store.find_by_url(item_url)

        item = Item(item_url, store.tag_name, store.query)
        item.load_price()
        item.save_to_mongo()

        Alert(item._id, name, price_limit, session['email']).save_to_mongo()

        return redirect(url_for('.index'))

    return render_template("alerts/new_alert.html")
Exemple #10
0
def create_alert():
    if request.method == 'POST':
        item_url = request.form['item_url']
        store = Store.find_by_url(item_url)
        item = Item(item_url, store.tag_name, store.query)
        item.load_price()
        item.save_to_mongo()

        alert_name = request.form['name']
        price_limit = request.form['price_limit']

        Alert(alert_name, item._id, price_limit).save_to_mongo()
        return redirect(url_for('.index'))

    # What happens if it's a GET request
    return render_template("alerts/new_alert.html")
Exemple #11
0
def new():
    """Adds a new alert."""
    if request.method == 'POST':
        item_name = request.form['item-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.html_tag_name, store.html_tag_attributes)
        item.fetch_price()
        item.save_to_db()

        alert = Alert(item_name, item._id, price_limit, session['email'])
        alert.save_to_db()

        return redirect(url_for('.index'))

    return render_template('alerts/new.html')
Exemple #12
0
def new_alert():
    if request.method == "POST":
        item_url = request.form["item_url"]

        store = Store.find_by_url(item_url)
        item = Item(url=item_url, tag_name=store.tag_name, query=store.query)
        item.load_price()
        item.save_to_mongo()

        alert_name = request.form["name"]
        price_limit = request.form["price_limit"]

        Alert(name=alert_name,
              item_id=item._id,
              price_limit=price_limit,
              user_email=session["email"]).save_to_mongo()

    return render_template("alerts/new_alert.html")
Exemple #13
0
def new_alert():
    """
    browser make a get request when access endpoint, endpoint will be respond to it with the html be rendered
    if receive a post request which is the form is gonna make, to take the data in form and process it
    """
    if request.method == 'POST':
        alert_name = request.form['name']
        price_limit = float(request.form['price_limit'])
        item_url = request.form['item_url']  # access the url field in the form data that's come in the request
        # such as: http://www.johnlewis.com/item/index.html

        store = Store.find_by_url(item_url)
        item = Item(item_url, store.tag_name, store.query)
        item.load_price()  # when they go to alerts.index page, item will have a price, don't need call load price on it
        item.save_to_mongo()

        Alert(alert_name, item._id, price_limit, session['email']).save_to_mongo()

    return render_template('alerts/new_alert.html')
Exemple #14
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')
Exemple #15
0
def new_alert():
    if request.method == 'POST':
        item_url = request.form['item_url']
        price_limit = float(request.form['price_limit'].strip())
        name = request.form['name']

        store = Store.find_by_url(item_url)

        item = Item(item_url, store.tag_name, store.query)
        item.load_price()
        item.save_to_mongo()

        Alert(item._id, name, price_limit, session['email']).save_to_mongo()
        alerts = Alert.find_many_by('user_email', session['email'])
        for i in alerts:
            i.__post__init__()
        return render_template("alerts/index.html",
                               alerts=alerts,
                               current=session['email'])
    return render_template("alerts/new_alert.html")
Exemple #16
0
def new_alert():
    if request.method == 'POST':
        # 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()
        item.save_to_mongo()

        alert_name = request.form["name"]
        price_limit = float(request.form["price_limit"])

        Alert(alert_name, item._id, price_limit,
              session['email']).save_to_mongo()
        # Warning is due to item._id is private object and not supposed to be modified outside of class.
        # However in this case, it is okay as it is used to retrieve data.

    # What happens if it's a GET request
    return render_template('alerts/new_alert.html')
Exemple #17
0
def new_alert(): ## Must be logged in to save an alert
    """
    This endpoint allows a user to enter a new alert and save to the database
    :return: Alert index for the application once alert is successfully added
    """

    if request.method == 'POST':
        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()
        item.save_to_mongo()

        ## Using protected here is fine since we are not changing item._id
        Alert(alert_name, item._id, price_limit, session['email']).save_to_mongo()

        return redirect(url_for('.index'))

    return render_template('alerts/new_alert.html')
def new_alert():
    if request.method == "POST":
        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()
        # item.save_to_mongo()

        # Alert(alert_name, item._id, price_limit, session["email"]).save_to_mongo()
        try:
            store = Store.find_by_url(item_url)
            item = Item(item_url, store.tag_name, store.query)
            item.load_price()
            item.save_to_mongo()
            Alert(alert_name, item._id, price_limit,
                  session["email"]).save_to_mongo()
        except StoreErrors.StoreNotFound as error:
            return render_template("alerts/error.html", error=error)

    return render_template("alerts/new_alert.html")
Exemple #19
0
def new_alert():
    if request.method == 'POST':
        # process the Data from the Form.
        # access the Item_ID, & Item_Price from the form
        alert_name = request.form['name_of_alert']
        item_url = request.form['item_url']
        price_limit = float(
            request.form['price_limit'])  # Str needs to converted to Float

        store = Store.find_by_url(item_url)

        item = Item(alert_name, item_url, store.tag_name, store.query)
        print(f"ITEM LOAD PRICE::: {item}")
        item.load_price()
        print(f"ITEM PRICE is found = {item.price}")
        print(f"ITEM AFTER PRICE is found ===== {item}")
        print(f"URL = {item_url}")

        my_item = Item.get_by_name(alert_name)
        print(f"FIND ITEM found ===== {my_item}")

        if my_item is None:
            print("SAVING ITEM ::::::::::: INTO MONGO")
            item.save_to_mongo()
        else:
            # assign found item's item._id to be passed to Alert module.
            item._id = my_item._id

        # item.save_to_mongo()

        # The warning below is fine as we are using but not changing the protected variable
        #print(f"Aname: {alert_name}  IT_id = {item._id}  2ndIT_id = {my_item._id}  PRICE = {price_limit}  MAIL: {session['rks_email']}")
        Alert(alert_name, item._id, price_limit,
              session['rks_email']).save_to_mongo()

    return render_template('alerts/new_alert.html')
 def __post_init__(self):
     store = Store.find_by_url(self.url)
     self.tag_name = store.tag_name
     self.query = store.query