def schedule_call(day, hour, client): """ Schedules a call between client and their coach on the specified datetime """ try: # first, delete all existing calls for this month # we are in sqlite so we can't use YEAR and MONTH functions... :( for match in db.session.query(Call).filter(Call.client_id == client.id).all(): if match.timeslot.year == day.year and match.timeslot.month == day.month: db.session.delete(match) db.session.commit() except Exception: LOGGER.exception('Failed to delete existing calls') db.session.rollback() # now, add the schedule timeslot = arrow.get(day).replace(hour=CALL_HOURS[hour]).datetime call = Call.create( timeslot=timeslot, client_id=client.id, coach_id=client.coach_id) db.session.add(call) db.session.commit()