def get_calendar(): """ Retrieves Code Clinic calendar and stores the events in a JSON file Parameter: nothing Returns: Dictionary of events on the calendar """ # assign variables stored in the user's config file days_to_get = int(config.retrieve_variable('days_to_get')) calendar = config.retrieve_variable('calendar') service = config.user_login() # set start and end time of events to get start_filter = datetime.datetime.utcnow().isoformat() + 'Z' end_filter = (datetime.datetime.utcnow() + timedelta(days=days_to_get)) end_filter = end_filter.isoformat() + 'Z' # Call the Calendar API events_result = service.events().list(calendarId=calendar, timeMin=start_filter, timeMax=end_filter, singleEvents=True, orderBy='startTime').execute() # store data in json if there is update information if os.path.exists("google/calendar_pull.json"): if config.check_calender_state("google/calendar_pull.json",\ config.user_login()): pass else: with open('google/calendar_pull.json', 'w') as outfile: json.dump(events_result, outfile, indent=4) return events_result
def remove_attendee(id): """ Sends API request to remove an attendee from an event Parameter: id: event identifier Returns: nothing """ # assign variables stored in the user's config file username = config.retrieve_variable('username') calendar = config.retrieve_variable('calendar') service = config.user_login() # First retrieve the event from the API. event = service.events().get(calendarId=calendar, eventId=id).execute() # update attendee event['attendees'] = [ event['attendees'][0], ] # revert description to volunteer's speciality event['description'] = (event['description'].split(" -"))[0] # API call to send updated information service.events().update(calendarId=calendar, eventId=event['id'], \ body=event, sendUpdates = 'all').execute() # call function to mail the volunteer mailer.cancelled_event(username, event)
def add_attendee(id, support_needed): """ Sends API request to add an attendee to an event and notify the volunteer of the booking Parameter: id: event identifier support_needed: what user needs help with Returns: link to the event """ # assign variables stored in the user's config file username = config.retrieve_variable('username') service = config.user_login() calendar = config.retrieve_variable('calendar') # First retrieve the event from the API. event = service.events().get(calendarId=calendar, eventId=id).execute() # update attendee event['attendees'] = [ event['attendees'][0], { 'email': f"{username}@student.wethinkcode.co.za" }, ] # update description event['description'] = f"{event['description']} - {support_needed}" # API call to send updated information service.events().update(calendarId=calendar, eventId=event['id'], \ body=event, sendUpdates = 'all').execute() # call function to mail the volunteer mailer.booked_event(username, event)
def freebusy(date, time): """ Sends API request to retrieve times that the calendar contains events between two time ranges Parameter: date (yyy-mm-dd), time (HH:MM) Returns: list of times that the calendar contains events """ # assign variables stored in the user's config file service = config.user_login() username = config.retrieve_variable('username') # set start time and add 30 minutes to the start time start_filter = (make_datetime_from_string(f'{date}T{time}:00+0200')) start_filter = start_filter.isoformat() end_filter = make_datetime_from_string(f'{date}T{time}:00+0200') end_filter = (end_filter + timedelta(minutes=30)).isoformat() # create body to be used by Freebusy request busy_body = { "timeMin": start_filter, "timeMax": end_filter, "timeZone": 'Africa/Johannesburg', "items": [{ "id": f'{username}@student.wethinkcode.co.za' }] } # API call to retrieve busy information results = service.freebusy().query(body=busy_body).execute() # refine returned date to only have list of busy times busy_dict = results[u'calendars'] busy_list = busy_dict[f'{username}@student.wethinkcode.co.za']['busy'] return busy_list
def create_event(speciality, date, time): """ Sends API request to create an event Parameter: speciality: what volunteer can help with date: when event should be made time: what time event should be made Returns: Nothing """ # assign variables stored in the user's config file location = config.retrieve_variable('campus') username = config.retrieve_variable('username') calendar = config.retrieve_variable('calendar') service = config.user_login() # add 30 minutes to the start time end_time = datetime.datetime.strptime(f"{date}{time}", '%Y-%m-%d%H:%M') end_time = end_time + timedelta(minutes=30) end_time = (str(end_time)).replace(" ", "T") + "+02:00" # details for the event to be created event = { 'summary': f"Code Clinic - {username}", 'location': location, 'description': speciality, 'start': { 'dateTime': f"{date}T{time}:00+02:00", 'timeZone': 'Africa/Johannesburg', }, 'end': { 'dateTime': end_time, 'timeZone': 'Africa/Johannesburg', }, 'attendees': [ { 'email': f"{username}@student.wethinkcode.co.za" }, ] } # call API to create event event = service.events().insert(calendarId=calendar, body=event, \ sendUpdates = 'all').execute()
def filter_my_empty_volunteer_slots(): ''' Filters out code_clinic_events to show specific volunteer slots where no one has booked Returns: list of not_booked ''' username = config.retrieve_variable('username') code_clinic_events = filter_for_codeclinic() not_booked = filter(lambda x: len(x['attendees']) == 1 \ and username in x['creator']['email'], code_clinic_events) return list(not_booked)
def delete_event(id): """ Sends API request to remove an event Parameter: id: event identifier Returns: nothing """ # assign variables stored in the user's config file calendar = config.retrieve_variable('calendar') service = config.user_login() # API call to send updated information service.events().delete(calendarId=calendar, eventId=id, \ sendUpdates = 'all').execute()
def filter_my_patient_slots(): ''' Filters out code_clinic_events events for events where user's patient slots are extracted - this is used in cancel operation. It only removes the patient. Returns: list of events where you are the patient ''' username = config.retrieve_variable('username') code_clinic_events = filter_for_codeclinic() patients_booked_slots = filter(lambda x: len(x['attendees']) == 2 \ and username in x['attendees'][1]['email'] \ and username not in x['creator']['email'], \ code_clinic_events) return list(patients_booked_slots)
def filter_for_booking(): ''' Gives all available slots that may be booked by current user. The filter searches for events where there is only one person attached to the event and that the user did not create the event i.e. you cannot book yourself for help. Returns: list of open_slots ''' username = config.retrieve_variable('username') code_clinic_events = filter_for_codeclinic() open_slots = filter(lambda x: len(x['attendees']) == 1 and username \ not in x['creator']['email'] and filter_available_creation(x['start']['dateTime'][:10], x['start']['dateTime'][11:16]), code_clinic_events) return list(open_slots)
def list_of_users_clinic_events(): ''' Filters out specific users code clinic events they have booked and volunteered for Returns: list of events booked and volunteered for ''' username = config.retrieve_variable('username') code_clinic_events = filter_for_codeclinic() volunteered_events = [] booked_events = [] for event in code_clinic_events: if username in event['creator']['email']: volunteered_events.append(event) else: for attendee in event['attendees']: if username in attendee['email']: booked_events.append(event) return volunteered_events, booked_events
def get_personal_cal(): """ Retrieves Code Clinic calendar and stores the events in a JSON file Parameter: nothing Returns: Dictionary of events on the calendar """ # assign variables stored in the user's config file days_to_get = int(config.retrieve_variable('days_to_get')) service = config.user_login() # set start and end time of events to get start_filter = datetime.datetime.utcnow().isoformat() + 'Z' end_filter = (datetime.datetime.utcnow() + timedelta(days=days_to_get)) end_filter = end_filter.isoformat() + 'Z' # Call the Calendar API events_result = service.events().list(calendarId='primary', timeMin=start_filter, timeMax=end_filter, singleEvents=True, orderBy='startTime').execute() my_events = events_result.get('items', []) return my_events