示例#1
0
def get_raw_user_data(realm_id, client_gravatar):
    # type: (int, bool) -> Dict[int, Dict[str, Text]]
    user_dicts = get_realm_user_dicts(realm_id)

    def user_data(row):
        # type: (Dict[str, Any]) -> Dict[str, Any]
        avatar_url = get_avatar_field(
            user_id=row['id'],
            realm_id= realm_id,
            email=row['email'],
            avatar_source=row['avatar_source'],
            avatar_version=row['avatar_version'],
            medium=False,
            client_gravatar=client_gravatar,
        )

        is_admin = row['is_realm_admin']

        return dict(
            email=row['email'],
            user_id=row['id'],
            avatar_url=avatar_url,
            is_admin=is_admin,
            is_bot=row['is_bot'],
            full_name=row['full_name'],
            timezone=row['timezone'],
            is_active = row['is_active'],
        )

    return {
        row['id']: user_data(row)
        for row in user_dicts
    }
示例#2
0
def get_raw_user_data(realm_id: int, client_gravatar: bool) -> Dict[int, Dict[str, Text]]:
    user_dicts = get_realm_user_dicts(realm_id)

    def user_data(row: Dict[str, Any]) -> Dict[str, Any]:
        avatar_url = get_avatar_field(
            user_id=row['id'],
            realm_id= realm_id,
            email=row['email'],
            avatar_source=row['avatar_source'],
            avatar_version=row['avatar_version'],
            medium=False,
            client_gravatar=client_gravatar,
        )

        is_admin = row['is_realm_admin']

        return dict(
            email=row['email'],
            user_id=row['id'],
            avatar_url=avatar_url,
            is_admin=is_admin,
            is_bot=row['is_bot'],
            full_name=row['full_name'],
            timezone=row['timezone'],
            is_active = row['is_active'],
        )

    return {
        row['id']: user_data(row)
        for row in user_dicts
    }
示例#3
0
def get_raw_user_data(
        realm: Realm,
        user_profile: UserProfile,
        client_gravatar: bool,
        for_api: bool = False,
        include_custom_profile_fields: bool = True
) -> Dict[int, Dict[str, str]]:
    user_dicts = get_realm_user_dicts(realm.id)

    if include_custom_profile_fields:
        profiles_by_user_id = get_custom_profile_field_values(realm.id)

    def user_data(row: Dict[str, Any]) -> Dict[str, Any]:
        avatar_url = get_avatar_field(
            user_id=row['id'],
            realm_id=realm.id,
            email=row['email'],
            avatar_source=row['avatar_source'],
            avatar_version=row['avatar_version'],
            medium=False,
            client_gravatar=client_gravatar,
        )

        is_admin = row['role'] == UserProfile.ROLE_REALM_ADMINISTRATOR
        is_guest = row['role'] == UserProfile.ROLE_GUEST
        is_bot = row['is_bot']
        # This format should align with get_cross_realm_dicts() and notify_created_user
        result = dict(
            email=row['email'],
            user_id=row['id'],
            avatar_url=avatar_url,
            is_admin=is_admin,
            is_guest=is_guest,
            is_bot=is_bot,
            full_name=row['full_name'],
            timezone=row['timezone'],
            is_active=row['is_active'],
            date_joined=row['date_joined'].isoformat(),
        )

        if (realm.email_address_visibility
                == Realm.EMAIL_ADDRESS_VISIBILITY_ADMINS
                and user_profile.is_realm_admin):
            result['delivery_email'] = row['delivery_email']

        if for_api:
            # The API currently has a quirk that it expects to include
            # a bot_type field even for human users; this field is
            # invalid so we plan to eventually remove this.
            result['bot_type'] = row['bot_type']
        if is_bot:
            if row['email'] in settings.CROSS_REALM_BOT_EMAILS:
                result['is_cross_realm_bot'] = True
            elif row['bot_owner_id'] is not None:
                result['bot_owner_id'] = row['bot_owner_id']
        elif include_custom_profile_fields:
            result['profile_data'] = profiles_by_user_id.get(row['id'], {})
        return result

    return {row['id']: user_data(row) for row in user_dicts}
示例#4
0
def get_raw_user_data(realm: Realm, client_gravatar: bool) -> Dict[int, Dict[str, str]]:
    user_dicts = get_realm_user_dicts(realm.id)

    # TODO: Consider optimizing this query away with caching.
    custom_profile_field_values = CustomProfileFieldValue.objects.select_related(
        "field").filter(user_profile__realm_id=realm.id)
    profiles_by_user_id = defaultdict(dict)  # type: Dict[int, Dict[str, Any]]
    for profile_field in custom_profile_field_values:
        user_id = profile_field.user_profile_id
        if profile_field.field.is_renderable():
            profiles_by_user_id[user_id][profile_field.field_id] = {
                "value": profile_field.value,
                "rendered_value": profile_field.rendered_value
            }
        else:
            profiles_by_user_id[user_id][profile_field.field_id] = {
                "value": profile_field.value
            }

    def user_data(row: Dict[str, Any]) -> Dict[str, Any]:
        avatar_url = get_avatar_field(
            user_id=row['id'],
            realm_id=realm.id,
            email=row['email'],
            avatar_source=row['avatar_source'],
            avatar_version=row['avatar_version'],
            medium=False,
            client_gravatar=client_gravatar,
        )

        is_admin = row['is_realm_admin']
        is_guest = row['is_guest']
        is_bot = row['is_bot']
        # This format should align with get_cross_realm_dicts() and notify_created_user
        result = dict(
            email=row['email'],
            user_id=row['id'],
            avatar_url=avatar_url,
            is_admin=is_admin,
            is_guest=is_guest,
            is_bot=is_bot,
            full_name=row['full_name'],
            timezone=row['timezone'],
            is_active = row['is_active'],
            date_joined = row['date_joined'].isoformat(),
        )
        if is_bot:
            if row['email'] in settings.CROSS_REALM_BOT_EMAILS:
                result['is_cross_realm_bot'] = True
            elif row['bot_owner_id'] is not None:
                result['bot_owner_id'] = row['bot_owner_id']
        else:
            result['profile_data'] = profiles_by_user_id.get(row['id'], {})
        return result

    return {
        row['id']: user_data(row)
        for row in user_dicts
    }
示例#5
0
def get_raw_user_data(realm_id: int, client_gravatar: bool) -> Dict[int, Dict[str, str]]:
    user_dicts = get_realm_user_dicts(realm_id)

    # TODO: Consider optimizing this query away with caching.
    custom_profile_field_values = CustomProfileFieldValue.objects.select_related(
        "field").filter(user_profile__realm_id=realm_id)
    profiles_by_user_id = defaultdict(dict)  # type: Dict[int, Dict[str, Any]]
    for profile_field in custom_profile_field_values:
        user_id = profile_field.user_profile_id
        if profile_field.field.is_renderable():
            profiles_by_user_id[user_id][profile_field.field_id] = {
                "value": profile_field.value,
                "rendered_value": profile_field.rendered_value
            }
        else:
            profiles_by_user_id[user_id][profile_field.field_id] = {
                "value": profile_field.value
            }

    def user_data(row: Dict[str, Any]) -> Dict[str, Any]:
        avatar_url = get_avatar_field(
            user_id=row['id'],
            realm_id= realm_id,
            email=row['email'],
            avatar_source=row['avatar_source'],
            avatar_version=row['avatar_version'],
            medium=False,
            client_gravatar=client_gravatar,
        )

        is_admin = row['is_realm_admin']
        is_guest = row['is_guest']
        is_bot = row['is_bot']
        # This format should align with get_cross_realm_dicts() and notify_created_user
        result = dict(
            email=row['email'],
            user_id=row['id'],
            avatar_url=avatar_url,
            is_admin=is_admin,
            is_guest=is_guest,
            is_bot=is_bot,
            full_name=row['full_name'],
            timezone=row['timezone'],
            is_active = row['is_active'],
            date_joined = row['date_joined'].isoformat(),
        )
        if not is_bot:
            result['profile_data'] = profiles_by_user_id.get(row['id'], {})
        return result

    return {
        row['id']: user_data(row)
        for row in user_dicts
    }
示例#6
0
def get_raw_user_data(
    realm: Realm,
    acting_user: Optional[UserProfile],
    *,
    target_user: Optional[UserProfile] = None,
    client_gravatar: bool,
    user_avatar_url_field_optional: bool,
    include_custom_profile_fields: bool = True,
) -> Dict[int, Dict[str, str]]:
    """Fetches data about the target user(s) appropriate for sending to
    acting_user via the standard format for the Zulip API.  If
    target_user is None, we fetch all users in the realm.
    """
    profiles_by_user_id = None
    custom_profile_field_data = None
    # target_user is an optional parameter which is passed when user data of a specific user
    # is required. It is 'None' otherwise.
    if target_user is not None:
        user_dicts = [user_profile_to_user_row(target_user)]
    else:
        user_dicts = get_realm_user_dicts(realm.id)

    if include_custom_profile_fields:
        base_query = CustomProfileFieldValue.objects.select_related("field")
        # TODO: Consider optimizing this query away with caching.
        if target_user is not None:
            custom_profile_field_values = base_query.filter(
                user_profile=target_user)
        else:
            custom_profile_field_values = base_query.filter(
                field__realm_id=realm.id)
        profiles_by_user_id = get_custom_profile_field_values(
            custom_profile_field_values)

    result = {}
    for row in user_dicts:
        if profiles_by_user_id is not None:
            custom_profile_field_data = profiles_by_user_id.get(row["id"], {})

        result[row["id"]] = format_user_row(
            realm,
            acting_user=acting_user,
            row=row,
            client_gravatar=client_gravatar,
            user_avatar_url_field_optional=user_avatar_url_field_optional,
            custom_profile_field_data=custom_profile_field_data,
        )
    return result
示例#7
0
def get_raw_user_data(realm_id: int,
                      client_gravatar: bool) -> Dict[int, Dict[str, str]]:
    user_dicts = get_realm_user_dicts(realm_id)

    # TODO: Consider optimizing this query away with caching.
    custom_profile_field_values = CustomProfileFieldValue.objects.filter(
        user_profile__realm_id=realm_id)
    profiles_by_user_id = defaultdict(dict)  # type: Dict[int, Dict[str, Any]]
    for profile_field in custom_profile_field_values:
        user_id = profile_field.user_profile_id
        profiles_by_user_id[user_id][
            profile_field.field_id] = profile_field.value

    def user_data(row: Dict[str, Any]) -> Dict[str, Any]:
        avatar_url = get_avatar_field(
            user_id=row['id'],
            realm_id=realm_id,
            email=row['email'],
            avatar_source=row['avatar_source'],
            avatar_version=row['avatar_version'],
            medium=False,
            client_gravatar=client_gravatar,
        )

        is_admin = row['is_realm_admin']
        is_guest = row['is_guest']
        is_bot = row['is_bot']
        # This format should align with get_cross_realm_dicts() and notify_created_user
        result = dict(
            email=row['email'],
            user_id=row['id'],
            avatar_url=avatar_url,
            is_admin=is_admin,
            is_guest=is_guest,
            is_bot=is_bot,
            full_name=row['full_name'],
            timezone=row['timezone'],
            is_active=row['is_active'],
            date_joined=row['date_joined'].isoformat(),
        )
        if not is_bot:
            result['profile_data'] = profiles_by_user_id.get(row['id'], {})
        return result

    return {row['id']: user_data(row) for row in user_dicts}
示例#8
0
文件: events.py 项目: phansen01/zulip
def get_raw_user_data(realm_id: int, client_gravatar: bool) -> Dict[int, Dict[str, str]]:
    user_dicts = get_realm_user_dicts(realm_id)

    # TODO: Consider optimizing this query away with caching.
    custom_profile_field_values = CustomProfileFieldValue.objects.filter(user_profile__realm_id=realm_id)
    profiles_by_user_id = defaultdict(dict)  # type: Dict[int, Dict[str, Any]]
    for profile_field in custom_profile_field_values:  # nocoverage # TODO: Fix this.
        user_id = profile_field.user_profile_id
        profiles_by_user_id[user_id][profile_field.field_id] = profile_field.value

    def user_data(row: Dict[str, Any]) -> Dict[str, Any]:
        avatar_url = get_avatar_field(
            user_id=row['id'],
            realm_id= realm_id,
            email=row['email'],
            avatar_source=row['avatar_source'],
            avatar_version=row['avatar_version'],
            medium=False,
            client_gravatar=client_gravatar,
        )

        is_admin = row['is_realm_admin']
        is_bot = row['is_bot']
        result = dict(
            email=row['email'],
            user_id=row['id'],
            avatar_url=avatar_url,
            is_admin=is_admin,
            is_bot=is_bot,
            full_name=row['full_name'],
            timezone=row['timezone'],
            is_active = row['is_active'],
            date_joined = row['date_joined'].isoformat(),
        )
        if not is_bot:
            result['profile_data'] = profiles_by_user_id.get(row['id'], {})
        return result

    return {
        row['id']: user_data(row)
        for row in user_dicts
    }
示例#9
0
def get_raw_user_data(realm_id: int, client_gravatar: bool) -> Dict[int, Dict[str, Text]]:
    user_dicts = get_realm_user_dicts(realm_id)
    # TODO: Consider optimizing this query away with caching.
    custom_profile_field_values = CustomProfileFieldValue.objects.filter(user_profile_id__in=[
        row['id'] for row in user_dicts
    ])
    profiles_by_user_id = defaultdict(dict)  # type: Dict[int, Dict[str, Any]]
    for profile_field in custom_profile_field_values:  # nocoverage # TODO: Fix this.
        user_id = profile_field.user_profile_id
        profiles_by_user_id[user_id][profile_field.field_id] = profile_field.value

    def user_data(row: Dict[str, Any]) -> Dict[str, Any]:
        avatar_url = get_avatar_field(
            user_id=row['id'],
            realm_id= realm_id,
            email=row['email'],
            avatar_source=row['avatar_source'],
            avatar_version=row['avatar_version'],
            medium=False,
            client_gravatar=client_gravatar,
        )

        is_admin = row['is_realm_admin']
        is_bot = row['is_bot']
        result = dict(
            email=row['email'],
            user_id=row['id'],
            avatar_url=avatar_url,
            is_admin=is_admin,
            is_bot=is_bot,
            full_name=row['full_name'],
            timezone=row['timezone'],
            is_active = row['is_active'],
        )
        if not is_bot:
            result['profile_data'] = profiles_by_user_id.get(row['id'], {})
        return result

    return {
        row['id']: user_data(row)
        for row in user_dicts
    }
示例#10
0
def get_raw_user_data(realm_id):
    # type: (int) -> Dict[int, Dict[str, Text]]
    user_dicts = get_realm_user_dicts(realm_id)

    def user_data(row):
        # type: (Dict[str, Any]) -> Dict[str, Any]
        avatar_url = avatar_url_from_dict(row)
        is_admin = row['is_realm_admin']

        return dict(
            email=row['email'],
            user_id=row['id'],
            avatar_url=avatar_url,
            is_admin=is_admin,
            is_bot=row['is_bot'],
            full_name=row['full_name'],
            timezone=row['timezone'],
            is_active=row['is_active'],
        )

    return {row['id']: user_data(row) for row in user_dicts}
示例#11
0
def get_raw_user_data(
        realm: Realm,
        user_profile: UserProfile,
        client_gravatar: bool,
        include_custom_profile_fields: bool = True
) -> Dict[int, Dict[str, str]]:
    user_dicts = get_realm_user_dicts(realm.id)
    profiles_by_user_id = None
    custom_profile_field_data = None

    if include_custom_profile_fields:
        profiles_by_user_id = get_custom_profile_field_values(realm.id)
    result = {}
    for row in user_dicts:
        if profiles_by_user_id is not None:
            custom_profile_field_data = profiles_by_user_id.get(row['id'], {})

        result[row['id']] = format_user_row(
            realm,
            acting_user=user_profile,
            row=row,
            client_gravatar=client_gravatar,
            custom_profile_field_data=custom_profile_field_data)
    return result