Exemplo n.º 1
0
def refresh_cohort_attributes_from_calnet(app, cohorts=None):
    members = cohorts or TeamMember.query.all()
    # Students who play more than one sport will have multiple records.
    member_map = {}
    for m in members:
        member_map.setdefault(m.member_csid, []).append(m)
    csids = list(member_map.keys())

    # Search LDAP.
    all_attrs = calnet.client(app).search_csids(csids)
    if len(csids) != len(all_attrs):
        app.logger.warning('Looked for {} CSIDS but only found {}'.format(
            len(csids),
            len(all_attrs),
        ))

    # Update the DB.
    for attrs in all_attrs:
        # Since we searched LDAP by CSID, we can be fairly sure that the results have CSIDs.
        csid = attrs['csid']
        for m in member_map[csid]:
            m.member_uid = attrs['uid']
            # A manually-entered ASC name may be more nicely formatted than a student's CalNet default.
            # For now, don't overwrite it.
            m.member_name = m.member_name or attrs['sortable_name']
    return members
Exemplo n.º 2
0
def _get_calnet_users(app, id_type, ids):
    cached_users = fetch_bulk(
        [f'calnet_user_for_{id_type}_{_id}' for _id in ids])
    users_by_id = {
        k.replace(f'calnet_user_for_{id_type}_', ''): v
        for k, v in cached_users.items()
    }
    uncached_ids = [c for c in ids if c not in users_by_id]
    calnet_client = calnet.client(app)
    if id_type == 'uid':
        calnet_results = calnet_client.search_uids(uncached_ids)
    elif id_type == 'csid':
        calnet_results = calnet_client.search_csids(uncached_ids)
    else:
        raise InternalServerError(
            f'get_calnet_users: {id_type} is an invalid id type')
    # Cache rows individually so that an isolated conflict doesn't sink the rest of the update.
    for _id in uncached_ids:
        calnet_result = next((r for r in calnet_results if r[id_type] == _id),
                             None)
        feed = {
            **_calnet_user_api_feed(calnet_result),
            **{
                id_type: _id
            },
        }
        insert_row(f'calnet_user_for_{id_type}_{_id}', feed)
        users_by_id[_id] = feed
    return users_by_id
Exemplo n.º 3
0
def get_calnet_user_for_uid(app, uid):
    persons = calnet.client(app).search_uids([uid])
    p = persons[0] if len(persons) > 0 else None
    return {
        'uid': uid,
        'firstName': p and p['first_name'],
        'lastName': p and p['last_name'],
    }
Exemplo n.º 4
0
def get_calnet_user_for_uid(app,
                            uid,
                            force_feed=True,
                            skip_expired_users=False):
    if skip_expired_users:
        persons = calnet.client(app).search_uids([uid])
    else:
        for search_expired in (False, True):
            persons = calnet.client(app).search_uids([uid], search_expired)
            if persons:
                break
    if not persons and not force_feed:
        return None
    return {
        **_calnet_user_api_feed(persons[0] if len(persons) else None),
        **{
            'uid': uid
        },
    }
Exemplo n.º 5
0
def get_calnet_user_for_csid(app, csid):
    for search_expired in (False, True):
        persons = calnet.client(app).search_csids([csid], search_expired)
        if persons:
            break
    return {
        **_calnet_user_api_feed(persons[0] if len(persons) else None),
        **{
            'csid': csid
        },
    }