def mobile_synccoffee(): # Parse JSON # Try to add # Return success or failure, with ID or error if request.headers["Content-Type"] == "application/json": coffeejson = request.get_json() else: return redirect(url_for("home")) try: print "Coffee JSON Request", coffeejson if "id" in coffeejson: coffee = Coffee.query.filter_by(id=coffeejson["id"]).first() else: coffee = Coffee(coffeejson["coffeetype"]) person = get_person(coffeejson["person"]) coffee.person = person.id coffee.addict = person coffee.size = coffeejson["size"] coffee.sugar = coffeejson["sugar"] coffee.run = coffeejson["run"] coffee.runobj = Run.query.filter_by(id=coffeejson["run"]).first() if "id" not in coffeejson: db.session.add(coffee) db.session.commit() return jsonify(msg="success", id=coffee.id, modified=coffee.jsondatetime("modified")) except: return jsonify(msg="error")
def add_coffee(runid=None): runs = Run.query.filter(Run.time >= sydney_timezone_now()).filter_by( is_open=True).all() if not runs: flash( "There are no upcoming coffee runs. Would you like to make one instead?", "warning") return redirect(url_for("home")) lastcoffee = Coffee.query.filter_by(addict=current_user).order_by( Coffee.id.desc()).first() form = CoffeeForm(request.form) form.runid.choices = [(r.id, r.time) for r in runs] if runid: run = Run.query.filter_by(id=runid).first() localmodified = run.time.replace( tzinfo=pytz.timezone("Australia/Sydney")) if sydney_timezone_now() > localmodified: flash("You can't add coffees to this run", "danger") return redirect(url_for("view_run", runid=runid)) form.runid.data = runid users = User.query.all() form.person.choices = [(user.id, user.name) for user in users] if request.method == "GET": form.person.data = current_user.id return render_template("coffeeform.html", form=form, formtype="Add", current_user=current_user) if form.validate_on_submit(): print form.data coffee = Coffee(form.data["coffee"]) person = User.query.filter_by(id=form.data["person"]).first() coffee.personid = person.id coffee.addict = person coffee.runid = form.data["runid"] run = Run.query.filter_by(id=form.data["runid"]).first() coffee.price = form.data["price"] print coffee.price coffee.modified = sydney_timezone_now() db.session.add(coffee) db.session.commit() write_to_events("created", "coffee", coffee.id) notify_run_owner_of_coffee(run.fetcher, person, coffee) flash("Coffee order added", "success") return redirect(url_for("view_coffee", coffeeid=coffee.id)) else: for field, errors in form.errors.items(): flash("Error in %s: %s" % (field, "; ".join(errors)), "danger") return render_template("coffeeform.html", form=form, current_user=current_user)