11from core .const .tag import OpenAPITag
2- from django .db .models import CharField , DecimalField , Exists , F , OuterRef , Q , Subquery , Sum , Value
2+ from django .db .models import CharField , DecimalField , Exists , F , OuterRef , Subquery , Sum , TextField , Value
33from django .db .models .functions import Coalesce
44from django_filters import rest_framework as filters
55from drf_spectacular .utils import extend_schema , extend_schema_view
66from rest_framework import mixins , routers , serializers , status , viewsets
7- from shop .order .models import Order , OrderProductOptionRelation , OrderProductRelation
7+ from shop .order .models import Order , OrderProductRelation , TicketInfo
88from shop .payment_history .models import REFUNDABLE_STATUSES , PaymentHistory
99
1010
@@ -18,27 +18,12 @@ class Meta:
1818
1919class PatronSerializer (serializers .ModelSerializer ):
2020 name = serializers .CharField (source = "customer_info.name" , allow_null = True )
21+ contribution_message = serializers .CharField (read_only = True )
2122
2223 class Meta :
23- fields : list [str ] = ["name" ]
24+ fields : list [str ] = ["name" , "contribution_message" ]
2425 model = Order
2526
26- def to_representation (self , instance : Order ) -> dict [str , str ]:
27- result = super ().to_representation (instance )
28-
29- opor : OrderProductOptionRelation = (
30- OrderProductOptionRelation .objects .filter_active ()
31- .filter (
32- Q (product_option_group__name__contains = "후원자" ) | Q (product_option_group__name__contains = "message" ),
33- order_product_relation__order = instance ,
34- order_product_relation__deleted_at__isnull = True ,
35- product_option_group__name__contains = "후원자" ,
36- product_option_group__is_custom_response = True ,
37- )
38- .first ()
39- )
40- return result | {"contribution_message" : opor .custom_response if opor else "" }
41-
4227
4328@extend_schema_view (
4429 list = extend_schema (
@@ -64,11 +49,23 @@ class PatronViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
6449 .annotate (total = Sum (F ("price" ) + F ("donation_price" )))
6550 .values ("total" )
6651 )
52+ contribution_message_sq = (
53+ TicketInfo .objects .filter_active ()
54+ .filter (order_product_relation__order_id = OuterRef ("id" ), order_product_relation__deleted_at__isnull = True )
55+ .exclude (contribution_message__isnull = True )
56+ .exclude (contribution_message = "" )
57+ .values ("contribution_message" )[:1 ]
58+ )
6759
6860 queryset = (
6961 Order .objects .filter_active ()
7062 .annotate (
7163 current_status = Subquery (latest_status_sq , output_field = CharField ()),
64+ contribution_message = Coalesce (
65+ Subquery (contribution_message_sq , output_field = TextField ()),
66+ Value ("" ),
67+ output_field = TextField (),
68+ ),
7269 has_donation_product = Exists (
7370 OrderProductRelation .objects .filter_active ().filter (
7471 order_id = OuterRef ("id" ),
0 commit comments