streams: Remove get_client_data function.

This commit changes the code to not use get_client_data
function and instead use `stream_to_dict` function to
get the stream data in a dictionary form. This is a
prep commit add stream traffic data to Stream objects.
This commit is contained in:
Sahil Batra
2023-07-26 16:11:42 +05:30
committed by Tim Abbott
parent 8c9ad30411
commit 2533e64be6
2 changed files with 10 additions and 13 deletions

View File

@@ -846,8 +846,9 @@ def stream_to_dict(stream: Stream) -> APIStreamDict:
def get_web_public_streams(realm: Realm) -> List[APIStreamDict]: # nocoverage def get_web_public_streams(realm: Realm) -> List[APIStreamDict]: # nocoverage
query = get_web_public_streams_queryset(realm) query = get_web_public_streams_queryset(realm)
streams = Stream.get_client_data(query) streams = query.only(*Stream.API_FIELDS)
return streams stream_dicts = [stream_to_dict(stream) for stream in streams]
return stream_dicts
def do_get_streams( def do_get_streams(
@@ -870,7 +871,7 @@ def do_get_streams(
query = Stream.objects.filter(realm=user_profile.realm, deactivated=False) query = Stream.objects.filter(realm=user_profile.realm, deactivated=False)
if include_all_active: if include_all_active:
streams = Stream.get_client_data(query) streams = query.only(*Stream.API_FIELDS)
else: else:
# We construct a query as the or (|) of the various sources # We construct a query as the or (|) of the various sources
# this user requested streams from. # this user requested streams from.
@@ -908,16 +909,17 @@ def do_get_streams(
if query_filter is not None: if query_filter is not None:
query = query.filter(query_filter) query = query.filter(query_filter)
streams = Stream.get_client_data(query) streams = query.only(*Stream.API_FIELDS)
else: else:
# Don't bother going to the database with no valid sources # Don't bother going to the database with no valid sources
streams = [] return []
streams.sort(key=lambda elt: elt["name"]) stream_dicts = [stream_to_dict(stream) for stream in streams]
stream_dicts.sort(key=lambda elt: elt["name"])
if include_default: if include_default:
default_stream_ids = get_default_stream_ids_for_realm(user_profile.realm_id) default_stream_ids = get_default_stream_ids_for_realm(user_profile.realm_id)
for stream in streams: for stream in stream_dicts:
stream["is_default"] = stream["stream_id"] in default_stream_ids stream["is_default"] = stream["stream_id"] in default_stream_ids
return streams return stream_dicts

View File

@@ -2701,11 +2701,6 @@ class Stream(models.Model):
"can_remove_subscribers_group_id", "can_remove_subscribers_group_id",
] ]
@staticmethod
def get_client_data(query: QuerySet["Stream"]) -> List[APIStreamDict]:
query = query.only(*Stream.API_FIELDS)
return [row.to_dict() for row in query]
def to_dict(self) -> APIStreamDict: def to_dict(self) -> APIStreamDict:
return APIStreamDict( return APIStreamDict(
can_remove_subscribers_group=self.can_remove_subscribers_group_id, can_remove_subscribers_group=self.can_remove_subscribers_group_id,