Beispiel #1
0
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))
Beispiel #2
0
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)