def test_user_has_coffees(self): user = User() db.session.add(user) db.session.commit() coffee1 = Coffee("Latte") coffee2 = Coffee("Cappuccino") coffee1.addict = user coffee2.addict = user db.session.add(coffee1) db.session.add(coffee2) db.session.commit() assert len(user.coffees) == 2 assert user.coffees[0] == coffee1 assert user.coffees[1] == coffee2
def add_coffee(runid=None): logger = logging.getLogger('views.add_coffee') runs = Run.query.filter(Run.time >= sydney_timezone_now()).filter_by( is_open=True).all() form = CoffeeForm(request.form) form.runid.choices = [(-1, '')] + [(r.id, r.prettyprint()) 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(): logger.info('Form: %s', form.data) coffee = Coffee(form.data["coffee"], form.data['price'], form.data['runid']) person = User.query.filter_by(id=form.data["person"]).first() coffee.personid = person.id coffee.addict = person if form.data["runid"] == -1: coffee.starttime = form.data["starttime"] coffee.endtime = form.data["endtime"] else: coffee.runid = form.data["runid"] run = Run.query.filter_by(id=form.data["runid"]).first() coffee.modified = sydney_timezone_now() db.session.add(coffee) db.session.commit() write_to_events("created", "coffee", coffee.id) if form.data["runid"] != -1: try: events.coffee_added(coffee.runid, coffee.id) except Exception as e: logging.exception('Error while trying to send notifications.') flash( 'Error occurred while trying to send notifications. Please tell Maddy, Elmo, or Katie.\n{}' .format(cgi.escape(str(e), quote=True)), "failure") 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)
def test_user_owes_money_to_person(self): # Set up cafe with menu cafe = Cafe() db.session.add(cafe) db.session.commit() price1 = Price(cafe.id, "S") price1.amount = 1.4 price1.cafe = cafe price2 = Price(cafe.id, "M") price2.amount = 2.5 price2.cafe = cafe db.session.add(price1) db.session.add(price2) db.session.commit() # Create run and add some coffees run = Run(datetime.utcnow()) user1 = User() user2 = User() run.fetcher = user2 db.session.add(user1) db.session.add(user2) db.session.add(run) db.session.commit() coffee1 = Coffee("Latte") coffee1.price = price1 coffee1.run = run coffee2 = Coffee("Cappuccino") coffee2.price = price2 coffee2.run = run coffee2.addict = user1 coffee3 = Coffee("Mocha") coffee3.price = price1 coffee3.run = run coffee3.paid = True coffee3.addict = user1 db.session.add(coffee1) db.session.add(coffee2) db.session.add(coffee3) db.session.commit() amount = user1.moneyOwedPerson(user2) assert amount == 2.5