1
1
import logging
2
2
from datetime import datetime
3
+ from typing import Any , Dict
3
4
4
5
from dateutil .relativedelta import relativedelta
5
6
from django .conf import settings
@@ -37,7 +38,7 @@ class Meta:
37
38
38
39
read_only_fields = fields
39
40
40
- def get_stats (self , obj ) :
41
+ def get_stats (self , obj : Owner ) -> str | None :
41
42
if obj .cache and "stats" in obj .cache :
42
43
return obj .cache ["stats" ]
43
44
@@ -50,7 +51,7 @@ class StripeLineItemSerializer(serializers.Serializer):
50
51
plan_name = serializers .SerializerMethodField ()
51
52
quantity = serializers .IntegerField ()
52
53
53
- def get_plan_name (self , line_item ) :
54
+ def get_plan_name (self , line_item : Dict [ str , str ]) -> str | None :
54
55
plan = line_item .get ("plan" )
55
56
if plan :
56
57
return plan .get ("name" )
@@ -85,7 +86,7 @@ class StripeDiscountSerializer(serializers.Serializer):
85
86
duration_in_months = serializers .IntegerField (source = "coupon.duration_in_months" )
86
87
expires = serializers .SerializerMethodField ()
87
88
88
- def get_expires (self , customer ) :
89
+ def get_expires (self , customer : Dict [ str , Dict ]) -> int | None :
89
90
coupon = customer .get ("coupon" )
90
91
if coupon :
91
92
months = coupon .get ("duration_in_months" )
@@ -121,7 +122,7 @@ class PlanSerializer(serializers.Serializer):
121
122
benefits = serializers .JSONField (read_only = True )
122
123
quantity = serializers .IntegerField (required = False )
123
124
124
- def validate_value (self , value ) :
125
+ def validate_value (self , value : str ) -> str :
125
126
current_org = self .context ["view" ].owner
126
127
current_owner = self .context ["request" ].current_owner
127
128
@@ -140,7 +141,7 @@ def validate_value(self, value):
140
141
)
141
142
return value
142
143
143
- def validate (self , plan ) :
144
+ def validate (self , plan : Dict [ str , Any ]) -> Dict [ str , Any ] :
144
145
current_org = self .context ["view" ].owner
145
146
if current_org .account :
146
147
raise serializers .ValidationError (
@@ -206,7 +207,7 @@ class StripeScheduledPhaseSerializer(serializers.Serializer):
206
207
plan = serializers .SerializerMethodField ()
207
208
quantity = serializers .SerializerMethodField ()
208
209
209
- def get_plan (self , phase ) :
210
+ def get_plan (self , phase : Dict [ str , Any ]) -> str :
210
211
plan_id = phase ["items" ][0 ]["plan" ]
211
212
stripe_plan_dict = settings .STRIPE_PLAN_IDS
212
213
plan_name = list (stripe_plan_dict .keys ())[
@@ -215,15 +216,15 @@ def get_plan(self, phase):
215
216
marketing_plan_name = PAID_PLANS [plan_name ].billing_rate
216
217
return marketing_plan_name
217
218
218
- def get_quantity (self , phase ) :
219
+ def get_quantity (self , phase : Dict [ str , Any ]) -> int :
219
220
return phase ["items" ][0 ]["quantity" ]
220
221
221
222
222
223
class ScheduleDetailSerializer (serializers .Serializer ):
223
224
id = serializers .CharField ()
224
225
scheduled_phase = serializers .SerializerMethodField ()
225
226
226
- def get_scheduled_phase (self , schedule ) :
227
+ def get_scheduled_phase (self , schedule : Dict [ str , Any ]) -> Dict [ str , Any ] | None :
227
228
if len (schedule ["phases" ]) > 1 :
228
229
return StripeScheduledPhaseSerializer (schedule ["phases" ][- 1 ]).data
229
230
else :
@@ -291,44 +292,44 @@ class Meta:
291
292
"uses_invoice" ,
292
293
)
293
294
294
- def _get_billing (self ):
295
+ def _get_billing (self ) -> BillingService :
295
296
current_owner = self .context ["request" ].current_owner
296
297
return BillingService (requesting_user = current_owner )
297
298
298
- def get_subscription_detail (self , owner ) :
299
+ def get_subscription_detail (self , owner : Owner ) -> Dict [ str , Any ] | None :
299
300
subscription_detail = self ._get_billing ().get_subscription (owner )
300
301
if subscription_detail :
301
302
return SubscriptionDetailSerializer (subscription_detail ).data
302
303
303
- def get_schedule_detail (self , owner ) :
304
+ def get_schedule_detail (self , owner : Owner ) -> Dict [ str , Any ] | None :
304
305
schedule_detail = self ._get_billing ().get_schedule (owner )
305
306
if schedule_detail :
306
307
return ScheduleDetailSerializer (schedule_detail ).data
307
308
308
- def get_checkout_session_id (self , _ ) :
309
+ def get_checkout_session_id (self , _ : Any ) -> str :
309
310
return self .context .get ("checkout_session_id" )
310
311
311
- def get_activated_student_count (self , owner ) :
312
+ def get_activated_student_count (self , owner : Owner ) -> int :
312
313
if owner .account :
313
314
return owner .account .activated_student_count
314
315
return owner .activated_student_count
315
316
316
- def get_activated_user_count (self , owner ) :
317
+ def get_activated_user_count (self , owner : Owner ) -> int :
317
318
if owner .account :
318
319
return owner .account .activated_user_count
319
320
return owner .activated_user_count
320
321
321
- def get_delinquent (self , owner ) :
322
+ def get_delinquent (self , owner : Owner ) -> bool :
322
323
if owner .account :
323
324
return owner .account .is_delinquent
324
325
return owner .delinquent
325
326
326
- def get_uses_invoice (self , owner ) :
327
+ def get_uses_invoice (self , owner : Owner ) -> bool :
327
328
if owner .account :
328
329
return owner .account .invoice_billing .filter (is_active = True ).exists ()
329
330
return owner .uses_invoice
330
331
331
- def update (self , instance , validated_data ) :
332
+ def update (self , instance : Owner , validated_data : Dict [ str , Any ]) -> object :
332
333
if "pretty_plan" in validated_data :
333
334
desired_plan = validated_data .pop ("pretty_plan" )
334
335
checkout_session_id_or_none = self ._get_billing ().update_plan (
@@ -367,7 +368,7 @@ class Meta:
367
368
"last_pull_timestamp" ,
368
369
)
369
370
370
- def update (self , instance , validated_data ) :
371
+ def update (self , instance : Owner , validated_data : Dict [ str , Any ]) -> object :
371
372
owner = self .context ["view" ].owner
372
373
373
374
if "activated" in validated_data :
@@ -391,7 +392,7 @@ def update(self, instance, validated_data):
391
392
# Re-fetch from DB to set activated and admin fields
392
393
return self .context ["view" ].get_object ()
393
394
394
- def get_last_pull_timestamp (self , obj ) :
395
+ def get_last_pull_timestamp (self , obj : Owner ) -> str | None :
395
396
# this field comes from an annotation that may not always be applied to the queryset
396
397
if hasattr (obj , "last_pull_timestamp" ):
397
398
return obj .last_pull_timestamp
0 commit comments