3
3
import django .contrib .auth .password_validation
4
4
import django .core .exceptions
5
5
import django .core .validators
6
- import django .utils .timezone
7
6
import pycountry
8
7
import rest_framework .exceptions
9
8
import rest_framework .serializers
10
- import rest_framework .status
11
9
import rest_framework_simplejwt .exceptions
12
10
import rest_framework_simplejwt .serializers
13
11
import rest_framework_simplejwt .tokens
14
- import rest_framework_simplejwt .views
15
12
16
13
import business .constants
17
14
import business .models as business_models
@@ -24,19 +21,19 @@ class CompanySignUpSerializer(rest_framework.serializers.ModelSerializer):
24
21
write_only = True ,
25
22
required = True ,
26
23
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 ,
29
26
style = {'input_type' : 'password' },
30
27
)
31
28
name = rest_framework .serializers .CharField (
32
29
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 ,
35
32
)
36
33
email = rest_framework .serializers .EmailField (
37
34
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 ,
40
37
validators = [
41
38
business .validators .UniqueEmailValidator (
42
39
'This email address is already registered.' ,
@@ -293,10 +290,17 @@ class PromoReadOnlySerializer(rest_framework.serializers.ModelSerializer):
293
290
read_only = True ,
294
291
)
295
292
target = TargetSerializer ()
293
+
296
294
promo_unique = rest_framework .serializers .SerializerMethodField ()
297
295
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
+ )
300
304
301
305
class Meta :
302
306
model = business_models .Promo
@@ -319,42 +323,12 @@ class Meta:
319
323
)
320
324
321
325
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
326
327
327
328
def get_like_count (self , obj ):
328
329
# TODO
329
330
return 0
330
331
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
-
358
332
def to_representation (self , instance ):
359
333
data = super ().to_representation (instance )
360
334
if instance .mode == business .constants .PROMO_MODE_COMMON :
@@ -389,7 +363,10 @@ class PromoDetailSerializer(rest_framework.serializers.ModelSerializer):
389
363
read_only = True ,
390
364
)
391
365
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
+ )
393
370
394
371
class Meta :
395
372
model = business_models .Promo
@@ -410,10 +387,7 @@ class Meta:
410
387
)
411
388
412
389
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
417
391
418
392
def update (self , instance , validated_data ):
419
393
target_data = validated_data .pop ('target' , None )
@@ -437,10 +411,3 @@ def validate(self, data):
437
411
def get_like_count (self , obj ):
438
412
# TODO
439
413
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