@ -15,12 +15,15 @@ from tildes.views.api.beta.api_utils import (
get_next_and_prev_link ,
get_next_and_prev_link ,
query_apply_pagination ,
query_apply_pagination ,
)
)
from tildes.views.api.beta.comment import comment_to_dict
from tildes.views.api.beta.topic import topic_to_dict
from tildes.views.api.beta.comment import comment_to_api_ dict
from tildes.views.api.beta.topic import topic_to_api_ dict
def _user_to_dict ( user : User ) - > dict :
""" Convert a User object to a dictionary for JSON serialization. """
def _user_to_api_dict ( user : User ) - > dict :
""" Convert a User object to a dictionary for JSON serialization.
The schema is defined in our OpenAPI YAML file .
"""
return {
return {
" username " : user . username ,
" username " : user . username ,
" joined_at " : user . created_time . isoformat ( ) ,
" joined_at " : user . created_time . isoformat ( ) ,
@ -85,16 +88,16 @@ def get_user(request: Request) -> dict: # noqa
processed_results = [ ]
processed_results = [ ]
for item in combined_results . results :
for item in combined_results . results :
if isinstance ( item , Topic ) :
if isinstance ( item , Topic ) :
processed_results . append ( topic_to_dict ( item ) )
processed_results . append ( topic_to_api_ dict ( item ) )
elif isinstance ( item , Comment ) :
elif isinstance ( item , Comment ) :
processed_results . append ( comment_to_dict ( request , item ) )
processed_results . append ( comment_to_api_ dict ( request , item ) )
# Construct the paging next and previous link if there are more topics
# Construct the paging next and previous link if there are more topics
( next_link , prev_link ) = get_next_and_prev_link ( request , combined_results )
( next_link , prev_link ) = get_next_and_prev_link ( request , combined_results )
# Construct the final response JSON object
# Construct the final response JSON object
response = {
response = {
" user " : _user_to_dict ( user ) ,
" user " : _user_to_api_ dict ( user ) ,
" history " : processed_results ,
" history " : processed_results ,
" pagination " : {
" pagination " : {
" num_items " : len ( processed_results ) ,
" num_items " : len ( processed_results ) ,
@ -144,7 +147,7 @@ def get_user_comments(request: Request) -> dict:
# Build the JSON history data
# Build the JSON history data
processed_comments = [ ]
processed_comments = [ ]
for comment in query_result . results :
for comment in query_result . results :
processed_comments . append ( comment_to_dict ( request , comment ) )
processed_comments . append ( comment_to_api_ dict ( request , comment ) )
# Construct the paging next and previous link if there are more comments
# Construct the paging next and previous link if there are more comments
( next_link , prev_link ) = get_next_and_prev_link ( request , query_result )
( next_link , prev_link ) = get_next_and_prev_link ( request , query_result )
@ -200,7 +203,7 @@ def get_user_topics(request: Request) -> dict:
# Build the JSON history data
# Build the JSON history data
processed_topics = [ ]
processed_topics = [ ]
for topic in query_result . results :
for topic in query_result . results :
processed_topics . append ( topic_to_dict ( topic ) )
processed_topics . append ( topic_to_api_ dict ( topic ) )
# Construct the paging next and previous link if there are more topics
# Construct the paging next and previous link if there are more topics
( next_link , prev_link ) = get_next_and_prev_link ( request , query_result )
( next_link , prev_link ) = get_next_and_prev_link ( request , query_result )