Beispiel #1
0
    def test_allows_advisor_to_change_departments(self):
        """Updates membership for a former CoE advisor who switches to L&S."""
        user = AuthorizedUser.find_by_uid('242881')
        dept_coe = UniversityDept.query.filter_by(dept_code='COENG').first()
        UniversityDeptMember.create_or_update_membership(
            dept_coe.id,
            user.id,
            role='advisor',
        )
        dept_ucls = UniversityDept.query.filter_by(
            dept_code='QCADVMAJ').first()
        UniversityDeptMember.delete_membership(dept_ucls.id, user.id)
        std_commit(allow_test_environment=True)

        ucls_users = [au.authorized_user for au in dept_ucls.authorized_users]
        assert len(ucls_users) == 2

        from boac.api.cache_utils import refresh_department_memberships
        refresh_department_memberships()
        std_commit(allow_test_environment=True)

        ucls_users = [au.authorized_user for au in dept_ucls.authorized_users]
        assert len(ucls_users) == 3
        assert next(u for u in ucls_users if u.uid == '242881')
        updated_user = AuthorizedUser.query.filter_by(uid='242881').first()
        assert updated_user.deleted_at is None
        assert updated_user.created_by == '0'
        assert updated_user.department_memberships[
            0].university_dept_id == dept_ucls.id
def _delete_existing_memberships(authorized_user):
    existing_memberships = UniversityDeptMember.get_existing_memberships(
        authorized_user_id=authorized_user.id)
    for university_dept_id in [
            m.university_dept.id for m in existing_memberships
    ]:
        UniversityDeptMember.delete_membership(
            university_dept_id=university_dept_id,
            authorized_user_id=authorized_user.id,
        )
def delete_university_dept_membership(university_dept_id, authorized_user_id):
    if not UniversityDeptMember.delete_membership(university_dept_id,
                                                  authorized_user_id):
        raise errors.ResourceNotFoundError(
            f'University dept membership not found: university_dept_id={university_dept_id} authorized_user_id={authorized_user_id}',
        )
    return tolerant_jsonify(
        {
            'message':
            f'University dept membership deleted: university_dept_id={university_dept_id} authorized_user_id={authorized_user_id}'
        },
        status=200,
    )
def remove_appointment_scheduler_from_dept(dept_code):
    _verify_membership_and_appointments_enabled(current_user, dept_code)
    params = request.get_json() or {}
    uid = params.get('uid')
    user = uid and AuthorizedUser.find_by_uid(uid)
    if not user:
        raise errors.BadRequestError(f'UID {uid} missing or invalid')
    scheduler_membership = next((
        d for d in user.department_memberships
        if d.university_dept.dept_code == dept_code and d.role == 'scheduler'),
                                None)
    if not scheduler_membership:
        raise errors.BadRequestError(
            f'UID {uid} is not a scheduler for department {dept_code}')
    UniversityDeptMember.delete_membership(
        university_dept_id=scheduler_membership.university_dept_id,
        authorized_user_id=user.id,
    )
    Scheduler.delete(authorized_user_id=user.id, dept_code=dept_code)
    if not len(user.department_memberships):
        AuthorizedUser.delete(uid)
    return tolerant_jsonify(
        _get_appointment_scheduler_list(current_user, dept_code))