def order_coffee(slackclient, user, channel, match): print(match.groupdict()) runid = match.groupdict().get('runid', None) run = None if runid and runid.isdigit(): run = Run.query.filter_by(id=int(runid)).first() if not run: # Pick a run runs = Run.query.filter_by(is_open=True).order_by('time').all() if len(runs) > 1: slackclient.rtm_send_message(channel.id, 'More than one open run, please specify by adding run=<id> on the end.') list_runs(slackclient, user, channel, None) return if len(runs) == 0: slackclient.rtm_send_message(channel.id, 'No open runs') return run = runs[0] # Create the coffee coffee = Coffee(match.group(1)) # Find the user that requested this dbuser = utils.get_or_create_user(user.id, TEAM_ID, user.name) print(dbuser) # Put it all together coffee.person = dbuser.id coffee.runid = run.id db.session.add(coffee) db.session.commit() print(coffee) runuser = User.query.filter_by(id=run.person).first() slackclient.rtm_send_message(channel.id, 'That\'s a {} for {} (added to <@{}>\'s run.)'.format(coffee.pretty_print(), mention(user), runuser.slack_user_id))
def test_add_to_run(self): coffee = Coffee("Latte") run = Run(datetime.utcnow()) coffee.run = run db.session.add(coffee) db.session.add(run) db.session.commit() assert coffee in run.coffees
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_add_coffee(self): coffee = Coffee("Latte") db.session.add(coffee) db.session.commit() assert coffee in db.session db.session.delete(coffee) db.session.commit() assert coffee not in db.session
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 test_run_has_coffee(self): run = Run(datetime.utcnow()) db.session.add(run) db.session.commit() coffee1 = Coffee("Latte") coffee2 = Coffee("Cappuccino") coffee1.run = run coffee2.run = run db.session.add(coffee1) db.session.add(coffee2) db.session.commit() assert len(run.coffees) == 2 assert run.coffees[0] == coffee1 assert run.coffees[1] == coffee2
def test_user_owes_money_total(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()) user = User() db.session.add(run) db.session.commit() coffee1 = Coffee("Latte") coffee1.price = price1 coffee1.run = run coffee1.addict = user coffee2 = Coffee("Cappuccino") coffee2.price = price2 coffee2.run = run coffee3 = Coffee("Mocha") coffee3.price = price1 coffee3.run = run coffee3.paid = True coffee3.addict = user db.session.add(coffee1) db.session.add(coffee2) db.session.add(coffee3) db.session.add(user) db.session.commit() amount = user.moneyOwedTotal() assert amount == 1.4
def test_run_calculate_cost_total(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()) 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 coffee3 = Coffee("Mocha") coffee3.price = price1 coffee3.run = run coffee3.paid = True db.session.add(coffee1) db.session.add(coffee2) db.session.add(coffee3) db.session.commit() # Calculate total price of run total = run.calculateTotalRunCost() assert total == 3.9
def order_coffee(slackclient, user, channel, match): """Handle adding coffee to existing orders. Args: slackclient: the slackclient.SlackClient object for the current connection to Slack. user: the slackclient.User object for the user who send the message to us. channel: the slackclient.Channel object for the channel the message was received on. match: the object returned by re.match (an _sre.SRE_Match object). """ logger = logging.getLogger('order_coffee') logger.info('Matches: %s', pprint.pformat(match.groupdict())) runid = match.groupdict().get('runid', None) run = None if runid and runid.isdigit(): run = Run.query.filter_by(id=int(runid)).first() if not run: # Pick a run runs = Run.query.filter_by(is_open=True).order_by('time').all() if len(runs) > 1: def resolve_run_feedback(): channel.send_message( 'More than one open run, please specify by adding run=<id> on the end.' ) list_runs(slackclient, user, channel, match=None) return (False, resolve_run_feedback) if len(runs) == 0: def resolve_closed_feedback(): channel.send_message('No open runs') return (False, resolve_closed_feedback) run = runs[0] # Create the coffee c = coffeespecs.Coffee(match.groupdict().get('order', None)) validation_errors = list(c.validation_errors()) if validation_errors: def resolve_validation_feedback(): channel.send_message( 'That coffee is not valid missing the following specs: {}. Got: {}' .format( ', '.join(spec.name for spec in validation_errors), c, )) return (False, resolve_validation_feedback) coffee = Coffee(c, 0, run.id) # Find the user that requested this dbuser = utils.get_or_create_user(user.id, TEAM_ID, user.name) logger.info('User: %s', dbuser) # Put it all together coffee.person = dbuser.id db.session.add(coffee) db.session.commit() events.coffee_added(run.id, coffee.id) # Write the event event = Event(coffee.person, "created", "coffee", coffee.id) event.time = sydney_timezone_now() db.session.add(event) db.session.commit() logger.info('Parsed coffee: %s', coffee) runuser = User.query.filter_by(id=run.person).first() if runuser.slack_user_id: mention_runner = '<@{}>'.format(runuser.slack_user_id) else: mention_runner = runuser.name channel.send_message('That\'s a {} for {} (added to {}\'s run.)'.format( coffee.pretty_print(), mention(user), mention_runner)) return (True, None)