@@ -246,104 +246,23 @@ class Meta:
246246 )
247247
248248 def validate (self , data ):
249- mode = data .get ('mode' )
250- promo_common = data .get ('promo_common' )
251- promo_unique = data .get ('promo_unique' )
252- max_count = data .get ('max_count' )
253-
254- if mode == business_models .Promo .MODE_COMMON :
255- if not promo_common :
256- raise rest_framework .serializers .ValidationError (
257- {
258- 'promo_common' : (
259- 'This field is required for COMMON mode.'
260- ),
261- },
262- )
263-
264- if promo_unique is not None :
265- raise rest_framework .serializers .ValidationError (
266- {
267- 'promo_unique' : (
268- 'This field is not allowed for COMMON mode.'
269- ),
270- },
271- )
272-
273- if max_count < 0 or max_count > 100000000 :
274- raise rest_framework .serializers .ValidationError (
275- {
276- 'max_count' : (
277- 'Must be between 0 and 100,000,000 '
278- 'for COMMON mode.'
279- ),
280- },
281- )
282-
283- elif mode == business_models .Promo .MODE_UNIQUE :
284- if not promo_unique :
285- raise rest_framework .serializers .ValidationError (
286- {
287- 'promo_unique' : (
288- 'This field is required for UNIQUE mode.'
289- ),
290- },
291- )
292-
293- if promo_common is not None :
294- raise rest_framework .serializers .ValidationError (
295- {
296- 'promo_common' : (
297- 'This field is not allowed for UNIQUE mode.'
298- ),
299- },
300- )
301-
302- if max_count != 1 :
303- raise rest_framework .serializers .ValidationError (
304- {'max_count' : 'Must be 1 for UNIQUE mode.' },
305- )
306-
307- else :
308- raise rest_framework .serializers .ValidationError (
309- {'mode' : 'Invalid mode.' },
310- )
311-
312- active_from = data .get ('active_from' )
313- active_until = data .get ('active_until' )
314- if active_from and active_until and active_from > active_until :
315- raise rest_framework .serializers .ValidationError (
316- {'active_until' : 'Must be after or equal to active_from.' },
317- )
318-
319- return data
249+ data = super ().validate (data )
250+ validator = business .validators .PromoValidator (data = data )
251+ return validator .validate ()
320252
321253 def create (self , validated_data ):
322254 target_data = validated_data .pop ('target' )
323255 promo_common = validated_data .pop ('promo_common' , None )
324256 promo_unique = validated_data .pop ('promo_unique' , None )
325- mode = validated_data ['mode' ]
326-
327- user = self .context ['request' ].user
328- validated_data ['company' ] = user
329257
330- promo = business_models .Promo .objects .create (
258+ return business_models .Promo .objects .create_promo (
259+ user = self .context ['request' ].user ,
260+ target_data = target_data ,
261+ promo_common = promo_common ,
262+ promo_unique = promo_unique ,
331263 ** validated_data ,
332- target = target_data ,
333264 )
334265
335- if mode == business_models .Promo .MODE_COMMON :
336- promo .promo_common = promo_common
337- promo .save ()
338- elif mode == business_models .Promo .MODE_UNIQUE and promo_unique :
339- promo_codes = [
340- business_models .PromoCode (promo = promo , code = code )
341- for code in promo_unique
342- ]
343- business_models .PromoCode .objects .bulk_create (promo_codes )
344-
345- return promo
346-
347266 def to_representation (self , instance ):
348267 data = super ().to_representation (instance )
349268 data ['target' ] = instance .target
@@ -505,74 +424,12 @@ def update(self, instance, validated_data):
505424 return instance
506425
507426 def validate (self , data ):
508- instance = self .instance
509- full_data = {
510- 'mode' : instance .mode ,
511- 'promo_common' : instance .promo_common ,
512- 'promo_unique' : None ,
513- 'max_count' : instance .max_count ,
514- 'active_from' : instance .active_from ,
515- 'active_until' : instance .active_until ,
516- 'target' : instance .target if instance .target is not None else {},
517- }
518- full_data .update (data )
519- mode = full_data .get ('mode' )
520- promo_common = full_data .get ('promo_common' )
521- promo_unique = full_data .get ('promo_unique' )
522- max_count = full_data .get ('max_count' )
523-
524- if mode == business_models .Promo .MODE_COMMON :
525- if not promo_common :
526- raise rest_framework .serializers .ValidationError (
527- {
528- 'promo_common' : (
529- 'This field is required for COMMON mode.'
530- ),
531- },
532- )
533-
534- if promo_unique is not None :
535- raise rest_framework .serializers .ValidationError (
536- {
537- 'promo_unique' : (
538- 'This field is not allowed for COMMON mode.'
539- ),
540- },
541- )
542-
543- if max_count < 0 or max_count > 100000000 :
544- raise rest_framework .serializers .ValidationError (
545- {'max_count' : 'Must be between 0 and 100,000,000.' },
546- )
547-
548- elif mode == business_models .Promo .MODE_UNIQUE :
549- if promo_common is not None :
550- raise rest_framework .serializers .ValidationError (
551- {
552- 'promo_common' : (
553- 'This field is not allowed for UNIQUE mode.'
554- ),
555- },
556- )
557-
558- if max_count != 1 :
559- raise rest_framework .serializers .ValidationError (
560- {'max_count' : 'Must be 1 for UNIQUE mode.' },
561- )
562- else :
563- raise rest_framework .serializers .ValidationError (
564- {'mode' : 'Invalid mode.' },
565- )
566-
567- active_from = full_data .get ('active_from' )
568- active_until = full_data .get ('active_until' )
569-
570- if active_from and active_until and active_from > active_until :
571- raise rest_framework .serializers .ValidationError (
572- {'active_until' : 'Must be after or equal to active_from.' },
573- )
574-
575- return data
427+ data = super ().validate (data )
428+ validator = business .validators .PromoValidator (
429+ data = data ,
430+ instance = self .instance ,
431+ )
432+ return validator .validate ()
576433
577434 def get_like_count (self , obj ):
578435 return 0
0 commit comments