33import django .contrib .auth .password_validation
44import django .core .exceptions
55import django .core .validators
6- import django .utils .timezone
76import pycountry
87import rest_framework .exceptions
98import rest_framework .serializers
10- import rest_framework .status
119import rest_framework_simplejwt .exceptions
1210import rest_framework_simplejwt .serializers
1311import rest_framework_simplejwt .tokens
14- import rest_framework_simplejwt .views
1512
1613import business .constants
1714import business .models as business_models
@@ -24,19 +21,19 @@ class CompanySignUpSerializer(rest_framework.serializers.ModelSerializer):
2421 write_only = True ,
2522 required = True ,
2623 validators = [django .contrib .auth .password_validation .validate_password ],
27- min_length = 8 ,
28- max_length = 60 ,
24+ min_length = business . constants . COMPANY_PASSWORD_MIN_LENGTH ,
25+ max_length = business . constants . COMPANY_PASSWORD_MAX_LENGTH ,
2926 style = {'input_type' : 'password' },
3027 )
3128 name = rest_framework .serializers .CharField (
3229 required = True ,
33- min_length = 5 ,
34- max_length = 50 ,
30+ min_length = business . constants . COMPANY_NAME_MIN_LENGTH ,
31+ max_length = business . constants . COMPANY_NAME_MAX_LENGTH ,
3532 )
3633 email = rest_framework .serializers .EmailField (
3734 required = True ,
38- min_length = 8 ,
39- max_length = 120 ,
35+ min_length = business . constants . COMPANY_EMAIL_MIN_LENGTH ,
36+ max_length = business . constants . COMPANY_EMAIL_MAX_LENGTH ,
4037 validators = [
4138 business .validators .UniqueEmailValidator (
4239 'This email address is already registered.' ,
@@ -293,10 +290,17 @@ class PromoReadOnlySerializer(rest_framework.serializers.ModelSerializer):
293290 read_only = True ,
294291 )
295292 target = TargetSerializer ()
293+
296294 promo_unique = rest_framework .serializers .SerializerMethodField ()
297295 like_count = rest_framework .serializers .SerializerMethodField ()
298- used_count = rest_framework .serializers .SerializerMethodField ()
299- active = rest_framework .serializers .SerializerMethodField ()
296+ used_count = rest_framework .serializers .IntegerField (
297+ source = 'get_used_codes_count' ,
298+ read_only = True ,
299+ )
300+ active = rest_framework .serializers .BooleanField (
301+ source = 'is_active' ,
302+ read_only = True ,
303+ )
300304
301305 class Meta :
302306 model = business_models .Promo
@@ -319,42 +323,12 @@ class Meta:
319323 )
320324
321325 def get_promo_unique (self , obj ):
322- if obj .mode == business .constants .PROMO_MODE_UNIQUE :
323- return [code .code for code in obj .unique_codes .all ()]
324-
325- return None
326+ return obj .get_available_unique_codes
326327
327328 def get_like_count (self , obj ):
328329 # TODO
329330 return 0
330331
331- def get_used_count (self , obj ):
332- if obj .mode == business .constants .PROMO_MODE_UNIQUE :
333- return obj .unique_codes .filter (is_used = True ).count ()
334-
335- # TODO
336- return 0
337-
338- def get_active (self , obj ):
339- now = django .utils .timezone .now ().date ()
340- active_from = obj .active_from
341- active_until = obj .active_until
342-
343- date_active = True
344-
345- if active_from and active_from > now :
346- date_active = False
347-
348- if active_until and active_until < now :
349- date_active = False
350-
351- else :
352- max_count_condition = obj .unique_codes .filter (
353- is_used = False ,
354- ).exists ()
355-
356- return date_active and max_count_condition
357-
358332 def to_representation (self , instance ):
359333 data = super ().to_representation (instance )
360334 if instance .mode == business .constants .PROMO_MODE_COMMON :
@@ -389,7 +363,10 @@ class PromoDetailSerializer(rest_framework.serializers.ModelSerializer):
389363 read_only = True ,
390364 )
391365 like_count = rest_framework .serializers .SerializerMethodField ()
392- used_count = rest_framework .serializers .SerializerMethodField ()
366+ used_count = rest_framework .serializers .IntegerField (
367+ source = 'get_used_codes_count' ,
368+ read_only = True ,
369+ )
393370
394371 class Meta :
395372 model = business_models .Promo
@@ -410,10 +387,7 @@ class Meta:
410387 )
411388
412389 def get_promo_unique (self , obj ):
413- if obj .mode == business .constants .PROMO_MODE_UNIQUE :
414- return [code .code for code in obj .unique_codes .all ()]
415-
416- return None
390+ return obj .get_available_unique_codes
417391
418392 def update (self , instance , validated_data ):
419393 target_data = validated_data .pop ('target' , None )
@@ -437,10 +411,3 @@ def validate(self, data):
437411 def get_like_count (self , obj ):
438412 # TODO
439413 return 0
440-
441- def get_used_count (self , obj ):
442- if obj .mode == business .constants .PROMO_MODE_UNIQUE :
443- return obj .unique_codes .filter (is_used = True ).count ()
444-
445- # TODO
446- return 0
0 commit comments