<s> from __future__ import unicode_literals <EOL> from decimal import Decimal <EOL> import random <EOL> from waffle . utils import get_setting , keyfmt <EOL> VERSION = ( <NUM_LIT:0> , <NUM_LIT:11> ) <EOL> __version__ = '<STR_LIT:.>' . join ( map ( str , VERSION ) ) <EOL> class DoesNotExist ( object ) : <EOL> """<STR_LIT>""" <EOL> @ property <EOL> def active ( self ) : <EOL> return get_setting ( '<STR_LIT>' ) <EOL> def set_flag ( request , flag_name , active = True , session_only = False ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( request , '<STR_LIT>' ) : <EOL> request . waffles = { } <EOL> request . waffles [ flag_name ] = [ active , session_only ] <EOL> def flag_is_active ( request , flag_name ) : <EOL> from . models import cache_flag , Flag <EOL> from . compat import cache <EOL> flag = cache . get ( keyfmt ( get_setting ( '<STR_LIT>' ) , flag_name ) ) <EOL> if flag is None : <EOL> try : <EOL> flag = Flag . objects . get ( name = flag_name ) <EOL> cache_flag ( instance = flag ) <EOL> except Flag . DoesNotExist : <EOL> return get_setting ( '<STR_LIT>' ) <EOL> if get_setting ( '<STR_LIT>' ) : <EOL> if flag_name in request . GET : <EOL> return request . GET [ flag_name ] == '<STR_LIT:1>' <EOL> if flag . everyone : <EOL> return True <EOL> elif flag . everyone is False : <EOL> return False <EOL> if flag . testing : <EOL> tc = get_setting ( '<STR_LIT>' ) % flag_name <EOL> if tc in request . GET : <EOL> on = request . GET [ tc ] == '<STR_LIT:1>' <EOL> if not hasattr ( request , '<STR_LIT>' ) : <EOL> request . waffle_tests = { } <EOL> request . waffle_tests [ flag_name ] = on <EOL> return on <EOL> if tc in request . COOKIES : <EOL> return request . COOKIES [ tc ] == '<STR_LIT:True>' <EOL> user = request . user <EOL> if flag . authenticated and user . is_authenticated ( ) : <EOL> return True <EOL> if flag . staff and user . is_staff : <EOL> return True <EOL> if flag . superusers and user . is_superuser : <EOL> return True <EOL> if flag . languages : <EOL> languages = flag . languages . split ( '<STR_LIT:U+002C>' ) <EOL> if ( hasattr ( request , '<STR_LIT>' ) and <EOL> request . LANGUAGE_CODE in languages ) : <EOL> return True <EOL> flag_users = cache . get ( keyfmt ( get_setting ( '<STR_LIT>' ) , <EOL> flag . name ) ) <EOL> if flag_users is None : <EOL> flag_users = flag . users . all ( ) <EOL> cache_flag ( instance = flag ) <EOL> if user in flag_users : <EOL> return True <EOL> flag_groups = cache . get ( keyfmt ( get_setting ( '<STR_LIT>' ) , <EOL> flag . name ) ) <EOL> if flag_groups is None : <EOL> flag_groups = flag . groups . all ( ) <EOL> cache_flag ( instance = flag ) <EOL> user_groups = user . groups . all ( ) <EOL> for group in flag_groups : <EOL> if group in user_groups : <EOL> return True <EOL> if flag . percent and flag . percent > <NUM_LIT:0> : <EOL> if not hasattr ( request , '<STR_LIT>' ) : <EOL> request . waffles = { } <EOL> elif flag_name in request . waffles : <EOL> return request . waffles [ flag_name ] [ <NUM_LIT:0> ] <EOL> cookie = get_setting ( '<STR_LIT>' ) % flag_name <EOL> if cookie in request . COOKIES : <EOL> flag_active = ( request . COOKIES [ cookie ] == '<STR_LIT:True>' ) <EOL> set_flag ( request , flag_name , flag_active , flag . rollout ) <EOL> return flag_active <EOL> if Decimal ( str ( random . uniform ( <NUM_LIT:0> , <NUM_LIT:100> ) ) ) <= flag . percent : <EOL> set_flag ( request , flag_name , True , flag . rollout ) <EOL> return True <EOL> set_flag ( request , flag_name , False , flag . rollout ) <EOL> return False <EOL> def switch_is_active ( switch_name ) : <EOL> from . models import cache_switch , Switch <EOL> from . compat import cache <EOL> switch = cache . get ( keyfmt ( get_setting ( '<STR_LIT>' ) , switch_name ) ) <EOL> if switch is None : <EOL> try : <EOL> switch = Switch . objects . get ( name = switch_name ) <EOL> cache_switch ( instance = switch ) <EOL> except Switch . DoesNotExist : <EOL> switch = DoesNotExist ( ) <EOL> switch . name = switch_name <EOL> cache_switch ( instance = switch ) <EOL> return switch . active <EOL> def sample_is_active ( sample_name ) : <EOL> from . models import cache_sample , Sample <EOL> from . compat import cache <EOL> sample = cache . get ( keyfmt ( get_setting ( '<STR_LIT>' ) , sample_name ) ) <EOL> if sample is None : <EOL> try : <EOL> sample = Sample . objects . get ( name = sample_name ) <EOL> cache_sample ( instance = sample ) <EOL> except Sample . DoesNotExist : <EOL> return get_setting ( '<STR_LIT>' ) <EOL> return Decimal ( str ( random . uniform ( <NUM_LIT:0> , <NUM_LIT:100> ) ) ) <= sample . percent </s>
