Exemplo n.º 1
0
def login_details(username, password):
    '''
    Sorts through registered students from the student.json file and confirms
    whether student username and password is registered. If student is
    registered, student log-in expiry date and time is updated. If student is
    not registered, an error message is returned and program exits.

            Parameters:
                    username  (str): Student's username
                    password  (str): Student's password
    '''

    filename = 'student-info/.student.json'
    msg = "INVALID: You are not registered! Register before logging in."
    executed, student_data = file_utils.read_data_from_json_file(filename)
    if not executed:
        utils.error_handling(msg)
    if is_valid_student_info(student_data['student_info'], username, password):
        if add_timestamps_to_json(username):
            msg = "Login successful. Welcome to Code Clinic " + username + "!"
            utils.print_output(msg)
            return
    else:
        msg = "ERROR: Incorrect username or password. Try again!"
        utils.error_handling(msg)
def do_patient_commands(student, clinic):
    '''
    Executes specified patient command, namely creating or cancelling a booked slot.

            Parameters:
                    student  (obj): Object with information on logged-in student
                    clinic   (obj): Object with information on Code clinic     
    '''

    created = False
    if student.info['command'] == 'create':
        try:
            created = booking.make_booking(student.username,
                                           student.info['UD'],
                                           clinic,
                                           student.info,
                                           student.service)
        except(KeyError):
            msg = 'ERROR: Please enter a description in enclosed quotes.\n'\
                                                        +'e.g. "Recursion"'
            utils.print_output(msg)
        if not created:
            listings.print_correlating_table(False, True, student, clinic, False, False)
    elif student.info['command'] == 'cancel':
        try:    
            created = booking.cancel_attendee(student.username,
                                              clinic ,
                                              student.info['UD'])
        except(KeyError):
            msg = "ERROR: Please include the correct uid when cancelling a booking.\n"\
                                            +"Format: <username> cancel patient <uid>"
            utils.print_output(msg)
        if not created:    
            listings.print_correlating_table(False, False, student, clinic, False, False)
def add_registration_info_to_json(username, password):
    '''
    Writes student's information (username and password) to the
    student.json file.

                Parameters:
                    username (str): Student's username
                    password (str): Student's password
    '''

    filename = 'student-info/.student.json'
    msg = ''
    if username == 'codeclinic':
        msg = 'ERROR: Invalid username.'
        utils.error_handling(msg)
    required_info = {'username': username, 'password': password}
    if os.stat(filename).st_size == 0:
        student_data = {'student_info': []}
        student_data['student_info'].append(required_info)
        file_utils.write_data_to_json_file(filename, student_data)
    else:
        executed, student_data = file_utils.read_data_from_json_file(filename)
        if executed:
            if is_student_registered(student_data['student_info'], username):
                msg = "ERROR: You are already registered."
                utils.error_handling(msg)
            student_data['student_info'].append(required_info)
            file_utils.write_data_to_json_file(filename, student_data)
            msg = "Registration successful. Welcome to Code Clinic " + username + "!"
            utils.print_output(msg)
        else:
            msg = "ERROR: Could not register student."
            utils.error_handling(msg)
Exemplo n.º 4
0
def create_volunteer_slot(username, date, time, volunteer_service,
                          clinic_service):
    '''
    Creates a 90 minute volunteer slot if the student has an open slot in
    their personal calendar during the specified slot time and the student
    does not have a volunteer slot created at the specified slot time in the
    clinic calendar.

            Parameters:
                    username          (str): Student's username
                    date              (str): Slot date in format <yyyy-mm-dd>
                    time              (str): Slot start time in format <hh:mm>
                    volunteer_service (str): Student's (volunteer) Google
                                             calendar API service
                    clinic_service    (str): Code clinic's Google calendar
                                             API service

            Returns:
                    True          (boolean): Volunteer slot succesfully
                                             created
                    False         (boolean): Volunteer slot not created
                    
    '''

    open_slots = get_open_volunteer_slots_of_the_day(date, username,
                                                     clinic_service)
    if not open_slots:
        utils.print_output('ERROR: There are no open slots on this day.')
        return False
    time_slot_lst = list(filter(lambda x: x[0] == time, open_slots))
    if not time_slot_lst:
        utils.print_output('ERROR: Choose a valid/open slot start time.')
        return False
    time_slot = time_slot_lst[0]
    start_datetime, end_datetime = utils.convert_date_and_time_to_rfc_format(
        date, time_slot[0], time_slot[1])
    open = utils.is_slot_available(volunteer_service, username, start_datetime,
                                   end_datetime)
    if open:
        thirty_minute_slots = convert_90_min_slot_into_30_min_slots(time_slot)
        for slot in thirty_minute_slots:
            start_datetime, end_datetime = utils.convert_date_and_time_to_rfc_format(
                date, slot[0], slot[1])
            event_info = {
                'summary': 'VOLUNTEER: ' + str(username),
                'start_datetime': start_datetime,
                'end_datetime': end_datetime,
                'attendees': []
            }
            response = utils.add_event_to_calendar(event_info, clinic_service,
                                                   True, username)
            utils.volunteer_accept_invite(clinic_service, response['id'],
                                          response)
        utils.print_output('Volunteer slots created!')
        return True
    msg = 'ERROR: You do not have an open slot in your calendar '\
                                            +'at the selected time.'
    utils.print_output(msg)
    return False
Exemplo n.º 5
0
def delete_volunteer_slot(username, date, time, volunteer_service,
                          clinic_service):
    '''
    The volunteer slots, that occur at specified date/time, are removed from the student's 
    calendar and the clinic's calendar. Deletes volunteer slots if:
    - the student's username is in the event summary (is the student's volunteered slot)
    - the student's 30 minute volunteered slots are not booked by another student

            Parameters:
                    username           (str): Student's username
                    date               (str): Slot date in format <yyyy-mm-dd>
                    time               (str): Slot start time in format <hh:mm>
                    volunteer_service  (str): Student's (volunteer) Google
                                              calendar API service
                    clinic_service     (str): Code clinic's Google calendar
                                              API service

            Returns:
                    True        (boolean): Volunteer slot succesfully deleted
                    False       (boolean): Volunteer slot could not be
                                           deleted/found          
    '''

    msg = ''
    volunteer_slot = get_volunteered_slot(clinic_service, username, date, time)
    if not volunteer_slot:
        msg = 'ERROR: No volunteer slot available to delete at specified date/time.'
        utils.print_output(msg)
        return False
    thirty_minute_slots = convert_90_min_slot_into_30_min_slots(
        (volunteer_slot[0], volunteer_slot[1]))
    for slot in thirty_minute_slots:
        start_datetime, end_datetime = utils.convert_date_and_time_to_rfc_format(
            date, slot[0], slot[1])
        delete_slots_on_calendars([volunteer_service, clinic_service],
                                  start_datetime, end_datetime, username)
    msg = 'Volunteered slots were succesfully deleted.'
    utils.print_output(msg)
    return True
Exemplo n.º 6
0
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)
Exemplo n.º 7
0
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