def edit_price(priceid): price = Price.query.filter_by(id=priceid).first_or_404() form = PriceForm(obj=price) form.cafeid.choices = [(price.cafe.id, price.cafe.name)] form.cafeid.data = price.cafe.id if request.method == "GET": return render_template("priceform.html", cafe=price.cafe, form=form, formtype="Edit", current_user=current_user) if request.method == "POST" and form.validate_on_submit(): form.populate_obj(price) coffee = coffeespecs.Coffee(form.data["price_key"]) price.price_key = coffee.get_price_key() db.session.commit() write_to_events("updated", "price", price.id) flash("Price updated for cafe '%s'" % price.cafe.name, "success") return redirect(url_for("view_cafe", cafeid=price.cafe.id)) else: for field, errors in form.errors.items(): flash("Error in %s: %s" % (field, "; ".join(errors)), "danger") return render_template("priceform.html", cafe=price.cafe, form=form, formtype="Add", current_user=current_user)
def __init__(self, coffee_request, entered_price, runid): if isinstance(coffee_request, coffeespecs.Coffee): c = coffee_request else: c = coffeespecs.Coffee(coffee_request) self.coffee = c.toJSON() if runid != -1: self.runid = runid if entered_price != 0: self.price = entered_price else: self.price = self.lookup_price()
def add_cafe_price(cafeid=None): form = PriceForm() if cafeid: cafe = Cafe.query.filter_by(id=cafeid).first_or_404() form.cafeid.choices = [(cafe.id, cafe.name)] form.cafeid.data = cafe.id else: cafes = Cafe.query.all() if not cafes: flash( "There are no existing cafes. Would you like to make one instead?", "warning") return redirect(url_for("home")) form.cafeid.choices = [(c.id, c.name) for c in cafes] cafe = cafes[0] if request.method == "GET": return render_template("priceform.html", cafe=cafe, form=form, formtype="Add", current_user=current_user) if request.method == "POST" and form.validate_on_submit(): cafeid = form.data["cafeid"] cafe = Cafe.query.filter_by(id=cafeid).first_or_404() coffee = coffeespecs.Coffee(form.data["price_key"]) price = Price(cafe.id, coffee) if cafeid: price.cafeid = cafeid else: price.cafeid = form.data["cafeid"] price.amount = form.data["amount"] db.session.add(price) db.session.commit() flash("Price added to cafe '%s'" % cafe.name, "success") return redirect(url_for("view_cafe", cafeid=cafeid)) else: for field, errors in form.errors.items(): flash("Error in %s: %s" % (field, "; ".join(errors)), "danger") return render_template("priceform.html", cafe=cafe, form=form, formtype="Add", current_user=current_user)
def edit_coffee(coffeeid): coffee = Coffee.query.filter(Coffee.id == coffeeid).first_or_404() form = CoffeeForm(request.form, obj=coffee) runs = Run.query.filter_by(is_open=True).all() form.runid.choices = [(r.id, r.prettyprint()) for r in runs] # Make sure it is possible to edit coffees in a run that has already been # closed. We do this by adding the existing coffee run to the dropdown. if coffee.run and coffee.run.id not in [r.id for r in runs]: form.runid.choices.append((coffee.run.id, coffee.run.prettyprint())) c = coffeespecs.Coffee.fromJSON(coffee.coffee) users = User.query.all() form.person.choices = [(user.id, user.name) for user in users] if request.method == "GET": form.coffee.data = str(c) form.runid.data = coffee.runid return render_template("coffeeform.html", form=form, formtype="Edit", price=coffee.price, current_user=current_user) if request.method == "POST" and form.validate_on_submit(): coffee.coffee = coffeespecs.Coffee(form.data["coffee"]).toJSON() coffee.price = form.data["price"] coffee.runid = form.data['runid'] coffee.person = form.data['person'] coffee.modified = sydney_timezone_now() db.session.commit() write_to_events("updated", "coffee", coffee.id) flash("Coffee edited", "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, formtype="Edit", current_user=current_user)
def __init__(self, coffee_request): c = coffeespecs.Coffee(coffee_request) self.coffee = c.toJSON() self.price = self.get_price()
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)