1
- # Copyright 2017-2021 , Optimizely
1
+ # Copyright 2017-2022 , Optimizely
2
2
# Licensed under the Apache License, Version 2.0 (the "License");
3
3
# you may not use this file except in compliance with the License.
4
4
# You may obtain a copy of the License at
@@ -378,8 +378,8 @@ def get_variation_for_rollout(self, project_config, feature, user):
378
378
# check forced decision first
379
379
rule = rollout_rules [index ]
380
380
optimizely_decision_context = OptimizelyUserContext .OptimizelyDecisionContext (feature .key , rule .key )
381
- forced_decision_variation , reasons_received = user . find_validated_forced_decision (
382
- optimizely_decision_context )
381
+ forced_decision_variation , reasons_received = self . validated_forced_decision (
382
+ project_config , optimizely_decision_context , user )
383
383
decide_reasons += reasons_received
384
384
385
385
if forced_decision_variation :
@@ -464,8 +464,8 @@ def get_variation_for_feature(self, project_config, feature, user_context, optio
464
464
optimizely_decision_context = OptimizelyUserContext .OptimizelyDecisionContext (feature .key ,
465
465
experiment .key )
466
466
467
- forced_decision_variation , reasons_received = user_context . find_validated_forced_decision (
468
- optimizely_decision_context )
467
+ forced_decision_variation , reasons_received = self . validated_forced_decision (
468
+ project_config , optimizely_decision_context , user_context )
469
469
decide_reasons += reasons_received
470
470
471
471
if forced_decision_variation :
@@ -489,3 +489,61 @@ def get_variation_for_feature(self, project_config, feature, user_context, optio
489
489
if rollout_variation_reasons :
490
490
decide_reasons += rollout_variation_reasons
491
491
return variation , decide_reasons
492
+
493
+ def validated_forced_decision (self , project_config , decision_context , user_context ):
494
+ """
495
+ Gets forced decisions based on flag key, rule key and variation.
496
+
497
+ Args:
498
+ project_config: a project config
499
+ decision context: a decision context
500
+ user_context context: a user context
501
+
502
+ Returns:
503
+ Variation of the forced decision.
504
+ """
505
+ reasons = []
506
+
507
+ forced_decision = user_context .get_forced_decision (decision_context )
508
+
509
+ flag_key = decision_context .flag_key
510
+ rule_key = decision_context .rule_key
511
+
512
+ if forced_decision :
513
+ if not project_config :
514
+ return None , reasons
515
+ variation = project_config .get_flag_variation (flag_key , 'key' , forced_decision .variation_key )
516
+ if variation :
517
+ if rule_key :
518
+ user_has_forced_decision = enums .ForcedDecisionLogs \
519
+ .USER_HAS_FORCED_DECISION_WITH_RULE_SPECIFIED .format (forced_decision .variation_key ,
520
+ flag_key ,
521
+ rule_key ,
522
+ user_context .user_id )
523
+
524
+ else :
525
+ user_has_forced_decision = enums .ForcedDecisionLogs \
526
+ .USER_HAS_FORCED_DECISION_WITHOUT_RULE_SPECIFIED .format (forced_decision .variation_key ,
527
+ flag_key ,
528
+ user_context .user_id )
529
+
530
+ reasons .append (user_has_forced_decision )
531
+ user_context .logger .info (user_has_forced_decision )
532
+
533
+ return variation , reasons
534
+
535
+ else :
536
+ if rule_key :
537
+ user_has_forced_decision_but_invalid = enums .ForcedDecisionLogs \
538
+ .USER_HAS_FORCED_DECISION_WITH_RULE_SPECIFIED_BUT_INVALID .format (flag_key ,
539
+ rule_key ,
540
+ user_context .user_id )
541
+ else :
542
+ user_has_forced_decision_but_invalid = enums .ForcedDecisionLogs \
543
+ .USER_HAS_FORCED_DECISION_WITHOUT_RULE_SPECIFIED_BUT_INVALID .format (flag_key ,
544
+ user_context .user_id )
545
+
546
+ reasons .append (user_has_forced_decision_but_invalid )
547
+ user_context .logger .info (user_has_forced_decision_but_invalid )
548
+
549
+ return None , reasons
0 commit comments