def get_appointment(appointment_id): appointment = Appointment.find_by_id(appointment_id) if not appointment: raise ResourceNotFoundError('Unknown path') api_json = appointment.to_api_json(current_user.get_id()) _put_student_profile_per_appointment([api_json]) return tolerant_jsonify(api_json)
def appointment_check_in(appointment_id): appointment = Appointment.find_by_id(appointment_id) if not appointment: raise ResourceNotFoundError('Unknown path') if appointment.dept_code in _dept_codes_with_scheduler_privilege(): params = request.get_json() advisor_uid = params.get('advisorUid', None) if not advisor_uid: raise BadRequestError( 'Appointment check-in requires \'advisor_uid\'') appointment = Appointment.check_in( appointment_id=appointment_id, checked_in_by=current_user.get_id(), advisor_dept_codes=params.get('advisorDeptCodes', None), advisor_name=params.get('advisorName', None), advisor_role=params.get('advisorRole', None), advisor_uid=advisor_uid, ) api_json = appointment.to_api_json(current_user.get_id()) _put_student_profile_per_appointment([api_json]) return tolerant_jsonify(api_json) else: raise ForbiddenRequestError( f'You are unauthorized to manage {appointment.dept_code} appointments.' )
def update_appointment(appointment_id): appointment = Appointment.find_by_id(appointment_id) if not appointment: raise ResourceNotFoundError('Unknown path') has_privilege = current_user.is_admin or appointment.dept_code in _dept_codes_with_scheduler_privilege() if not has_privilege: raise ForbiddenRequestError(f'You are unauthorized to manage {appointment.dept_code} appointments.') params = request.get_json() details = params.get('details', None) scheduled_time = params.get('scheduledTime', None) if scheduled_time: scheduled_time = localized_timestamp_to_utc(scheduled_time) student_contact_info = params.get('studentContactInfo', None) student_contact_type = params.get('studentContactType', None) topics = params.get('topics', None) appointment.update( details=process_input_from_rich_text_editor(details), scheduled_time=scheduled_time, student_contact_info=student_contact_info, student_contact_type=student_contact_type, topics=topics, updated_by=current_user.get_id(), ) api_json = appointment.to_api_json(current_user.get_id()) _put_student_profile_per_appointment([api_json]) return tolerant_jsonify(api_json)
def test_search_with_no_input_and_date(self, coe_advisor, client): """Notes and appointments search needs no input when date options are set.""" from boac import db, std_commit appointment = Appointment.find_by_id(2) appointment.created_at = util.localized_timestamp_to_utc( '2017-11-01T00:00:00') std_commit() db.session.refresh(appointment) response = client.post( '/api/search', data=json.dumps({ 'appointments': True, 'notes': True, 'searchPhrase': '', 'appointmentOptions': { 'dateFrom': '2017-11-01', 'dateTo': '2017-11-02' }, 'noteOptions': { 'dateFrom': '2017-11-01', 'dateTo': '2017-11-02' }, }), content_type='application/json', ) self._assert(response, note_count=4, appointment_count=1)
def test_search_by_appointment_cancel_reason(self, coe_advisor, client): """Appointments can be searched for by cancel reason and cancel reason explained.""" appointment = Appointment.find_by_id(1) Appointment.cancel( appointment_id=appointment.id, canceled_by=AuthorizedUser.get_id_per_uid('6972201'), cancel_reason='Sick cat', cancel_reason_explained= 'Student needed to attend to ailing feline.', ) response = client.post( '/api/search', data=json.dumps({ 'appointments': True, 'notes': True, 'searchPhrase': 'cat' }), content_type='application/json', ) self._assert(response, appointment_count=1) response = client.post( '/api/search', data=json.dumps({ 'appointments': True, 'notes': True, 'searchPhrase': 'feline' }), content_type='application/json', ) self._assert(response, appointment_count=1)
def unreserve_appointment(appointment_id): appointment = Appointment.find_by_id(appointment_id) if not appointment: raise ResourceNotFoundError('Unknown path') has_privilege = current_user.is_admin or appointment.dept_code in _dept_codes_with_scheduler_privilege() if not has_privilege: raise ForbiddenRequestError(f'You are unauthorized to manage appointment {appointment_id}.') if appointment.status != 'reserved': raise BadRequestError(appointment.to_api_json(current_user.get_id())) _set_appointment_to_waiting(appointment) return Response(status=200)
def test_search_by_appointment_cancel_reason(self, coe_advisor, client): """Appointments can be searched for by cancel reason and cancel reason explained.""" appointment = Appointment.find_by_id(1) Appointment.cancel( appointment_id=appointment.id, cancelled_by=AuthorizedUser.get_id_per_uid('6972201'), cancel_reason='Sick cat', cancel_reason_explained= 'Student needed to attend to ailing feline.', ) api_json = _api_search(client, 'cat', appointments=True) self._assert(api_json, appointment_count=1) api_json = _api_search(client, 'feline', appointments=True) self._assert(api_json, appointment_count=1)
def test_search_respects_date_filters(self, app, coe_advisor, client): """Search results include appointments created within provided date range.""" from boac import std_commit appointment = Appointment.find_by_id(2) appointment.created_at = util.localized_timestamp_to_utc( '2017-10-31T00:00:00') std_commit(allow_test_environment=True) api_json = _api_search( client, 'pick me', appointments=True, appointment_options={ 'dateFrom': '2017-10-31', 'dateTo': '2017-11-01', }, ) self._assert(api_json, appointment_count=1)
def reserve_appointment(appointment_id): appointment = Appointment.find_by_id(appointment_id) if not appointment: raise ResourceNotFoundError('Unknown path') has_privilege = current_user.is_admin or appointment.dept_code in _dept_codes_with_scheduler_privilege( ) if has_privilege and appointment.status in ('reserved', 'waiting'): appointment = Appointment.reserve( appointment_id=appointment_id, reserved_by=current_user.get_id(), ) api_json = appointment.to_api_json(current_user.get_id()) _put_student_profile_per_appointment([api_json]) return tolerant_jsonify(api_json) else: raise ForbiddenRequestError( f'You are unauthorized to manage appointment {appointment_id}.')
def appointment_check_in(appointment_id): appointment = Appointment.find_by_id(appointment_id) if not appointment: raise ResourceNotFoundError('Unknown path') if appointment.dept_code not in _dept_codes_with_scheduler_privilege(): raise ForbiddenRequestError(f'You are unauthorized to manage {appointment.dept_code} appointments.') if not appointment.status_change_available(): raise BadRequestError(appointment.to_api_json(current_user.get_id())) params = request.get_json() advisor_attrs = _advisor_attrs_for_uid(params.get('advisorUid')) if not advisor_attrs: raise BadRequestError('Appointment reservation requires valid "advisorUid"') Appointment.check_in( appointment_id=appointment_id, checked_in_by=current_user.get_id(), advisor_attrs=advisor_attrs, ) return Response(status=200)
def cancel_appointment(appointment_id): appointment = Appointment.find_by_id(appointment_id) if not appointment: raise ResourceNotFoundError('Unknown path') has_privilege = current_user.is_admin or appointment.dept_code in _dept_codes_with_scheduler_privilege() if not has_privilege: raise ForbiddenRequestError(f'You are unauthorized to manage {appointment.dept_code} appointments.') if not appointment.status_change_available(): raise BadRequestError(appointment.to_api_json(current_user.get_id())) params = request.get_json() cancel_reason = params.get('cancelReason', None) cancel_reason_explained = params.get('cancelReasonExplained', None) Appointment.cancel( appointment_id=appointment_id, cancelled_by=current_user.get_id(), cancel_reason=cancel_reason, cancel_reason_explained=cancel_reason_explained, ) return Response(status=200)
def test_appointment_search_with_no_input_and_date(self, coe_advisor, client): """Appointments search needs no input when date options are set.""" from boac import db, std_commit appointment = Appointment.find_by_id(2) appointment.created_at = util.localized_timestamp_to_utc( '2017-11-01T00:00:00') std_commit() db.session.refresh(appointment) api_json = _api_search( client, '', appointments=True, appointment_options={ 'dateFrom': '2017-11-01', 'dateTo': '2017-11-02' }, ) self._assert(api_json, appointment_count=3)
def cancel_appointment(appointment_id): appointment = Appointment.find_by_id(appointment_id) if not appointment: raise ResourceNotFoundError('Unknown path') if current_user.is_admin or appointment.dept_code in _dept_codes_with_scheduler_privilege( ): params = request.get_json() cancel_reason = params.get('cancelReason', None) cancel_reason_explained = params.get('cancelReasonExplained', None) appointment = Appointment.cancel( appointment_id=appointment_id, canceled_by=current_user.get_id(), cancel_reason=cancel_reason, cancel_reason_explained=cancel_reason_explained, ) api_json = appointment.to_api_json(current_user.get_id()) _put_student_profile_per_appointment([api_json]) return tolerant_jsonify(api_json) else: raise ForbiddenRequestError( f'You are unauthorized to manage {appointment.dept_code} appointments.' )
def unreserve_appointment(appointment_id): appointment = Appointment.find_by_id(appointment_id) if not appointment: raise ResourceNotFoundError('Unknown path') has_privilege = current_user.is_admin or appointment.dept_code in _dept_codes_with_scheduler_privilege( ) if has_privilege and appointment.status == 'reserved': event = AppointmentEvent.get_most_recent_per_type( appointment.id, 'reserved') if event.user_id == current_user.get_id(): appointment = Appointment.unreserve( appointment_id=appointment_id, unreserved_by=current_user.get_id(), ) api_json = appointment.to_api_json(current_user.get_id()) _put_student_profile_per_appointment([api_json]) return tolerant_jsonify(api_json) else: raise ForbiddenRequestError( f'You did not reserve appointment {appointment_id}.') else: raise ForbiddenRequestError( f'You are unauthorized to manage appointment {appointment_id}.')
def test_search_respects_date_filters(self, coe_advisor, client): """Search results include notes and appointments updated within provided date range.""" from boac import std_commit appointment = Appointment.find_by_id(2) appointment.updated_at = util.localized_timestamp_to_utc( '2017-10-31T00:00:00') std_commit(allow_test_environment=True) response = client.post( '/api/search', data=json.dumps({ 'appointments': True, 'notes': True, 'searchPhrase': 'making', 'noteOptions': { 'dateFrom': '2017-10-31', 'dateTo': '2017-11-01', }, }), content_type='application/json', ) self._assert(response, note_count=1, appointment_count=2, note_ids=['11667051-00001'])
def reopen_appointment(appointment_id): appointment = Appointment.find_by_id(appointment_id) if not appointment: raise ResourceNotFoundError('Unknown path') _set_appointment_to_waiting(appointment) return Response(status=200)