11
11
read_messages_at_postgres ,
12
12
read_messages_at_redis ,
13
13
)
14
- from apps .product .models import ProductImage
14
+ from apps .product .models import ProductImage , RentalHistory
15
+ from apps .product .serializers import RentalHistoryStatusSerializer
15
16
16
17
17
18
class ChatroomListSerializer (serializers .ModelSerializer [Chatroom ]):
@@ -79,10 +80,13 @@ class EnterChatroomSerializer(serializers.ModelSerializer[Chatroom]):
79
80
product_name = serializers .CharField (source = "product.name" , read_only = True )
80
81
product_rental_fee = serializers .IntegerField (source = "product.rental_fee" , read_only = True )
81
82
product_condition = serializers .CharField (source = "product.condition" , read_only = True )
83
+ rental_history = serializers .SerializerMethodField ()
82
84
83
85
class Meta :
84
86
model = Chatroom
85
- fields = ["product" , "product_image" , "product_name" , "product_rental_fee" , "product_condition" ]
87
+ fields = [
88
+ "product" , "product_image" , "product_name" , "product_rental_fee" , "product_condition" , "rental_history"
89
+ ]
86
90
87
91
def to_representation (self , instance : Chatroom ) -> Dict [str , Any ]:
88
92
data = super ().to_representation (instance )
@@ -97,9 +101,20 @@ def to_representation(self, instance: Chatroom) -> Dict[str, Any]:
97
101
data ["messages" ] = messages
98
102
return data
99
103
100
- def get_product_image (self , obj : ProductImage ) -> Any :
104
+ def get_product_image (self , obj : Chatroom ) -> Any :
101
105
if obj .product :
102
106
product_images = obj .product .images .first ()
103
107
if product_images :
104
108
return product_images .image .url # 이미지의 URL을 리턴
105
109
return None # 이미지가 없을 경우 None을 리턴
110
+
111
+ def get_rental_history (self , obj : Chatroom ) -> Optional [dict [str , Any ]]:
112
+ if obj .product :
113
+ chat_product = obj .product
114
+ try :
115
+ rental_history = chat_product .rentalhistory_set .get (borrower = obj .borrower , product = obj .product )
116
+ return RentalHistoryStatusSerializer (rental_history ).data
117
+
118
+ except RentalHistory .DoesNotExist :
119
+ return None
120
+ return None
0 commit comments