Beispiel #1
0
def newbag():
  if request.method == 'POST':
    bag = Bag(request.form['store'], request.form['threshold'], 0, request.form['network'])
    db_session.add(bag)
    db_session.commit()
    return redirect(url_for('home'))
  return render_template('newbagform.html')
Beispiel #2
0
def addtobag(userid):
    if request.method == 'POST':
        bag = Bag.query.filter_by(store = request.form['store']).first()
        bag.amountinbag = bag.amountinbag + int(request.form['price'])
        user = User.query.filter_by(id = userid).first()
        # add the user to the bag
        bag.users.append(user)
        # add the user's order to the bag
        order = Order(request.form['itemurl'], request.form['price'], request.form['quantity'], request.form['size'], bag.id, userid)
        bag.orders.append(order)
        db_session.add(order)
        db_session.commit()
        return redirect(url_for('mybags', userid=userid))
    return render_template('addtobagform.html')
Beispiel #3
0
def bagpage(bagid):
  bag = Bag.query.filter_by(id=bagid).first()
  if request.method == 'POST':
    bag.amountinbag = bag.amountinbag + int(request.form['price'])
    # add the user to the bag
    user = User.query.filter_by(id=session.get('userid')).first()
    bag.users.append(user)
    # add the user's order to the bag
    order = Order(request.form['itemurl'], request.form['price'], request.form['quantity'], bag.id, user.id)
    bag.orders.append(order)
    db_session.add(order)
    db_session.commit()
    flash("Your purchase has been added")
    return redirect(url_for('bagpage', bagid=bagid))
  return render_template('bagpage.html', bag=bag)
Beispiel #4
0
def facebook_authorized(resp):
    next_url = request.args.get('next') or url_for('home')
    if resp is None or 'access_token' not in resp:
        return redirect(next_url)

    session['logged_in'] = True
    session['facebook_token'] = (resp['access_token'], '')

    fbuser = facebook.get('me').data
#    return fbuser['email']
    if User.query.filter_by(email = fbuser['email']).first() == None:
      user = User(fbuser['first_name'], fbuser['last_name'], fbuser['email'], '', '')
      db_session.add(user)
      db_session.commit()
    
    session['userid'] = User.query.filter_by(email = fbuser['email']).first().id
    return redirect(url_for('home'))
Beispiel #5
0
def bagpage(bagid):
  if not session.get('logged_in'):
    abort(401)
  user = User.query.filter_by(id=session.get('userid')).first()
  if not user.isauthenticated:
    abort(401)
  redir = False
  for order in user.orders:
    if str(order.bag.id) == bagid:
      redir = True
      orderid = order.id
  if redir:
    return redirect(url_for('editorder', orderid=orderid))
  bag = Bag.query.filter_by(id=bagid).first()

    # update order info for progress bar
  percentfull = """ "width: """ + str(bag.amountinbag*100 / max(bag.threshold,bag.amountinbag)) + """%;" """ 
  percentempty = """ "width: """ + str(100-100*bag.amountinbag / max(bag.threshold,bag.amountinbag)) + """%;" """
  
  if request.method == 'POST':
    # check the validity of input, if something is wrong, return the page with error messages where appropriate
    errorfound = False
    try:
      price = float(request.form['price'])
      if price < 0:
        flash("Invalid price", "priceerror")
        errorfound = True
    except ValueError:
      flash("Invalid price", "priceerror")
      errorfound = True

    # check if any of the input is too long
    if len(request.form['itemurl']) > 200:
      flash("That URL is too long.  Please contact us!", "urllongerr")
      errorfound = True
    if len(request.form['details']) > 400:
      flash("Looks like you've got too many details.  If you can't shorten it, please contact us!", "detailslongerr")
      errorfound = True

    # check if any fields were left empty
    if not request.form['itemurl']:
      flash("Please input the item's URL", "missingurlerror")
      errorfound = True
    if not request.form['price']:
      flash("Please input the price of the item", "missingpriceerror")
      errorfound = True
    if not request.form['details']:
      flash("Please enter details about your order", "missingdetailserror")
      errorfound = True
    if errorfound:
      return redirect(url_for('bagpage', bagid=bagid))


    bag.amountinbag = bag.amountinbag + price
    # add the user to the bag
    user = User.query.filter_by(id=session.get('userid')).first()
    bag.users.append(user)

    ship = False
    if 'ship' in request.form:
      ship = True

    # add the user's order to the bag
    order = Order(request.form['itemurl'], request.form['price'], request.form['details'], ship, None, None, None, None, None, bag.id, user.id)
    bag.orders.append(order)
    db_session.add(order)
    db_session.commit()
    flash("Your purchase of " + order.url + " has been added to the " + bag.store + " bag!", "addmessage")
    # update order info for progress bar
    percentfull = """ "width: """ + str(int(bag.amountinbag*100 / max(bag.threshold,bag.amountinbag))) + """%;" """ 
    percentempty = """ "width: """ + str(100-int(100*bag.amountinbag / max(bag.threshold,bag.amountinbag))) + """%;" """

    return redirect(url_for('bagpage', bagid=bagid))
  return render_template('bagpage.html', percentempty=percentempty, percentfull=percentfull, bag=bag, userid=session.get('userid'))