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