def _flag_key(self, ff, request): """ This creates a tuple key with various values to uniquely identify the request and flag """ d = SortedDict() d['name'] = ff.name d['ip_address'] = get_ip(request) #avoid fake requests for tests if hasattr(request, 'user'): d['user_id'] = request.user.id else: d['user_id'] = None return tuple(d.values())
def dolphin_test(request): """A test page to verify the flags dolphin considers active""" user_info = {'Authenticated':request.user.is_authenticated(), 'Is Staff':request.user.is_staff, 'Username':request.user.username} if settings.DOLPHIN_USE_GIS: user_info['Geolocation'] = utils.get_geoip_coords(utils.get_ip(request)) context = RequestContext(request, { 'user_info':user_info, 'flags':flipper.all_flags(request=request), 'flipper':flipper }) return render_to_response('dolphin_test.html', context_instance=context)
def _flag_is_active(self, flag, request): """ Checks the flag to see if it should be enabled or not. Encompases A/B tests, regional, and user based flags as well. Will only calculate random and max flags once per request. Will store flags for the request if DOLPHIN_STORE_FLAGS is True (default). """ key = self._flag_key(flag, request) flags = LocalStoreMiddleware.local.setdefault('flags', {}) store_flags = settings.DOLPHIN_STORE_FLAGS if store_flags and key in flags: return flags[key] def store(val): """quick wrapper to store the flag results if it needs to""" if flag.cookie_max_age: self.set_cookie(request, flag, val) if store_flags: flags[key] = val return val if not flag.enabled: return store(False) enabled_for_site = self._limit('enabled_for_site', flag, self._enabled_for_site, request) if not enabled_for_site: return store(False) enabled = True if flag.registered_only or flag.limit_to_group or flag.staff_only: #user based flag if not request: enabled = False elif not request.user.is_authenticated(): enabled = False else: if flag.limit_to_group: enabled = enabled and self._in_group(flag, request) if flag.staff_only: enabled = enabled and request.user.is_staff if flag.registered_only: enabled = enabled and True if enabled == False: return store(enabled) if flag.enable_geo: #distance based x = get_geoip_coords(get_ip(request)) if x is None or flag.center is None: enabled = False else: enabled = enabled and self._in_circle(flag, x[0], x[1]) if enabled == False: return store(enabled) #A/B flags if flag.random: #doing this so that the random key is only calculated once per request def rand_bool(flag): random.seed(time.time()) return bool(random.randrange(0, 2)) enabled = enabled and self._limit('random', flag, rand_bool, request) if flag.b_test_start: #start date if flag.b_test_start.tzinfo is not None: now = datetime.utcnow().replace(tzinfo=pytz.UTC) else: now = datetime.now() enabled = enabled and now >= flag.b_test_start if flag.b_test_end: #end date if flag.b_test_end.tzinfo is not None: now = datetime.utcnow().replace(tzinfo=pytz.UTC) else: now = datetime.now() enabled = enabled and now <= flag.b_test_end if flag.maximum_b_tests: #max B tests enabled = enabled and self._limit('maxb', flag, self._check_maxb, request) percent_enabled = self._limit('percent', flag, self._check_percent, request) enabled = enabled and percent_enabled return store(enabled)