def create_run(self, slackclient, user, channel, match): """Create an open run 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('create_run') logger.info('Matches: %s', pprint.pformat(match.groupdict())) cafeid = match.group('cafeid') if cafeid and cafeid.isdigit(): cafe = Cafe.query.filter_by(id=int(cafeid)).first() if not cafe: channel.send_message('Cafe does not exist. These are the available cafes:') self.list_cafes(slackclient, user, channel, match=None) return pickup = match.groupdict().get('pickup', None) timestr = match.groupdict().get('time', None) # Get the person creating the run person = utils.get_or_create_user(user.id, self.TEAM_ID, user.name) logger.info('User: %s', dbuser) # Assume valid? Create the run run = Run(timestr) run.person = person run.fetcher = person run.cafeid = cafeid run.pickup = pickup run.modified = sydney_timezone_now() run.is_open = True db.session.add(run) db.session.commit() # Create the event self.write_to_events("created", "run", run.id, run.person) # Notify Slack try: events.run_created(run.id) except Exception as e: logging.exception('Error while trying to send notifications.')
def add_run(cafeid=None): form = RunForm(request.form) users = User.query.all() form.person.choices = [(user.id, user.name) for user in users] cafes = Cafe.query.all() if not cafes: flash( "There are no cafes currently configured. Please add one before creating a run", "warning") return redirect(url_for("home")) form.cafeid.choices = [(cafe.id, cafe.name) for cafe in cafes] if request.method == "GET": if cafeid: form.cafeid.data = cafeid form.person.data = current_user.id # Generate a time for the run. The algorithm here is we want the # default run time to be at least 15 minutes in the future, and also a # multiple of 15 minutes (since it look nicer). # This means that by default, the run will be between 15 and 30 minutes # from the current time. t = sydney_timezone_now().replace(second=0, microsecond=0) t += datetime.timedelta(minutes=15) t += datetime.timedelta( minutes=15 - (t.minute % 15)) # truncate up to the nearest 15 minutes form.time.data = t form.is_open.data = True return render_template("runform.html", form=form, formtype="Add", current_user=current_user) if form.validate_on_submit(): # Add run run = Run(form.data["time"]) person = User.query.filter_by(id=form.data["person"]).first() run.person = person.id run.fetcher = person run.cafeid = form.data["cafeid"] run.pickup = form.data["pickup"] run.modified = sydney_timezone_now() run.is_open = form.data["is_open"] db.session.add(run) db.session.commit() try: events.run_created(run.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") write_to_events("created", "run", run.id) flash("Run added", "success") return redirect(url_for("view_run", runid=run.id)) else: for field, errors in form.errors.items(): flash("Error in %s: %s" % (field, "; ".join(errors)), "danger") return render_template("runform.html", form=form, formtype="Add", current_user=current_user)