Example #1
0
  def get(self):
    place_name = self.request.get("p", None)
    callback = self.request.get("callback", None)

    if not place_name:
      raise InvalidPlace(" ")
    elif place_name == "favicon.ico":
      """ This happens so often, it's annoying. """
      self.response.set_status(204)
    else:
      place_name = urllib2.unquote(place_name)
      p = memcache.get(self.PLACE_KEY % place_name)
      if not p:
        p = Place(place_name)
        memcache.set(self.PLACE_KEY % place_name, p) # cache indefinitely

      alerts = memcache.get(self.ALERT_KEY % place_name)
      if not alerts:
        alerts = Alert.alerts_for(p)
        try:
          memcache.set(self.ALERT_KEY % place_name, alerts, time=10) # cache a SHORT time
        except:
          logging.error('Error setting memcache.')
          logging.error(''.join(traceback.format_exception(*sys.exc_info())))

      p.alerts = alerts
      javascript = json.dumps(p, sort_keys=True, indent=2, cls=PlaceEncoder)

      self.response.headers['Content-Type'] = 'text/javascript'
      self.response.out.write("%s(%s)" % (callback, javascript) if callback else javascript)
Example #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')
Example #3
0
def edit_alert(alert_id):
    alert = Alert.get_by_id(alert_id)

    if request.method == "POST":
        price_limit = float(request.form["price_limit"])
        name = request.form["name"]

        alert.price_limit = price_limit
        alert.name = name
        alert.save_to_mongo()

        return redirect(url_for("alert.index"))

    return render_template("edit_alert.html", alert=alert)
Example #4
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")
Example #5
0
def new_alert():
    if request.method == "POST":
        alert_name = request.form["alert_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.attrs)
        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 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()

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

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

    # What happens if it's a GET request
    return render_template("alerts/new_alert.html")
Example #7
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).save_to_mongo()
        return redirect(url_for(".index"))

    return render_template("alerts/new_alert.html")
Example #8
0
def edit_alert(alert_id):
    alert = Alert.get_by_id(alert_id)
    if request.method == 'POST':
        for a in alert:
            if a.user_email == session['email']:
                Price_limit = request.form['price_limit']

                a.Price_limit = Price_limit
                a.get_item_url()
                print(a.item_url)
                print(a.name)
                a.update_to_mongo()
                text = "alert has been successfully edited"
                return redirect(url_for('.index', text=text))
    print(alert)
    return render_template('alerts/edit_alert.html', alert=alert)
Example #9
0
def create_alert():
    if request.method == 'POST':
        subject_url = request.form['subject_url']

        course = Course.find_by_url(subject_url)
        subject = Subject(subject_url, course.tag_name, course.query)
        subject.load_price()
        subject.save_to_mongo()

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

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

    # What happens if it's a GET request
    return render_template("alerts/new_alert.html")
Example #10
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")
Example #11
0
def edit_alert(alert_id):
    """
    This endpoint allows a user to edit an existing alert and save to the database
    :return: Alert index for the application once alert is successfully edited
    """
    alert = Alert.get_by_id(alert_id)

    if request.method == 'POST':
        price_limit = float(request.form['price_limit'])

        alert.price_limit = price_limit
        alert.save_to_mongo()

        return redirect(url_for('.index'))

    return render_template('alerts/edit_alert.html', alert=alert)
Example #12
0
def create_alert():
    if request.method == 'POST':
        item_url = request.form['item_url']
        alert_name = request.form['name']
        price_limit = float(request.form['price_limit'])
        print('ITEM URL', item_url)

        store = Store.find_by_url(item_url)
        item = Dolar(item_url, store.tag_name)
        item.buscar_precio()
        item.save_to_mongo()

        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")
Example #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')
Example #14
0
def delete(alert_id: str) -> Union[str, Response]:
    """
    Handles the RESTful DESTROY route.

    Parameters
    ----------
    alert_id : str
        The :class:`models.alert.Alert` id

    Returns
    -------
    str
        The INDEX template.
    """
    alert = Alert.get_by_id(alert_id)
    if alert.user_email == session['email']:
        logger.debug(f"deleting alert: {alert}")
        alert.remove_from_mongo()
    return redirect(url_for('.index'))
Example #15
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')
Example #16
0
def new_alert():
    if request.method == 'POST':
        item_name = request.form['item_name']
        item_url = request.form['item_url']
        user_email = session['email']
        price_limit = float(request.form['price_limit'])
        store = Store.get_by_url_self_made(item_url)
        print(store)
        if len(store) is 0:
            text1 = "The store doesnot exist in our database, please contact develpoers to add store."
            return render_template('alerts/new_alert.html', text=text1)
        else:
            for s in store:
                item = Item(item_url, s.tag_name, s.query)
            item.load_price()
            item.save_to_mongo()
            Alert(item_name, item._id, user_email, price_limit,
                  item_url).save_to_mongo()
            text1 = "The alert has been saved to the database, if you want to save another item fill details and click button."
            return render_template('alerts/new_alert.html', text=text1)
    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")
Example #18
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')
Example #19
0
    def do_POST(self):
        if self.client_address[0] not in self.valid_ips:
            self.logger.warning("Unauthorized IP address: %s",
                                self.client_address[0])
            self.send_response(401)
            self.end_headers()
        else:
            self.logger.debug("Received alert from IP address: %s",
                              self.client_address[0])

            self.send_response(200)
            self.end_headers()

            length = int(self.headers.get('Content-Length', 0))
            body = str(self.rfile.read(length))

            self.logger.debug("Body of alert: %s", body)

            if body.startswith('b\'', 0, 2) and body.endswith('\''):
                body = body[2:len(body) - 1]

                alert = Alert(body)
                if alert.accounts:
                    if alert.accounts[0] == "*":
                        for account in self.accounts:
                            account.processAlert(alert)
                    else:
                        for account in alert.accounts:
                            if account in self.accounts.keys():
                                self.accounts.get(account).processAlert(alert)
                            else:
                                self.logger.error(
                                    ("Unable to find account with "
                                     "name: %s"), account)
                else:
                    self.logger.error(("Unable to parse alert "
                                       "with body: %s"), body)
            else:
                self.logger.error("Invalid alert body received: %s", body)
Example #20
0
def new_alert():
    if request.method == 'POST':
        # process the data
        item_url = request.form['item_url']
        price_limit = float(request.form['price_limit'])
        item_name = request.form['item_name']
        user_email = session['email']

        store = Store.get_by_url(item_url)
        # print(store)
        item = Item(item_url, store.tag_name, store.query)
        # print(item)
        item.load_price()
        # print(item.load_price())
        item.save_to_mongo()

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

        return redirect(url_for('.index'))

    else:

        return render_template('alerts/new_alert.html')
Example #21
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')
Example #22
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')
def index():
    alerts = Alert.all()
    return render_template('alerts/index.html', alerts=alerts)
def delete_alert(alert_id):
    Alert.get_by_id(alert_id).remove_from_mongo()
    return redirect(url_for('.index'))
Example #25
0
from models.alert import Alert
from dotenv import load_dotenv

load_dotenv()
alerts = Alert.all()
for alert in alerts:
    alert.load_item_price()
    alert.item.save_to_mongo()
    alert.notify_if_price_reached()
if not alerts:
    print("No alerts have been created. Add an item and an alert to begin!")
Example #26
0
from models.alert import Alert

alert = Alert(item_id="26beafafee7e4391a02d54b54fe90d73", price_limit=2000)
alert.save_to_mongo()
Example #27
0
from models.alert import Alert

alert = Alert("e81341d983ef47979282e13d236f73b4", 1000)
alert.save_to_mongo()
Example #28
0
def delete_alert(alert_id):
    alert = Alert.get_by_id(alert_id)
    if alert.user_email == session['email']:
        alert.remove_from_mongo()
    return redirect(url_for('.index'))
Example #29
0
def index():
    alerts = Alert.find_many_by('user_email', session['email'])
    return render_template('alerts/index.html', alerts=alerts)
 def post(self, subdomain, method = None, alert_id = None):
     form = AlertForm(MultiDict(self))
     if not form.validate():
         args = self.template_args
         args['form'] = form
         args['method'] = method
         args['subdomain'] = subdomain
         return self.render('alert.html', **args)
     user = UserManager.get_current_user()
     if method == 'create':
         alert = Alert()
     else:
         alert = AlertManager.get_alert(alert_id)
     alert.email = user.email
     alert.subdomain = user.subdomain
     alert.name = self.get_argument('name', '')
     alert.description = self.get_argument('description', '')
     alert.saved_search = int(self.get_argument('saved_search'))
     alert.threshold_operator = self.get_argument('threshold_operator')
     alert.threshold_count = int(self.get_argument('threshold_count'))
     alert.threshold_time_secs = int(self.get_argument('threshold_time_secs'))
     alert.sound = self.get_argument('sound')
     alert.endpoint = self.get_argument('endpoint', '')
     if method == 'create':
         alert.active = True
         alert.muted = False
         alert.state = 'N'
         alert.last_run = 0
         alert.last_state_change = 0
     alert.put()
     self.redirect('/%s' % subdomain)
Example #31
0
def index():
    alerts = Alert.find_all()
    return render_template("alerts/index.html", alerts=alerts)
Example #32
0
def index():
    print(session['email'])
    alerts = Alert.find_many_by('user_email', session['email'])
    return render_template("alerts/index.html", alerts=alerts)