def is_volunteering_at_specified_time(clinic_service, username, start_datetime, end_datetime): ''' Sorts through events, occuring within specified datetimes, on the Code clinic's calendar to confirm whether student with specified username is volunteering during specified date/time. Parameters: clinic_service (obj): Code clinic's Google calendar API service username (str): Student's username start_datetime (str): Datetime (in rfc format) that volunteer event starts end_datetime (str): Datetime (in rfc format) that volunteer event ends Returns: True (boolean): Student is volunteering at specified datetime False (boolean): Student is not volunteering at specified datetime ''' events = utils.get_events(clinic_service, start_datetime, end_datetime) events = list( filter(lambda x: x['start'].get('dateTime') == start_datetime, events)) events = list( filter(lambda y: y['end'].get('dateTime') == end_datetime, events)) if events: if "VOLUNTEER: " + str(username) in events[0]['summary']: return True return False
def get_event_id(start_datetime, end_datetime, username, clinic_service): ''' Sorts through events, occuring between specified datetimes, to find the event UID of the student's volunteered slot. Parameters: start_datetime (str): Datetime (in rfc format) of when slot time starts end_datetime (str): Datetime (in rfc format) of when slot time ends username (str): Student's username clinic_service (str): Code clinic's Google calendar API service Returns: volunteered_event[0]['id'] (str): Event UID of volunteered event at specified datetime '' (empty str): If student did not volunteer at specified datetime ''' events = utils.get_events(clinic_service, start_datetime, end_datetime) volunteered_event = list( filter(lambda x: x['summary'][11:] == username, events)) if volunteered_event: return volunteered_event[0]['id'] return ''
def make_booking(username, uid, clinic, student_info, student_service): ''' Function will handle the logic for booking an empty slot. Sorts through events occuring in the next 7 days to find specified volunteer slot to book. If event is found - the student is added as an attendee to volunteered slot. Parameters: username (str): Patient's (student) username uid (str): Unique event id of event clinic.service (obj): Code clinic's Google calendar API service student_info (dict): Information on student and given command Returns: True (boolean): Student added as an attendee to specified event (booked slot) False (boolean): Student not added as an attendee to specified event (slot not booked) ''' now = datetime.datetime.utcnow().isoformat() + 'Z' end_date = ((datetime.datetime.utcnow())\ + datetime.timedelta(days=7)).isoformat() + 'Z' slots = utils.get_events(clinic.service, now, end_date) if slots == []: utils.print_output('ERROR: This slot is unavailable') return False available, volunteered_event = get_chosen_slot(slots, username, uid, student_service) if not available: utils.print_output('ERROR: Choose a valid event id.') return False updated_event, unique_id = create_booking_body(volunteered_event, username, student_info['description']) try: updated_response = clinic.service.events()\ .update(calendarId='primary', eventId=unique_id, body=updated_event).execute() booker_accept_invite(clinic.service, unique_id, updated_response) email.send_message( 'me', email.patient_create_text(username, updated_response), clinic.email_service) utils.print_output("Booking succesfully made! You're unique id is: "\ + str(updated_response['id'])) return True except: error_msg = 'ERROR: An error has stopped the booking from being made.\n'\ +'Please try again.' utils.error_handling(error_msg)
def cancel_attendee(username, clinic, uid): ''' Cancels booking slot by using the unique event ID and removing student as an attendee to the event. If event cannot be cancelled, then the program outputs an error message with the reason for failure. Parameters: username (str): Patient's (student) username clinic_service (obj): Code clinic's Google calendar API service uid (str): Unique event id of event Returns: True (boolean): Student removed as an attendee from event (cancelled booked slot) False (boolean): Student not removed as an attendee from event (events left unchanged) ''' msg = '' now = datetime.datetime.utcnow().isoformat() + 'Z' end_date = ((datetime.datetime.utcnow())\ + datetime.timedelta(days=7)).isoformat()+'Z' deletion = False slots = utils.get_events(clinic.service, now, end_date) deletion, event = utils.get_chosen_slot(slots, username, uid) if not is_user_valid(event, username): msg = "ERROR: You cannot cancel another users booking" utils.print_output(msg) return False if deletion == True: try: updated_event = update_booking_body(event, username) event = clinic.service.events().update(calendarId='primary',\ eventId=event['id'], body=updated_event).execute() email.send_message('me', email.patient_cancel_text(username,event),\ clinic.email_service) except: msg = "ERROR: Could not cancel booking." utils.error_handling(msg) msg = "Booking successfully deleted." utils.print_output(msg) return True else: msg = 'ERROR: You cannot cancel selected booking.\n'\ +'Use the help command (-h) for more infromation.' utils.print_output(msg) return False
def get_volunteered_slot(clinic_service, username, date, time): ''' Sorts through possible volunteer slot times to confirm that the specified time is a valid volunteer slot time. If the time is a valid volunteer time the Code clinic's calendar is used to check whether the student is volunteering at the specified date/time and that the volunteer slot has only one attendee (slot is not booked). The volunteer slot time is returned if the event fulfills this criteria. Parameters: clinic_service (str): Code clinic's Google calendar API service username (str): Student's username date (str): Slot date in format <yyyy-mm-dd> time (str): Slot start time in format <hh:mm> Returns: chosen_slot (tuple): Start and end time of 90 min slot ''' ninety_min_slots, chosen_slot = [], '' events_exist = False slots = [('08:30', '10:00'), ('10:00', '11:30'), ('11:30', '13:00'), ('13:00', '14:30'), ('14:30', '16:00'), ('16:00', '17:30')] for slot in slots: if time == slot[0]: ninety_min_slots = convert_90_min_slot_into_30_min_slots(slot) chosen_slot = slot if ninety_min_slots: for slot in ninety_min_slots: start, end = slot[0], slot[1] start_datetime, end_datetime = utils\ .convert_date_and_time_to_rfc_format(date, start, end) events = utils.get_events(clinic_service, start_datetime, end_datetime) if events: events_exist = True for event in events: if event['summary'][11:] == username: if not len(event['attendees']) == 1: return if events_exist: return chosen_slot return
def get_events_for_n_days(service): ''' Returns events occuring withing the given amount of days on the user's calendar by using the Google calendar API service. If the number of days are not specified, the default amount of days to return is 7 days. Parameters: service (obj): Google calendar API service Returns: n_days (int): Number of days events (list): Events occuring in specified amount of days ''' n_days = 7 now = datetime.datetime.utcnow().isoformat() + 'Z' if sys.argv[-1].isdigit(): n_days = int(sys.argv[-1]) end_date = ((datetime.datetime.utcnow()) + datetime.timedelta(days=n_days))\ .isoformat() + 'Z' events = utils.get_events(service, now, end_date) return n_days, events